Exemple #1
0
        /// <summary>
        /// Generates all PDF pages as JPEG files for a given pdf source and a destination folder.
        /// </summary>
        /// <param name="source">The PDF source</param>
        /// <param name="destination">The destination folder</param>
        /// <param name="width">Used to get a smaller or bigger JPEG file, depending on the specified value</param>
        /// <param name="password">The password for the pdf file (if required)</param>
        public static void GeneratePagesAtWidth(IPdfSource source, string destination, int width, string password = null)
        {
            if (!Directory.Exists(destination))
            {
                throw new DirectoryNotFoundException("The directory \"" + destination + "\" does not exist !");
            }

            using (var stream = new PdfFileStream(source))
            {
                ValidatePassword(stream.Document, password);
                int pageCount  = CountPages(source);
                var currentDpi = DpiHelper.GetCurrentDpi();

                for (int i = 0; i < pageCount; i++)
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    IntPtr p   = NativeMethods.LoadPage(stream.Document, i); // loads the page
                    var    bmp = RenderPageAtWidth(stream.Context, stream.Document, p, width, currentDpi.HorizontalDpi, currentDpi.VerticalDpi);
                    NativeMethods.FreePage(stream.Document, p);              // releases the resources consumed by the page
                    bmp.Save(Path.Combine(destination, i + generateOutputExtension), generateOutputFormat);
                }
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
Exemple #2
0
 /// <summary>
 /// Return the total number of pages for a give PDF filename.
 /// </summary>
 public static int CountPages(string pdfFilename)
 {
     using (var stream = new PdfFileStream(pdfFilename))
     {
         return(NativeMethods.CountPages(stream.Document));                // gets the number of pages in the document
     }
 }
Exemple #3
0
        /// <summary>
        ///     Gets the page bounds for all pages of the given PDF. If a relevant rotation is supplied, the bounds will
        ///     be rotated accordingly before returning.
        /// </summary>
        /// <param name="rotation">The rotation that should be applied</param>
        /// <param name="password">The password for the pdf file (if required)</param>
        /// <returns></returns>
        public static Size[] GetPageBounds(IPdfSource source, ImageRotation rotation = ImageRotation.None,
            string password = null)
        {
            Func<double, double, Size> sizeCallback = (width, height) => new Size(width, height);

            if (rotation == ImageRotation.Rotate90 || rotation == ImageRotation.Rotate270)
                sizeCallback = (width, height) => new Size(height, width); // switch width and height

            using (var stream = new PdfFileStream(source))
            {
                ValidatePassword(stream.Document, password);

                var pageCount = NativeMethods.CountPages(stream.Document); // gets the number of pages in the document
                var resultBounds = new Size[pageCount];

                for (var i = 0; i < pageCount; i++)
                {
                    var p = NativeMethods.LoadPage(stream.Document, i); // loads the page
                    var pageBound = NativeMethods.BoundPage(stream.Document, p);

                    resultBounds[i] = sizeCallback(pageBound.Width, pageBound.Height);

                    NativeMethods.FreePage(stream.Document, p); // releases the resources consumed by the page
                }

                return resultBounds;
            }
        }
Exemple #4
0
 public static bool NeedsPassword(IPdfSource source)
 {
     using (var stream = new PdfFileStream(source))
     {
         return(NeedsPassword(stream.Document));
     }
 }
Exemple #5
0
        /// <summary>
        /// Gets the page bounds for all pages of the given PDF. If a relevant rotation is supplied, the bounds will
        /// be rotated accordingly before returning.
        /// </summary>
        /// <param name="rotation">The rotation that should be applied</param>
        /// <param name="password">The password for the pdf file (if required)</param>
        /// <returns></returns>
        public static System.Windows.Size[] GetPageBounds(IPdfSource source, ImageRotation rotation = ImageRotation.None, string password = null)
        {
            Func <double, double, System.Windows.Size> sizeCallback = (width, height) => new System.Windows.Size(width, height);

            if (rotation == ImageRotation.Rotate90 || rotation == ImageRotation.Rotate270)
            {
                sizeCallback = (width, height) => new System.Windows.Size(height, width);                 // switch width and height
            }
            using (var stream = new PdfFileStream(source))
            {
                ValidatePassword(stream.Document, password);

                var pageCount    = NativeMethods.CountPages(stream.Document);              // gets the number of pages in the document
                var resultBounds = new System.Windows.Size[pageCount];

                for (int i = 0; i < pageCount; i++)
                {
                    IntPtr    p         = NativeMethods.LoadPage(stream.Document, i);          // loads the page
                    Rectangle pageBound = NativeMethods.BoundPage(stream.Document, p);

                    resultBounds[i] = sizeCallback(pageBound.Width, pageBound.Height);

                    NativeMethods.FreePage(stream.Document, p);                     // releases the resources consumed by the page
                }

                return(resultBounds);
            }
        }
Exemple #6
0
        /// <summary>
        /// Generates all PDF pages as JPEG files for a given pdf source and a destination folder.
        /// </summary>
        /// <param name="source">The PDF source</param>
        /// <param name="destination">The destination folder</param>
        /// <param name="size">Used to get a smaller or bigger JPEG file, depending on the specified value</param>
        /// <param name="password">The password for the pdf file (if required)</param>
        public static void GeneratePagesAtSize(IPdfSource source, string destination, Size size, string password = null)
        {
            if (!Directory.Exists(destination))
            {
                throw new DirectoryNotFoundException("The directory \"" + destination + "\" does not exist !");
            }

            using (var stream = new PdfFileStream(source))
            {
                ValidatePassword(stream.Document, password);
                int pageCount = CountPages(source);
                var currentDpi = DpiHelper.GetCurrentDpi();

                for (int i = 0; i < pageCount; i++)
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    IntPtr p = NativeMethods.LoadPage(stream.Document, i); // loads the page
                    var bmp = RenderPageAtSize(stream.Context, stream.Document, p, size, currentDpi.HorizontalDpi, currentDpi.VerticalDpi);
                    NativeMethods.FreePage(stream.Document, p); // releases the resources consumed by the page
                    bmp.Save(Path.Combine(destination,  i + generateOutputExtension), generateOutputFormat);
                }
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
Exemple #7
0
        /// <summary>
        /// Return the total number of pages for a give PDF.
        /// </summary>
        public static int CountPages(IPdfSource source, string password = null)
        {
            using (var stream = new PdfFileStream(source))
            {
                ValidatePassword(stream.Document, password);

                return(NativeMethods.CountPages(stream.Document));                // gets the number of pages in the document
            }
        }
Exemple #8
0
        /// <summary>
        /// Extracts a PDF page as a Bitmap for a given pdf filename and a page number.
        /// </summary>
        /// <param name="pdfFilename">Must be the full path for an existing PDF file</param>
        /// <param name="pageNumber">Page number, starting at 1</param>
        /// <param name="zoomFactor">Used to get a smaller or bigger Bitmap, depending on the specified value</param>
        public static Bitmap ExtractPage(string pdfFilename, int pageNumber, float zoomFactor = 1.0f)
        {
            var pageNumberIndex = Math.Max(0, pageNumber - 1);             // pages start at index 0

            using (var stream = new PdfFileStream(pdfFilename))
            {
                IntPtr p   = NativeMethods.LoadPage(stream.Document, pageNumberIndex); // loads the page
                var    bmp = RenderPage(stream.Context, stream.Document, p, zoomFactor);
                NativeMethods.FreePage(stream.Document, p);                            // releases the resources consumed by the page

                return(bmp);
            }
        }
Exemple #9
0
		/// <summary>
		/// Extracts a PDF page as a Bitmap for a given pdf filename and a page number.
		/// </summary>
		/// <param name="pageNumber">Page number, starting at 1</param>
		/// <param name="zoomFactor">Used to get a smaller or bigger Bitmap, depending on the specified value</param>
        /// <param name="password">The password for the pdf file (if required)</param>
        public static Bitmap ExtractPage(IPdfSource source, int pageNumber, float zoomFactor = 1.0f, string password = null)
		{
			var pageNumberIndex = Math.Max(0, pageNumber - 1); // pages start at index 0

            using (var stream = new PdfFileStream(source))
			{
                ValidatePassword(stream.Document, password);

				IntPtr p = NativeMethods.LoadPage(stream.Document, pageNumberIndex); // loads the page
				var bmp = RenderPage(stream.Context, stream.Document, p, zoomFactor);
				NativeMethods.FreePage(stream.Document, p); // releases the resources consumed by the page

				return bmp;
			}
		}
Exemple #10
0
        /// <summary>
        /// Extracts a PDF page as a Bitmap for a given pdf filename and a page number.
        /// </summary>
        /// <param name="pageNumber">Page number, starting at 1</param>
        /// <param name="zoomFactor">Used to get a smaller or bigger Bitmap, depending on the specified value</param>
        /// <param name="password">The password for the pdf file (if required)</param>
        public static Bitmap ExtractPage(IPdfSource source, int pageNumber, float zoomFactor = 1.0f, string password = null)
        {
            var pageNumberIndex = Math.Max(0, pageNumber - 1); // pages start at index 0

            using (var stream = new PdfFileStream(source))
            {
                ValidatePassword(stream.Context, stream.Document, password);

                IntPtr p   = NativeMethods.LoadPage(stream.Context, stream.Document, pageNumberIndex); // loads the page
                var    bmp = RenderPage(stream.Context, p, zoomFactor);
                NativeMethods.DropPage(stream.Context, p);                                             // releases the resources consumed by the page

                return(bmp);
            }
        }
Exemple #11
0
        /// <summary>
        /// Gets the page bounds for a given pdf source and a page number.
        /// </summary>
        /// <param name="source">The PDF source</param>
        /// <param name="pageNumber">Page number, starting at 0</param>
        /// <param name="password">The password for the pdf file (if required)</param>
        /// <returns></returns>
        public static System.Windows.Size GetPageBound(IPdfSource source, int pageNumber, string password = null)
        {
            if (pageNumber < 0 || pageNumber >= CountPages(source))
            {
                throw new ArgumentOutOfRangeException("pageNumber", "The page \"" + pageNumber + "\" does not exist !");
            }

            using (var stream = new PdfFileStream(source))
            {
                ValidatePassword(stream.Document, password);

                IntPtr    p         = NativeMethods.LoadPage(stream.Document, pageNumber); // loads the page
                Rectangle pageBound = NativeMethods.BoundPage(stream.Document, p);
                NativeMethods.FreePage(stream.Document, p);                                // releases the resources consumed by the page

                return(new System.Windows.Size(pageBound.Width, pageBound.Height));
            }
        }
Exemple #12
0
        /// <summary>
        /// Extracts a PDF page as a Bitmap for a given pdf source and a page number.
        /// </summary>
        /// <param name="source">The PDF source</param>
        /// <param name="pageNumber">Page number, starting at 0</param>
        /// <param name="zoomFactor">Used to get a smaller or bigger Bitmap, depending on the specified value</param>
        /// <param name="password">The password for the pdf file (if required)</param>
        public static BitmapSource ExtractPageAtScale(IPdfSource source, int pageNumber, float zoomFactor = 1.0f, string password = null)
        {
            if (pageNumber < 0 || pageNumber >= CountPages(source))
            {
                throw new ArgumentOutOfRangeException("pageNumber", "The page \"" + pageNumber + "\" does not exist !");
            }

            using (var stream = new PdfFileStream(source))
            {
                ValidatePassword(stream.Document, password);

                IntPtr p          = NativeMethods.LoadPage(stream.Document, pageNumber); // loads the page
                var    currentDpi = DpiHelper.GetCurrentDpi();
                var    bmp        = RenderPageAtScale(stream.Context, stream.Document, p, zoomFactor, currentDpi.HorizontalDpi, currentDpi.VerticalDpi);
                NativeMethods.FreePage(stream.Document, p); // releases the resources consumed by the page

                return(bmp.ToBitmapSource());
            }
        }
        /// <summary>
        ///     Extracts a PDF page as a Bitmap for a given pdf filename and a page number.
        /// </summary>
        /// <param name="pageNumber">Page number, starting at 1</param>
        /// <param name="zoomFactor">Used to get a smaller or bigger Bitmap, depending on the specified value</param>
        /// <param name="password">The password for the pdf file (if required)</param>
        public static Bitmap ExtractPage(IPdfSource source, int pageNumber, float zoomFactor = 1.0f,
                                         string password = null)
        {
            var pageNumberIndex = Math.Max(0, pageNumber - 1); // pages start at index 0

            using (var stream = new PdfFileStream(source))
            {
                ValidatePassword(stream.Document, password);
                //TODO below method fails sometimes
                var p = new IntPtr();
                try
                {
                    p = NativeMethods.LoadPage(stream.Document, pageNumberIndex); // loads the page
                }
                catch (Exception ex)
                {
                    Logger.Log.Error("Error extracting cover image with MuPDF", ex);
                }
                var bmp = RenderPage(stream.Context, stream.Document, p, zoomFactor);
                NativeMethods.FreePage(stream.Document, p); // releases the resources consumed by the page

                return(bmp);
            }
        }
Exemple #14
0
        /// <summary>
        ///     Extracts a PDF page as a Bitmap for a given pdf filename and a page number.
        /// </summary>
        /// <param name="pageNumber">Page number, starting at 1</param>
        /// <param name="zoomFactor">Used to get a smaller or bigger Bitmap, depending on the specified value</param>
        /// <param name="password">The password for the pdf file (if required)</param>
        public static Bitmap ExtractPage(IPdfSource source, int pageNumber, float zoomFactor = 1.0f,
            string password = null)
        {
            var pageNumberIndex = Math.Max(0, pageNumber - 1); // pages start at index 0

            using (var stream = new PdfFileStream(source))
            {
                ValidatePassword(stream.Document, password);
                //TODO below method fails sometimes
                var p = new IntPtr();
                try
                {
                    p = NativeMethods.LoadPage(stream.Document, pageNumberIndex); // loads the page
                }
                catch (Exception ex)
                {
                    Logger.Log.Error("Error extracting cover image with MuPDF", ex);
                }
                var bmp = RenderPage(stream.Context, stream.Document, p, zoomFactor);
                NativeMethods.FreePage(stream.Document, p); // releases the resources consumed by the page

                return bmp;
            }
        }
Exemple #15
0
 public static bool NeedsPassword(IPdfSource source)
 {
     using (var stream = new PdfFileStream(source))
     {
         return NeedsPassword(stream.Document);
     }
 }
Exemple #16
0
        /// <summary>
        ///     Return the total number of pages for a give PDF.
        /// </summary>
        public static int CountPages(IPdfSource source, string password = null)
        {
            using (var stream = new PdfFileStream(source))
            {
                ValidatePassword(stream.Document, password);

                return NativeMethods.CountPages(stream.Document); // gets the number of pages in the document
            }
        }
Exemple #17
0
        /// <summary>
        /// Extracts a PDF page as a Bitmap for a given pdf source and a page number.
        /// </summary>
        /// <param name="source">The PDF source</param>
        /// <param name="pageNumber">Page number, starting at 0</param>
        /// <param name="zoomFactor">Used to get a smaller or bigger Bitmap, depending on the specified value</param>
        /// <param name="password">The password for the pdf file (if required)</param>
        public static BitmapSource ExtractPageAtScale(IPdfSource source, int pageNumber, float zoomFactor = 1.0f, string password = null)
        {
            if (pageNumber < 0 || pageNumber >= CountPages(source))
            {
                throw new ArgumentOutOfRangeException("pageNumber", "The page \"" + pageNumber + "\" does not exist !");
            }

            using (var stream = new PdfFileStream(source))
            {
                ValidatePassword(stream.Document, password);

                IntPtr p = NativeMethods.LoadPage(stream.Document, pageNumber); // loads the page
                var currentDpi = DpiHelper.GetCurrentDpi();
                var bmp = RenderPageAtScale(stream.Context, stream.Document, p, zoomFactor, currentDpi.HorizontalDpi, currentDpi.VerticalDpi);
                NativeMethods.FreePage(stream.Document, p); // releases the resources consumed by the page

                return bmp.ToBitmapSource();
            }
        }
Exemple #18
0
        /// <summary>
        /// Gets the page bounds for a given pdf source and a page number.
        /// </summary>
        /// <param name="source">The PDF source</param>
        /// <param name="pageNumber">Page number, starting at 0</param>
        /// <param name="password">The password for the pdf file (if required)</param>
        /// <returns></returns>
        public static System.Windows.Size GetPageBound(IPdfSource source, int pageNumber, string password = null)
        {
            if (pageNumber < 0 || pageNumber >= CountPages(source))
            {
                throw new ArgumentOutOfRangeException("pageNumber", "The page \"" + pageNumber + "\" does not exist !");
            }

            using (var stream = new PdfFileStream(source))
            {
                ValidatePassword(stream.Document, password);

                IntPtr p = NativeMethods.LoadPage(stream.Document, pageNumber); // loads the page
                Rectangle pageBound = NativeMethods.BoundPage(stream.Document, p);
                NativeMethods.FreePage(stream.Document, p); // releases the resources consumed by the page

                return new System.Windows.Size(pageBound.Width, pageBound.Height);
            }
        }