Esempio n. 1
0
        /// <summary>
        /// Exports the PDF pages as Tiff Images.
        /// </summary>
        /// <param name="fileName">The PDF file name</param>
        private void ExportPDFtoTiff(string fileName)
        {
            PdfDocumentView pdfViewer = new PdfDocumentView();

            //Load the input PDF file
            pdfViewer.Load(fileName);
            //Export the images From the input PDF file at the page range of 0 to 1 .
            BitmapSource[] image = pdfViewer.ExportAsImage(0, pdfViewer.PageCount - 1);
            if (image != null)
            {
                for (int i = 0; i < image.Length; i++)
                {
                    //Initialize the new Tiff bitmap encoder
                    TiffBitmapEncoder encoder = new TiffBitmapEncoder();
                    //Set the compression to zip to reduce the file size.
                    encoder.Compression = TiffCompressOption.Zip;
                    //Create the bitmap frame using the bitmap source and add it to the encoder.
                    encoder.Frames.Add(BitmapFrame.Create(image[i]));
                    //Create the file stream for the output in the desired image format.
                    using (FileStream stream = new FileStream("Image_" + i.ToString() + ".Tiff", FileMode.Create))
                    {
                        //Save the stream, so that the image will be generated in the output location.
                        encoder.Save(stream);
                    }
                }
            }
        }
Esempio n. 2
0
        public string ConvertPdfToImage(string inputFilePath)
        {
            var imgPath = string.Empty;

            using (var viewer = new PdfDocumentView())
            {
                var file = new FileInfo(inputFilePath);
                viewer.Load(file.FullName);
                var images = viewer.ExportAsImage(PageNumber - 1, PageNumber - 1);
                imgPath = Path.Combine(Common.TempStorage, $"{file.Name.Substring(0, file.Name.Length - 4)}.png");
                var resizedPath = Path.Combine(Common.UserCriteriaStorage, $"{file.Name.Substring(0, file.Name.Length - 4)}-R.png");
                var image       = images[0];
                if (File.Exists(imgPath))
                {
                    File.Delete(imgPath);
                }
                image.Save(imgPath);
                double scaleFactor = 0;
                var    resize      = false;
                using (var bmp = Image.FromFile(imgPath))
                {
                    if (bmp.Size.Width > 1428)
                    {
                        scaleFactor = 1428.0 / Convert.ToDouble(bmp.Size.Width);
                        resize      = true;
                    }
                }
                if (resize)
                {
                    Common.Resize(imgPath, resizedPath, scaleFactor);
                }
            }
            return(imgPath);
        }
        void ExportPages(Window window)
        {
            int from = StartPageNumber - 1;
            int to   = EndPageNumber - 1;

            if (from > to)
            {
                MessageBox.Show("The 'From' value cannot be greater than the 'To' value.", "Error");
                return;
            }
            BitmapSource image = null;

            BitmapSource[] images = null;

            if (from == to)
            {
                image = pdfViewer.ExportAsImage(from);
            }
            else
            {
                images = pdfViewer.ExportAsImage(from, to);
            }
#if NET50
            string output = @"ExportasImage\";

            System.IO.Directory.CreateDirectory(@"ExportasImage\");
#else
            string output = @"ExportasImage\";

            System.IO.Directory.CreateDirectory(@"ExportasImage\");
#endif

            BitmapEncoder encoder = null;
            if (image != null)
            {
                encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(image));
                using (FileStream stream = new FileStream(output + Guid.NewGuid().ToString() + ".png", FileMode.Create))
                {
                    encoder.Save(stream);
                }
            }
            else
            {
                foreach (BitmapSource img in images)
                {
                    encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(img));
                    using (FileStream stream = new FileStream(output + Guid.NewGuid().ToString() + ".png", FileMode.Create))
                    {
                        encoder.Save(stream);
                    }
                }
            }

            if (image != null || images != null)
            {
                //Message box confirmation to view the created PDF document.
                if (System.Windows.MessageBox.Show("Do you want to view the exported image files?", "Image Exported",
                                                   MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
                {
                    //Launching the PDF file using the default Application.[Acrobat Reader]
#if NET50
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo = new System.Diagnostics.ProcessStartInfo(@"ExportasImage\")
                    {
                        UseShellExecute = true
                    };
                    process.Start();
#else
                    System.Diagnostics.Process.Start(@"ExportasImage\");
#endif
                    if (window != null)
                    {
                        window.Close();
                    }
                }
                else
                // Exit
                if (window != null)
                {
                    window.Close();
                }
                pdfViewer.Unload(true);
            }
        }