コード例 #1
0
ファイル: MuPdfWrapper.cs プロジェクト: aragoubi/Project
        /// <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();
        }
コード例 #2
0
        public void Load(IPdfSource source, string password = null)
        {
            this.virtualPanel = VisualTreeHelperEx.FindChild <CustomVirtualizingPanel>(this);
            this.scrollViewer = VisualTreeHelperEx.FindChild <ScrollViewer>(this);
            this.virtualPanel.PageRowBounds = this.parent.PageRowBounds.Select(f => f.SizeIncludingOffset).ToArray();
            this.imageProvider = new PdfImageProvider(source, this.parent.TotalPages,
                                                      new PageDisplaySettings(this.parent.GetPagesPerRow(), this.parent.ViewType, this.parent.HorizontalMargin, this.parent.Rotation),
                                                      password: password);

            if (this.parent.ZoomType == ZoomType.Fixed)
            {
                this.CreateNewItemsSource();
            }
            else if (this.parent.ZoomType == ZoomType.FitToHeight)
            {
                this.ZoomToHeight();
            }
            else if (this.parent.ZoomType == ZoomType.FitToWidth)
            {
                this.ZoomToWidth();
            }

            if (this.scrollViewer != null)
            {
                this.scrollViewer.Visibility = System.Windows.Visibility.Visible;
                this.scrollViewer.ScrollToTop();
            }
        }
コード例 #3
0
ファイル: MuPdfWrapper.cs プロジェクト: aragoubi/Nine
        /// <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();
        }
コード例 #4
0
ファイル: MuPdfWrapper.cs プロジェクト: nathanashton/Bookie
        /// <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;
            }
        }
コード例 #5
0
        public void Open(IPdfSource source, string password = null)
        {
            var pw = password;

            if (this.PasswordRequired != null && MuPdfWrapper.NeedsPassword(source) && pw == null)
            {
                var e = new PasswordRequiredEventArgs();
                this.PasswordRequired(this, e);

                if (e.Cancel)
                {
                    return;
                }

                pw = e.Password;
            }

            this.LoadPdf(source, pw);
            this.CurrentSource   = source;
            this.CurrentPassword = pw;

            if (this.PdfLoaded != null)
            {
                this.PdfLoaded(this, EventArgs.Empty);
            }
        }
コード例 #6
0
        public void Load(IPdfSource source, string password = null)
        {
            this.scrollViewer  = VisualTreeHelperEx.FindChild <ScrollViewer>(this);
            this.imageProvider = new PdfImageProvider(source, this.parent.TotalPages,
                                                      new PageDisplaySettings(this.parent.GetPagesPerRow(), this.parent.ViewType, this.parent.HorizontalMargin, this.parent.Rotation), false, password);

            currentPageIndex = 0;

            if (this.scrollViewer != null)
            {
                this.scrollViewer.Visibility = System.Windows.Visibility.Visible;
            }

            if (this.parent.ZoomType == ZoomType.Fixed)
            {
                this.SetItemsSource();
            }
            else if (this.parent.ZoomType == ZoomType.FitToHeight)
            {
                this.ZoomToHeight();
            }
            else if (this.parent.ZoomType == ZoomType.FitToWidth)
            {
                this.ZoomToWidth();
            }
        }
コード例 #7
0
 public static bool NeedsPassword(IPdfSource source)
 {
     using (var stream = new PdfFileStream(source))
     {
         return(NeedsPassword(stream.Document));
     }
 }
コード例 #8
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);
            }
        }
コード例 #9
0
        public void Load(IPdfSource source, string password = null)
        {
            this.scrollViewer  = this.ChildOfType <ScrollViewer>();
            this.imageProvider = new PdfImageProvider(source, this.parent.TotalPages,
                                                      new PageDisplaySettings(this.parent.GetPagesPerRow(), this.parent.ViewType, this.parent.HorizontalMargin, this.parent.Rotation), false, password);

            if (scrollViewer != null)
            {
                scrollViewer.RemoveHandler(UIElement.MouseWheelEvent, new MouseWheelEventHandler(this.OnScrollWheel));
                scrollViewer.AddHandler(UIElement.MouseWheelEvent, new MouseWheelEventHandler(this.OnScrollWheel), true);
            }

            this.CurrentPageIndex = 0;

            if (this.scrollViewer != null)
            {
                this.scrollViewer.Visibility = System.Windows.Visibility.Visible;
            }

            if (this.parent.ZoomType == ZoomType.Fixed)
            {
                this.SetItemsSource();
            }
            else if (this.parent.ZoomType == ZoomType.FitToHeight)
            {
                this.ZoomToHeight();
            }
            else if (this.parent.ZoomType == ZoomType.FitToWidth)
            {
                this.ZoomToWidth();
            }
        }
コード例 #10
0
 public PdfImageProvider(IPdfSource pdfSource, int totalPages, PageDisplaySettings settings, bool preFetch = true, string password = null)
 {
     this.pdfSource  = pdfSource;
     this.totalPages = totalPages;
     this.Settings   = settings;
     this.preFetch   = preFetch;           // preFetch is relevant for continuous page display
     this.password   = password;
 }
コード例 #11
0
        private void LoadPdf(IPdfSource source, string password)
        {
            var pageBounds = MuPdfWrapper.GetPageBounds(source, this.Rotation, password);

            this.pageRowBounds = CalculatePageRowBounds(pageBounds, this.ViewType);
            this.TotalPages    = pageBounds.Length;
            this.innerPanel.Load(source, password);
        }
コード例 #12
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
            }
        }
コード例 #13
0
        public void Load(IPdfSource source, string password = null)
        {
            this.scrollViewer = VisualTreeHelperEx.FindChild<ScrollViewer>(this);
            this.imageProvider = new PdfImageProvider(source, this.parent.TotalPages,
                new PageDisplaySettings(this.parent.GetPagesPerRow(), this.parent.ViewType, this.parent.HorizontalMargin, this.parent.Rotation), false, password);

            currentPageIndex = 0;

            if (this.parent.ZoomType == ZoomType.Fixed)
                this.SetItemsSource();
            else if (this.parent.ZoomType == ZoomType.FitToHeight)
                this.ZoomToHeight();
            else if (this.parent.ZoomType == ZoomType.FitToWidth)
                this.ZoomToWidth();
        }
コード例 #14
0
ファイル: PDFToPNG.cs プロジェクト: Kopanskyi/Paint-PDF
        public static void Convert()
        {
            if (!File.Exists(inputFile))
            {
                MessageBox.Show("File not found!");
                return;
            }

            pdfSource  = new FileSource(inputFile);
            PagesCount = MuPdfWrapper.CountPages(pdfSource);

            Thread thread = new Thread(convertPdfToImg);

            thread.Start();
        }
コード例 #15
0
ファイル: MuPdfWrapper.cs プロジェクト: ZWN476867589/moonpdf
		/// <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;
			}
		}
コード例 #16
0
ファイル: MoonPdfPanel.xaml.cs プロジェクト: a136131816/gsof
        private void LoadPdf(IPdfSource source, string password)
        {
            var pageBounds = MuPdfWrapper.GetPageBounds(source, this.Rotation, password);

            this.pageRowBounds = CalculatePageRowBounds(pageBounds, this.ViewType);
            this.TotalPages    = pageBounds.Length;
            var ue = this.innerPanel as FrameworkElement;

            if (ue != null &&
                !ue.IsLoaded)
            {
                ue.UpdateLayout();
            }
            this.innerPanel.Load(source, password);
        }
コード例 #17
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);
            }
        }
コード例 #18
0
ファイル: MuPdfWrapper.cs プロジェクト: csutcliff/moonpdf
 public PdfFileStream(IPdfSource source)
 {
     if (source is FileSource fs)
     {
         Context  = NativeMethods.NewContext();                        // Creates the context
         Stream   = NativeMethods.OpenFile(Context, fs.Filename);      // opens file as a stream
         Document = NativeMethods.OpenDocumentStream(Context, Stream); // opens the document
     }
     else if (source is MemorySource ms)
     {
         Context = NativeMethods.NewContext(); // Creates the context
         GCHandle pinnedArray = GCHandle.Alloc(ms.Bytes, GCHandleType.Pinned);
         IntPtr   pointer     = pinnedArray.AddrOfPinnedObject();
         Stream   = NativeMethods.OpenStream(Context, pointer, ms.Bytes.Length); // opens file as a stream
         Document = NativeMethods.OpenDocumentStream(Context, Stream);           // opens the document
         pinnedArray.Free();
     }
 }
コード例 #19
0
ファイル: MuPdfWrapper.cs プロジェクト: aragoubi/Project
        /// <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));
            }
        }
コード例 #20
0
        public void Load(IPdfSource source, string password = null)
        {
            this.virtualPanel = VisualTreeHelperEx.FindChild<CustomVirtualizingPanel>(this);
            this.scrollViewer = VisualTreeHelperEx.FindChild<ScrollViewer>(this);
            this.virtualPanel.PageRowBounds = this.parent.PageRowBounds.Select(f => f.SizeIncludingOffset).ToArray();
            this.imageProvider = new PdfImageProvider(source, this.parent.TotalPages,
                                        new PageDisplaySettings(this.parent.GetPagesPerRow(), this.parent.ViewType, this.parent.HorizontalMargin, this.parent.Rotation),
                                        password: password);

            if (this.parent.ZoomType == ZoomType.Fixed)
                this.CreateNewItemsSource();
            else if (this.parent.ZoomType == ZoomType.FitToHeight)
                this.ZoomToHeight();
            else if (this.parent.ZoomType == ZoomType.FitToWidth)
                this.ZoomToWidth();

            if (this.scrollViewer != null)
                this.scrollViewer.ScrollToTop();
        }
コード例 #21
0
ファイル: MuPdfWrapper.cs プロジェクト: aragoubi/Project
        /// <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());
            }
        }
コード例 #22
0
ファイル: MuPdfWrapper.cs プロジェクト: MailbirdInc/moonpdf
 public PdfFileStream(IPdfSource source)
 {
     if (source is FileSource)
     {
         var fs = (FileSource)source;
         Context = NativeMethods.NewContext(IntPtr.Zero, IntPtr.Zero, FZ_STORE_DEFAULT); // Creates the context
         NativeMethods.InitializeContext(Context);
         Stream   = NativeMethods.OpenFile(Context, fs.Filename);                        // opens file as a stream
         Document = NativeMethods.OpenDocumentStream(Context, ".pdf", Stream);           // opens the document
     }
     else if (source is MemorySource)
     {
         var ms = (MemorySource)source;
         Context = NativeMethods.NewContext(IntPtr.Zero, IntPtr.Zero, FZ_STORE_DEFAULT); // Creates the context
         NativeMethods.InitializeContext(Context);
         GCHandle pinnedArray = GCHandle.Alloc(ms.Bytes, GCHandleType.Pinned);
         IntPtr   pointer     = pinnedArray.AddrOfPinnedObject();
         Stream   = NativeMethods.OpenStream(Context, pointer, ms.Bytes.Length); // opens file as a stream
         Document = NativeMethods.OpenDocumentStream(Context, ".pdf", Stream);   // opens the document
         pinnedArray.Free();
     }
 }
コード例 #23
0
ファイル: MuPdf.cs プロジェクト: a136131816/gsof
        public void Open(IPdfSource p_source, string p_password = null)
        {
            if (p_source == null)
            {
                throw new ArgumentNullException(nameof(p_source));
            }

            _password = p_password;

            using (var stream = p_source.Open())
            {
                var buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);

                var data = buffer.ToIntPtr();

                _context  = NativeMethods.NewContext(IntPtr.Zero, IntPtr.Zero, FZ_STORE_DEFAULT);
                _stream   = NativeMethods.OpenStream(_context, data, buffer.Length);
                _document = NativeMethods.OpenDocumentStream(_context, ".pdf", _stream);

                IsEncrypt = NativeMethods.NeedsPassword(_document) != 0;

                var isAuthenticate = true;

                if (IsEncrypt && !string.IsNullOrEmpty(_password))
                {
                    isAuthenticate = AuthenticatePassword(_password);
                }

                if (isAuthenticate)
                {
                    Pages = NativeMethods.CountPages(_document);
                }

                data.Free();
            }
        }
コード例 #24
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);
            }
        }
コード例 #25
0
ファイル: MuPdfWrapper.cs プロジェクト: nathanashton/Bookie
        /// <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;
            }
        }
コード例 #26
0
ファイル: MuPdfWrapper.cs プロジェクト: aragoubi/Nine
        /// <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();
            }
        }
コード例 #27
0
ファイル: MuPdfWrapper.cs プロジェクト: aragoubi/Nine
        /// <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);
            }
        }
コード例 #28
0
 private void LoadPdf(IPdfSource source, string password)
 {
     var pageBounds = MuPdfWrapper.GetPageBounds(source, this.Rotation, password);
     this.pageRowBounds = CalculatePageRowBounds(pageBounds, this.ViewType);
     this.TotalPages = pageBounds.Length;
     this.innerPanel.Load(source, password);
 }
コード例 #29
0
        public void Open(IPdfSource source, string password = null)
        {
            var pw = password;

            if (this.PasswordRequired != null && MuPdfWrapper.NeedsPassword(source) && pw == null)
            {
                var e = new PasswordRequiredEventArgs();
                this.PasswordRequired(this, e);

                if (e.Cancel)
                    return;

                pw = e.Password;
            }

            this.LoadPdf(source, pw);
            this.CurrentSource = source;
            this.CurrentPassword = pw;

            if (this.PdfLoaded != null)
                this.PdfLoaded(this, EventArgs.Empty);
        }
コード例 #30
0
ファイル: MuPdfWrapper.cs プロジェクト: nathanashton/Bookie
            public PdfFileStream(IPdfSource source)
            {
                if (source is FileSource)
                {
                    var fs = (FileSource) source;
                    Context = NativeMethods.NewContext(IntPtr.Zero, IntPtr.Zero, FZ_STORE_DEFAULT);
                    // Creates the context

                    //TODO Following fails if file doesnt exist
                    Stream = NativeMethods.OpenFile(Context, fs.Filename); // opens file as a stream
                    Document = NativeMethods.OpenDocumentStream(Context, ".pdf", Stream); // opens the document
                }
                else if (source is MemorySource)
                {
                    var ms = (MemorySource) source;
                    Context = NativeMethods.NewContext(IntPtr.Zero, IntPtr.Zero, FZ_STORE_DEFAULT);
                    // Creates the context
                    var pinnedArray = GCHandle.Alloc(ms.Bytes, GCHandleType.Pinned);
                    var pointer = pinnedArray.AddrOfPinnedObject();
                    Stream = NativeMethods.OpenStream(Context, pointer, ms.Bytes.Length); // opens file as a stream
                    Document = NativeMethods.OpenDocumentStream(Context, ".pdf", Stream); // opens the document
                    pinnedArray.Free();
                }
            }
コード例 #31
0
ファイル: MuPdfWrapper.cs プロジェクト: nathanashton/Bookie
 public static bool NeedsPassword(IPdfSource source)
 {
     using (var stream = new PdfFileStream(source))
     {
         return NeedsPassword(stream.Document);
     }
 }
コード例 #32
0
ファイル: MuPdfWrapper.cs プロジェクト: nathanashton/Bookie
        /// <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
            }
        }