public static void Convert()
        {
            //Copy "x86" and "x64" folders from download package to your .NET project Bin folder.
            PdfToImageConverter converter = new PdfToImageConverter();

            converter.Load(File.ReadAllBytes("sample.pdf"));

            //Default is 72, the higher DPI, the bigger size out image will be
            converter.DPI = 96;
            //The value need to be 1-100. If set to 100, the converted image will take the
            //original quality with less time and memory. If set to 1, the converted image
            //will be compressed to minimum size with more time and memory.
            //converter.CompressedRatio = 80;

            for (int i = 0; i < converter.PageCount; i++)
            {
                //The converted image will keep the original size of PDF page
                Image pageImage = converter.PageToImage(i);
                //To specific the converted image size by width and height
                //Image pageImage = converter.PageToImage(i, 100, 150);
                //You can save this Image object to jpeg, tiff and png format to local file.
                //Or you can make it in memory to other use.
                pageImage.Save(i.ToString() + ".jpg", ImageFormat.Jpeg);
            }

            converter.Dispose();
        }
        public ActionResult UploadPdf()
        {
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path     = Path.Combine(Server.MapPath("~/Images/"), fileName);
                    file.SaveAs(path);

                    //var fileStream = new FileStream(path,
                    //                 FileMode.Open,
                    //                 FileAccess.Read
                    //               );
                    //var fsResult = new FileStreamResult(fileStream, "application/pdf");
                    //return fsResult;

                    var converter = new PdfToImageConverter();
                    var images    = converter.Convert(path, fileName, Server.MapPath("~/Images/"), ImageFormat.Bmp);
                }
            }
            return(View());
        }
Esempio n. 3
0
        public void TestGetImage()
        {
            MainController      controller = new MainController("testdoc.pdf");
            PdfToImageConverter converter  = new PdfToImageConverter();
            PdfDocument         doc        = controller.GetDocument();

            Image img = converter.PdfToImageByPage(0, doc);

            Assert.IsNull(img);
        }
Esempio n. 4
0
        public void SaveImage(int pageNum)
        {
            _ = new HtmlGenericControl("div");
            PdfToImageConverter converter = new PdfToImageConverter();
            string imgPath = finder.GetImgFilePath(fileFromUI, pageNum);

            PdfDocument document = GetDocument();

            Image pageImg = converter.PdfToImageByPage(pageNum, document);

            pageImg.Save(HostingEnvironment.MapPath(imgPath));
        }
        public ActionResult GetPdfPageImage(int page)
        {
            var pdfFilePath = HttpContext.Server.MapPath("~/App_Data/Sample3.PDF");

            var pdfToImg = new PdfToImageConverter();

            pdfToImg.ScaleTo = 800;

            var memStream = new MemoryStream();

            pdfToImg.GenerateImage(pdfFilePath, page, ImageFormat.Png, memStream);
            return(File(memStream.ToArray(), "image/png"));
        }
        public static void Convert2()
        {
            //Copy "x86" and "x64" folders from download package to your .NET project Bin folder.
            PdfToImageConverter converter = new PdfToImageConverter();

            //Default is 72, the higher DPI, the bigger size out image will be
            converter.DPI = 96;

            using (Stream stream = File.OpenRead("sample.pdf"))
            {
                converter.Load(stream);
                //Save pdf to multiple pages tiff to local file
                converter.DocumentToMultiPageTiff("convert.tiff");
                //Or save the multiple pages tiff in memory to other use
                //Image multipageTif = converter.DocumentToMultiPageTiff();
            }
        }
Esempio n. 7
0
        public static Stream ConvertPageToJpg(this PdfPage pdfPage)
        {
            var converter = new PdfToImageConverter();

            return(converter.ConvertToJpgStream(pdfPage));
        }
Esempio n. 8
0
        public static IEnumerable <Stream> ConvertToJpgStreams(this PdfDocument pdfDocument)
        {
            var converter = new PdfToImageConverter();

            return(converter.ConvertToJpgStreams(pdfDocument));
        }
Esempio n. 9
0
        public static Bitmap ConvertPageToBitmap(this PdfPage pdfPage)
        {
            var converter = new PdfToImageConverter();

            return(converter.ConvertToBitmap(pdfPage));
        }
Esempio n. 10
0
        public static IEnumerable <Bitmap> ConvertToBitmaps(this PdfDocument pdfDocument)
        {
            var converter = new PdfToImageConverter();

            return(converter.ConvertToBitmaps(pdfDocument));
        }
Esempio n. 11
0
        public async Task <byte[]> getImagePreview(TFile file)
        {
            try
            {
                byte[] outPreview = null;
                bool   topImage   = false;
                if (file.TipoArchivo.Contains("video"))
                {
                    try {
found:
                        string nameMD5 = DateTime.Now.Ticks.GetHashCode().ToString("x").ToUpper();
                        var VideoPath = AppDomain.CurrentDomain.BaseDirectory + @"PreviewFile\Video_" + nameMD5 + "." + file.Nombre.Split('.')[file.Nombre.Split('.').Length - 1];
                        if (File.Exists(VideoPath))
                        {
                            goto found;
                        }
                        File.WriteAllBytes(VideoPath, file.ArchivoData);
                        var imageFile = AppDomain.CurrentDomain.BaseDirectory + @"PreviewFile\VidImage_" + nameMD5 + ".JPG";
                        var urlFile   = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                        FFmpeg.SetExecutablesPath(urlFile + @"\MediaToolkit", "ffmpeg");
                        var infoVideo = await FFmpeg.GetMediaInfo(VideoPath);

                        var conversion = await FFmpeg.Conversions.FromSnippet.Snapshot(VideoPath, imageFile, infoVideo.Duration / 2.0);

                        await conversion.Start();

                        outPreview = File.ReadAllBytes(imageFile);
                        File.Delete(VideoPath);
                        File.Delete(imageFile);
                    }
                    catch (Exception e) {
                        outPreview = new byte[0];
                    }
                }
                else if (file.TipoArchivo.Contains("pdf"))
                {
                    try
                    {
again:
                        string nameMD5 = DateTime.Now.Ticks.GetHashCode().ToString("x").ToUpper();
                        var pdfFile = AppDomain.CurrentDomain.BaseDirectory + @"PreviewFile\DocPdf_" + nameMD5 + ".pdf";
                        if (File.Exists(pdfFile))
                        {
                            goto again;
                        }
                        File.WriteAllBytes(pdfFile, file.ArchivoData);
                        var imageFile = AppDomain.CurrentDomain.BaseDirectory + @"PreviewFile\DocImg_" + nameMD5 + ".JPG";
                        var pdfToImg  = new PdfToImageConverter();
                        pdfToImg.ScaleTo = 3000; // fit 200x200 box
                        pdfToImg.GenerateImage(pdfFile, 1, ImageFormat.Jpeg, imageFile);
                        outPreview = File.ReadAllBytes(imageFile);
                        File.Delete(pdfFile);
                        File.Delete(imageFile);
                        topImage = true;
                    }
                    catch (Exception e)
                    {
                        outPreview = new byte[0];
                    }
                }
                else
                {
                    outPreview = file.ArchivoData;
                }
                Stream stream  = new MemoryStream(outPreview);
                Image  imageIn = Image.FromStream(stream);
                Bitmap bmPhoto = new Bitmap(imageIn);

                // recortar imagen
                int sourceWidth = bmPhoto.Width;
                int sourceHeight = bmPhoto.Height;
                int cuadro = 0;
                int x = 0, y = 0;
//                double div = (minWidth * 1.0 / minHeight * 1.0);
                if (sourceWidth > sourceHeight)
                {
                    cuadro = sourceHeight;
                    x      = sourceWidth / 2 - sourceHeight / 2;
                }
                else
                {
                    cuadro = sourceWidth;
                    if (topImage)
                    {
                        y = 0;
                    }
                    else
                    {
                        y = sourceHeight / 2 - sourceWidth / 2;
                    }
                }
                Rectangle cropArea = new Rectangle(x, y, cuadro, cuadro);

                Bitmap bmpCrop = bmPhoto.Clone(cropArea, bmPhoto.PixelFormat);

                //cambiar tamaño
                Bitmap bmpRsize = resizeImage(bmpCrop);
                // cambiar resolucion
                bmpRsize.SetResolution(50, 50);
                MemoryStream ms = new MemoryStream();

                bmpRsize.Save(ms, imageIn.RawFormat);
                return(ms.ToArray());
            }
            catch (Exception ex)
            {
                return(new byte[0]);
            }
        }