Example #1
0
        protected void LoadDocument(IntPtr doc)
        {
            _document = doc;

            PdfiumLibrary.FPDF_GetDocPermissions(_document);

            _formCallbacks       = new API.FPDF_FORMFILLINFO();
            _formCallbacksHandle = GCHandle.Alloc(_formCallbacks, GCHandleType.Pinned);

            for (int i = 1; i <= 2; i++)
            {
                _formCallbacks.version = i;

                _form = PdfiumLibrary.FPDFDOC_InitFormFillEnvironment(_document, _formCallbacks);
                if (_form != IntPtr.Zero)
                {
                    break;
                }
            }

            PdfiumLibrary.FPDF_SetFormFieldHighlightColor(_form, 0, 0xFFE4DD);
            PdfiumLibrary.FPDF_SetFormFieldHighlightAlpha(_form, 100);

            PdfiumLibrary.FORM_DoDocumentJSAction(_form);
            PdfiumLibrary.FORM_DoDocumentOpenAction(_form);

            Bookmarks = new PdfBookmarkCollection();

            LoadBookmarks(Bookmarks, PdfiumLibrary.FPDFBookmark_GetFirstChild(_document, IntPtr.Zero));
        }
Example #2
0
        protected void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            StreamDictionary.Remove(_streamId);

            if (_form != IntPtr.Zero)
            {
                PdfiumLibrary.FORM_DoDocumentAAction(_form, FPDFDOC_AACTION.WC);
                PdfiumLibrary.FPDFDOC_ExitFormFillEnvironment(_form);
                _form = IntPtr.Zero;
            }

            if (_document != IntPtr.Zero)
            {
                PdfiumLibrary.FPDF_CloseDocument(_document);
                _document = IntPtr.Zero;
            }

            if (_formCallbacksHandle.IsAllocated)
            {
                _formCallbacksHandle.Free();
            }

            if (_stream != null)
            {
                _stream.Dispose();
                _stream = null;
            }

            _disposed = true;
        }
Example #3
0
        public SizeF GetPageSize(int pageIndex)
        {
            double width, height;

            PdfiumLibrary.FPDF_GetPageSizeByIndex(_document, pageIndex, out width, out height);

            return(new SizeF((float)width, (float)height));
        }
Example #4
0
        public bool Save(Stream stream)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            return(PdfiumLibrary.FPDF_SaveAsCopy(_document, stream, FPDF_SAVE_FLAGS.FPDF_NO_INCREMENTAL));;
        }
Example #5
0
 public static void Init()
 {
     lock (_mutex)
     {
         if (_library == null)
         {
             _library = new PdfiumLibrary();
         }
     }
 }
Example #6
0
        private uint GetBookmarkPageIndex(IntPtr bookmark)
        {
            var dest = PdfiumLibrary.FPDFBookmark_GetDest(_document, bookmark);

            if (dest != IntPtr.Zero)
            {
                return(PdfiumLibrary.FPDFDest_GetPageIndex(_document, dest));
            }

            return(0);
        }
Example #7
0
        public RotateType GetPageRotation(int pageIndex)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            using (var pdfData = new PdfData(_document, _form, pageIndex))
            {
                return(PdfiumLibrary.FPDFPage_GetRotation(pdfData.Page));
            }
        }
Example #8
0
        public PdfData(IntPtr document, IntPtr form, int pageIndex)
        {
            _form = form;

            Page     = PdfiumLibrary.FPDF_LoadPage(document, pageIndex);
            TextPage = PdfiumLibrary.FPDFText_LoadPage(Page);
            PdfiumLibrary.FORM_OnAfterLoadPage(Page, _form);
            PdfiumLibrary.FORM_DoPageAAction(Page, _form, FPDFPAGE_AACTION.OPEN);

            Width  = PdfiumLibrary.FPDF_GetPageWidth(Page);
            Height = PdfiumLibrary.FPDF_GetPageHeight(Page);
        }
Example #9
0
        public bool GetPageMediaBox(int pageIndex, out float left, out float bottom, out float right, out float top)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            using (var pdfData = new PdfData(_document, _form, pageIndex))
            {
                return(PdfiumLibrary.FPDFPage_GetMediaBox(pdfData.Page, out left, out bottom, out right, out top));
            }
        }
Example #10
0
        private void LoadBookmarks(PdfBookmarkCollection bookmarks, IntPtr bookmark)
        {
            if (bookmark == IntPtr.Zero)
            {
                return;
            }

            bookmarks.Add(LoadBookmark(bookmark));
            while ((bookmark = PdfiumLibrary.FPDFBookmark_GetNextSibling(_document, bookmark)) != IntPtr.Zero)
            {
                bookmarks.Add(LoadBookmark(bookmark));
            }
        }
Example #11
0
        public void Dispose()
        {
            if (_disposed)
            {
                return;
            }

            PdfiumLibrary.FORM_DoPageAAction(Page, _form, FPDFPAGE_AACTION.CLOSE);
            PdfiumLibrary.FORM_OnBeforeClosePage(Page, _form);
            PdfiumLibrary.FPDFText_ClosePage(TextPage);
            PdfiumLibrary.FPDF_ClosePage(Page);

            _disposed = true;
        }
Example #12
0
        public bool SetPageMediaBox(int pageIndex, float left, float bottom, float right, float top)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            using (var pdfData = new PdfData(_document, _form, pageIndex))
            {
                PdfiumLibrary.FPDFPage_SetMediaBox(pdfData.Page, left, bottom, right, top);
            }

            return(true);
        }
Example #13
0
        public bool RenderPageToDC(int pageIndex, IntPtr dc, int dpiX, int dpiY, int boundsOriginX, int boundsOriginY, int boundsWidth, int boundsHeight, FPDF flags)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            using (var pdfData = new PdfData(_document, _form, pageIndex))
            {
                PdfiumLibrary.FPDF_RenderPage(dc, pdfData.Page, boundsOriginX, boundsOriginY, boundsWidth, boundsHeight, 0, flags);
            }

            return(true);
        }
Example #14
0
        public bool SetPageRotation(int pageIndex, int rotate)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            using (var pdfData = new PdfData(_document, _form, pageIndex))
            {
                PdfiumLibrary.FPDFPage_SetRotation(pdfData.Page, rotate);
            }

            return(true);
        }
Example #15
0
        private string GetBookmarkTitle(IntPtr bookmark)
        {
            uint length = PdfiumLibrary.FPDFBookmark_GetTitle(bookmark, null, 0);

            byte[] bytes = new byte[length];
            PdfiumLibrary.FPDFBookmark_GetTitle(bookmark, bytes, length);

            string title = Encoding.Unicode.GetString(bytes);

            if (title.Length > 0 && title[title.Length - 1] == 0)
            {
                title = title.Substring(0, title.Length - 1);
            }

            return(title);
        }
Example #16
0
        private PdfBookmark LoadBookmark(IntPtr bookmark)
        {
            var pdfBookmark = new PdfBookmark
            {
                Title     = GetBookmarkTitle(bookmark),
                PageIndex = (int)GetBookmarkPageIndex(bookmark)
            };

            var child = PdfiumLibrary.FPDFBookmark_GetFirstChild(_document, bookmark);

            if (child != IntPtr.Zero)
            {
                LoadBookmarks(pdfBookmark.Children, child);
            }

            return(pdfBookmark);
        }
Example #17
0
        public PdfObject(Stream pdfStream, string password)
        {
            if (pdfStream == null)
            {
                throw new ArgumentNullException(nameof(pdfStream));
            }

            PdfiumLibrary.Init();

            _stream   = pdfStream;
            _streamId = StreamDictionary.Add(pdfStream);

            var document = PdfiumLibrary.FPDF_LoadCustomDocument(pdfStream, password, _streamId);

            if (document == IntPtr.Zero)
            {
                throw new PdfException(PdfiumLibrary.FPDF_GetLastError());
            }

            LoadDocument(document);
        }
Example #18
0
        public List <SizeF> GetPageSizes(bool isActualSize)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            int pageCount = PdfiumLibrary.FPDF_GetPageCount(_document);
            var pageSizes = new List <SizeF>(pageCount);

            for (int i = 0; i < pageCount; i++)
            {
                var rotate = GetPageRotation(i);
                if (rotate != RotateType.Rotate0 && isActualSize)
                {
                    SetPageRotation(i, (int)RotateType.Rotate0);
                }

                bool result = GetPageCropBox(i, out float left, out float bottom, out float right, out float top);
                if (result && isActualSize)
                {
                    SetPageCropBox(i, 0, 0, 0, 0);
                }

                pageSizes.Add(GetPageSize(i));

                if (rotate != RotateType.Rotate0 && isActualSize)
                {
                    SetPageRotation(i, (int)rotate);
                }

                if (result && isActualSize)
                {
                    SetPageCropBox(i, left, bottom, right, top);
                }
            }

            return(pageSizes);
        }
Example #19
0
        public bool RenderPageToBitmap(int pageIndex, IntPtr bitmapHandle, int dpiX, int dpiY, int boundsOriginX, int boundsOriginY, int boundsWidth, int boundsHeight, int rotate, FPDF flags, bool renderFormFill)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            using (var pdfData = new PdfData(_document, _form, pageIndex))
            {
                if (renderFormFill)
                {
                    flags &= ~FPDF.ANNOT;
                }

                PdfiumLibrary.FPDF_RenderPageBitmap(bitmapHandle, pdfData.Page, boundsOriginX, boundsOriginY, boundsWidth, boundsHeight, rotate, flags);

                if (renderFormFill)
                {
                    PdfiumLibrary.FPDF_FFLDraw(_form, bitmapHandle, pdfData.Page, boundsOriginX, boundsOriginY, boundsWidth, boundsHeight, rotate, flags);
                }
            }

            return(true);
        }