protected virtual void Dispose(bool disposing) { if (!_disposed) { if (_form != IntPtr.Zero) { NativePdfiumMethods.FORM_DoDocumentAAction(_form, NativePdfiumMethods.FPDFDOC_AACTION.WC); NativePdfiumMethods.FPDFDOC_ExitFormFillEnviroument(_form); _form = IntPtr.Zero; } if (_document != IntPtr.Zero) { NativePdfiumMethods.FPDF_CloseDocument(_document); _document = IntPtr.Zero; } if (_formCallbacksHandle.IsAllocated) { _formCallbacksHandle.Free(); } _disposed = true; } }
private void Dispose(bool disposing) { if (!_disposed) { NativePdfiumMethods.FPDF_Release(); _disposed = true; } }
private uint GetBookmarkPageIndex(IntPtr bookmark) { IntPtr dest = NativePdfiumMethods.FPDF_BookmarkGetDest(_document, bookmark); if (dest != IntPtr.Zero) { return(NativePdfiumMethods.FPDFDest_GetPageIndex(_document, dest)); } return(0); }
private string GetBookmarkTitle(IntPtr bookmark) { uint length = NativePdfiumMethods.FPDF_BookmarkGetTitle(bookmark, null, 0); byte[] buffer = new byte[length]; NativePdfiumMethods.FPDF_BookmarkGetTitle(bookmark, buffer, length); string result = Encoding.Unicode.GetString(buffer); return(result.Substring(0, result.Length - 1)); }
public void Dispose() { if (!_disposed) { NativePdfiumMethods.FORM_DoPageAAction(Page, _form, NativePdfiumMethods.FPDFPAGE_AACTION.CLOSE); NativePdfiumMethods.FORM_OnBeforeClosePage(Page, _form); NativePdfiumMethods.FPDFText_ClosePage(TextPage); NativePdfiumMethods.FPDF_ClosePage(Page); _disposed = true; } }
public PageData(IntPtr document, IntPtr form, int pageNumber) { _form = form; Page = NativePdfiumMethods.FPDF_LoadPage(document, pageNumber); TextPage = NativePdfiumMethods.FPDFText_LoadPage(Page); NativePdfiumMethods.FORM_OnAfterLoadPage(Page, form); NativePdfiumMethods.FORM_DoPageAAction(Page, form, NativePdfiumMethods.FPDFPAGE_AACTION.OPEN); Width = NativePdfiumMethods.FPDF_GetPageWidth(Page); Height = NativePdfiumMethods.FPDF_GetPageHeight(Page); }
public PdfBufferFile(byte[] buffer, int length) { if (buffer == null) { throw new ArgumentNullException("buffer"); } _buffer = buffer; _length = length; LoadDocument(NativePdfiumMethods.FPDF_LoadMemDocument(_buffer, length, null)); }
private void LoadBookmarks(PdfBookmarkCollection bookmarks, IntPtr bookmark) { if (bookmark == IntPtr.Zero) { return; } bookmarks.Add(LoadBookmark(bookmark)); while ((bookmark = NativePdfiumMethods.FPDF_BookmarkGetNextSibling(_document, bookmark)) != IntPtr.Zero) { bookmarks.Add(LoadBookmark(bookmark)); } }
public PdfMatches Search(string text, bool matchCase, bool wholeWord, int startPage, int endPage) { var matches = new List <PdfMatch>(); for (int page = startPage; page <= endPage; page++) { using (var pageData = new PageData(_document, _form, page)) { NativePdfiumMethods.FPDF_SEARCH_FLAGS flags = 0; if (matchCase) { flags |= NativePdfiumMethods.FPDF_SEARCH_FLAGS.FPDF_MATCHCASE; } if (wholeWord) { flags |= NativePdfiumMethods.FPDF_SEARCH_FLAGS.FPDF_MATCHWHOLEWORD; } var handle = NativePdfiumMethods.FPDFText_FindStart(pageData.TextPage, FPDFEncoding.GetBytes(text), flags, 0); try { while (NativePdfiumMethods.FPDFText_FindNext(handle)) { int index = NativePdfiumMethods.FPDFText_GetSchResultIndex(handle); int matchLength = NativePdfiumMethods.FPDFText_GetSchCount(handle); var result = new byte[(matchLength + 1) * 2]; NativePdfiumMethods.FPDFText_GetText(pageData.TextPage, index, matchLength, result); string match = FPDFEncoding.GetString(result, 0, matchLength * 2); double left, right, bottom, top; NativePdfiumMethods.FPDFText_GetCharBox(pageData.TextPage, index, out left, out right, out bottom, out top); matches.Add(new PdfMatch( new PointF((float)left, (float)top), match, page )); } } finally { NativePdfiumMethods.FPDFText_FindClose(handle); } } } return(new PdfMatches(startPage, endPage, matches)); }
public bool RenderPDFPageToDC(int pageNumber, IntPtr dc, int dpiX, int dpiY, int boundsOriginX, int boundsOriginY, int boundsWidth, int boundsHeight, NativePdfiumMethods.FPDF flags) { if (_disposed) { throw new ObjectDisposedException(GetType().Name); } using (var pageData = new PageData(_document, _form, pageNumber)) { NativePdfiumMethods.FPDF_RenderPage(dc, pageData.Page, boundsOriginX, boundsOriginY, boundsWidth, boundsHeight, 0, flags); } return(true); }
/// <summary> /// Renders a page of the PDF document to an image. /// </summary> /// <param name="page">Number of the page to render.</param> /// <param name="width">Width of the rendered image.</param> /// <param name="height">Height of the rendered image.</param> /// <param name="dpiX">Horizontal DPI.</param> /// <param name="dpiY">Vertical DPI.</param> /// <param name="flags">Flags used to influence the rendering.</param> /// <returns>The rendered image.</returns> public Image Render(int page, int width, int height, float dpiX, float dpiY, PdfRenderFlags flags) { if (_disposed) { throw new ObjectDisposedException(GetType().Name); } var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); bitmap.SetResolution(dpiX, dpiY); var data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bitmap.PixelFormat); try { var handle = NativePdfiumMethods.FPDFBitmap_CreateEx(width, height, 4, data.Scan0, width * 4); try { uint background = (flags & PdfRenderFlags.Transparent) == 0 ? 0xFFFFFFFF : 0x00FFFFFF; NativePdfiumMethods.FPDFBitmap_FillRect(handle, 0, 0, width, height, background); bool success = _file.RenderPDFPageToBitmap( page, handle, (int)dpiX, (int)dpiY, 0, 0, width, height, this.FlagsToFPDFFlags(flags) ); if (!success) { throw new Win32Exception(); } } finally { NativePdfiumMethods.FPDFBitmap_Destroy(handle); } } finally { bitmap.UnlockBits(data); } return(bitmap); }
private PdfBookmark LoadBookmark(IntPtr bookmark) { var result = new PdfBookmark { Title = GetBookmarkTitle(bookmark), PageIndex = (int)GetBookmarkPageIndex(bookmark) }; //Action = NativePdfiumMethods.FPDF_BookmarkGetAction(_bookmark); //if (Action != IntPtr.Zero) // ActionType = NativePdfiumMethods.FPDF_ActionGetType(Action); var child = NativePdfiumMethods.FPDF_BookmarkGetFirstChild(_document, bookmark); if (child != IntPtr.Zero) { LoadBookmarks(result.Children, child); } return(result); }
protected void LoadDocument(IntPtr document) { _document = document; NativePdfiumMethods.FPDF_GetDocPermissions(_document); _formCallbacks = new NativePdfiumMethods.FPDF_FORMFILLINFO(); _formCallbacksHandle = GCHandle.Alloc(_formCallbacks); _formCallbacks.version = 1; _form = NativePdfiumMethods.FPDFDOC_InitFormFillEnvironment(_document, ref _formCallbacks); NativePdfiumMethods.FPDF_SetFormFieldHighlightColor(_form, 0, 0xFFE4DD); NativePdfiumMethods.FPDF_SetFormFieldHighlightAlpha(_form, 100); NativePdfiumMethods.FORM_DoDocumentJSAction(_form); NativePdfiumMethods.FORM_DoDocumentOpenAction(_form); Bookmarks = new PdfBookmarkCollection(); LoadBookmarks(Bookmarks, NativePdfiumMethods.FPDF_BookmarkGetFirstChild(document, IntPtr.Zero)); }
public List <SizeF> GetPDFDocInfo() { if (_disposed) { throw new ObjectDisposedException(GetType().Name); } int pageCount = NativePdfiumMethods.FPDF_GetPageCount(_document); var result = new List <SizeF>(pageCount); for (int i = 0; i < pageCount; i++) { double height; double width; NativePdfiumMethods.FPDF_GetPageSizeByIndex(_document, i, out width, out height); result.Add(new SizeF((float)width, (float)height)); } return(result); }
protected PdfFileHandleFile(SafeHandle handle, int length) { if (handle == null) { throw new ArgumentNullException("handle"); } _mappedHandle = NativeMethods.CreateFileMapping(handle, IntPtr.Zero, NativeMethods.FileMapProtection.PageReadonly, 0, (uint)length, null); if (_mappedHandle.IsInvalid) { throw new Win32Exception(); } _buffer = NativeMethods.MapViewOfFile(_mappedHandle, NativeMethods.FileMapAccess.FileMapRead, 0, 0, (uint)length); if (_buffer.IsInvalid) { throw new Win32Exception(); } LoadDocument(NativePdfiumMethods.FPDF_LoadMemDocument(_buffer, length, null)); }
public byte[] GetPDFPage(int pageNumber) { if (_disposed) { throw new ObjectDisposedException(GetType().Name); } IntPtr newDocument = NativePdfiumMethods.FPDF_CreateNewDocument(); var bl = NativePdfiumMethods.FPDF_ImportPages(newDocument, _document, pageNumber.ToString(), 0); int pageCount = NativePdfiumMethods.FPDF_GetPageCount(newDocument); using (MemoryStream ms = new MemoryStream()) { FPDF_FILEWRITE saveData = new FPDF_FILEWRITE(); saveData.WriteBlock = (WriteBlockCallback)((pThis, buffer, buflen) => { ms.Write(buffer, 0, buffer.Length); return(true); }); try { NativePdfiumMethods.FPDF_SaveAsCopy(newDocument, saveData, 1); } finally { GC.KeepAlive((object)saveData); } NativePdfiumMethods.FPDF_CloseDocument(newDocument); return(ms.ToArray()); } }
private PdfLibrary() { NativePdfiumMethods.FPDF_AddRef(); }
public PdfPageLinks GetPageLinks(int pageNumber, Size pageSize) { if (_disposed) { throw new ObjectDisposedException(GetType().Name); } var links = new List <PdfPageLink>(); using (var pageData = new PageData(_document, _form, pageNumber)) { int link = 0; IntPtr annotation; while (NativePdfiumMethods.FPDFLink_Enumerate(pageData.Page, ref link, out annotation)) { var destination = NativePdfiumMethods.FPDFLink_GetDest(_document, annotation); int? target = null; string uri = null; if (destination != IntPtr.Zero) { target = (int)NativePdfiumMethods.FPDFDest_GetPageIndex(_document, destination); } var action = NativePdfiumMethods.FPDFLink_GetAction(annotation); if (action != IntPtr.Zero) { const uint length = 1024; var sb = new StringBuilder(1024); NativePdfiumMethods.FPDFAction_GetURIPath(_document, action, sb, length); uri = sb.ToString(); } var rect = new NativePdfiumMethods.FS_RECTF(); if (NativePdfiumMethods.FPDFLink_GetAnnotRect(annotation, rect) && (target.HasValue || uri != null)) { int deviceX1; int deviceY1; int deviceX2; int deviceY2; NativePdfiumMethods.FPDF_PageToDevice( pageData.Page, 0, 0, pageSize.Width, pageSize.Height, 0, rect.left, rect.top, out deviceX1, out deviceY1 ); NativePdfiumMethods.FPDF_PageToDevice( pageData.Page, 0, 0, pageSize.Width, pageSize.Height, 0, rect.right, rect.bottom, out deviceX2, out deviceY2 ); links.Add(new PdfPageLink( new Rectangle(deviceX1, deviceY1, deviceX2 - deviceX1, deviceY2 - deviceY1), target, uri )); } } } return(new PdfPageLinks(links)); }