/// <summary> /// Start/Continue progressive rendering for specified page /// </summary> /// <param name="page">Pdf page object</param> /// <param name="pageRect">Actual page's rectangle. </param> /// <param name="pageRotate">Page orientation: 0 (normal), 1 (rotated 90 degrees clockwise), 2 (rotated 180 degrees), 3 (rotated 90 degrees counter-clockwise).</param> /// <param name="renderFlags">0 for normal display, or combination of flags defined above.</param> /// <param name="useProgressiveRender">True for use progressive render</param> /// <returns>Null if page still painting, PdfBitmap object if page successfully rendered.</returns> internal bool RenderPage(PdfPage page, Rectangle pageRect, PageRotate pageRotate, RenderFlags renderFlags, bool useProgressiveRender) { if (!this.ContainsKey(page)) { ProcessNew(page); //Add new page into collection if (PaintBackground != null) { PaintBackground(this, new EventArgs <Rectangle>(pageRect)); } } if ((renderFlags & (RenderFlags.FPDF_THUMBNAIL | RenderFlags.FPDF_HQTHUMBNAIL)) != 0) { this[page].status = ProgressiveRenderingStatuses.RenderDone + 4; } else if (!useProgressiveRender) { this[page].status = ProgressiveRenderingStatuses.RenderDone + 3; } PdfBitmap bitmap = this[page].Bitmap; bool ret = ProcessExisting(bitmap ?? CanvasBitmap, page, pageRect, pageRotate, renderFlags); if (bitmap != null) { Pdfium.FPDFBitmap_CompositeBitmap(CanvasBitmap.Handle, pageRect.X, pageRect.Y, pageRect.Width, pageRect.Height, bitmap.Handle, pageRect.X, pageRect.Y, BlendTypes.FXDIB_BLEND_NORMAL); } return(ret); }
private IntPtr InitDocument() { if (!_useDP) { return(_pdfDoc.Handle); } _printHandle = Pdfium.FPDFPRINT_Open( _pdfDoc.Handle, string.Format("{0}-{1}", PrinterSettings.FromPage, PrinterSettings.ToPage), DefaultPageSettings.PaperSize.Width / 100 * 72, DefaultPageSettings.PaperSize.Height / 100 * 72, (int)((double)DefaultPageSettings.PrintableArea.X / 100 * 72), (int)((double)DefaultPageSettings.PrintableArea.Y / 100 * 72), (int)((double)DefaultPageSettings.PrintableArea.Width / 100 * 72), (int)((double)DefaultPageSettings.PrintableArea.Height / 100 * 72), PrintScallingMode.PrintableArea); if (_printHandle == IntPtr.Zero) { return(IntPtr.Zero); } _docForPrint = Pdfium.FPDFPRINT_GetDocument(_printHandle); if (_docForPrint == IntPtr.Zero) { return(IntPtr.Zero); } return(_docForPrint); }
static void Main(string[] args) { Pdfium pdf = new Pdfium(); TestParams param = LoadJson(); //string file = path + fileName; string file = param.pathToFile; Console.WriteLine("\nOpen PDF file: " + file); pdf.LoadFile(file); Console.WriteLine("\nNumber of Pages:" + pdf.PageCount().ToString()); var info = pdf.GetInformation(); Console.WriteLine("\nCreator: " + info.Creator); Console.WriteLine("\nTitle: " + info.Title); Console.WriteLine("\nAuthor: " + info.Author); Console.WriteLine("\nSubject: " + info.Subject); Console.WriteLine("\nKeywords: " + info.Keywords); Console.WriteLine("\nProducer: " + info.Producer); Console.WriteLine("\nCreationDate: " + info.CreationDate); Console.WriteLine("\nModDate: " + info.ModificationDate); }
private void CalcSize(IntPtr currentPage, double dpiX, double dpiY, RectangleF printableArea, bool isLandscape, out double width, out double height, out double x, out double y) { x = y = 0; width = Pdfium.FPDF_GetPageWidth(currentPage) / 72 * dpiX; height = Pdfium.FPDF_GetPageHeight(currentPage) / 72 * dpiY; if (_useDP) { return; } //Calculate the size of the printable area in pixels var fitSize = new SizeF( (float)dpiX * printableArea.Width / 100.0f, (float)dpiY * printableArea.Height / 100.0f ); var pageSize = new SizeF( (float)width, (float)height ); var rot = Pdfium.FPDFPage_GetRotation(currentPage); bool isRotated = (rot == PageRotate.Rotate270 || rot == PageRotate.Rotate90) || (width > height); if (AutoRotate && isRotated) { fitSize = new SizeF(fitSize.Height, fitSize.Width); } else if (!AutoRotate && isLandscape) { fitSize = new SizeF(fitSize.Height, fitSize.Width); } switch (PrintSizeMode) { case PrintSizeMode.Fit: var sz = GetRenderSize(pageSize, fitSize); width = sz.Width; height = sz.Height; break; case PrintSizeMode.CustomScale: width *= (double)Scale / 100.0; height *= (double)Scale / 100.0; break; case PrintSizeMode.ActualSize: default: break; } if (AutoCenter) { x = (fitSize.Width - width) / 2; y = (fitSize.Height - height) / 2; } }
private void _searchTimer_Tick(object sender, EventArgs e) { if (_searchPageIndex < 0) { return; } var doc = PdfViewer.Document; int cnt = doc.Pages.Count; if (_searchPageIndex >= cnt) { _searchTimer.Stop(); return; } IntPtr page = Pdfium.FPDF_LoadPage(doc.Handle, _searchPageIndex); if (page == IntPtr.Zero) { _searchTimer.Stop(); return; } IntPtr text = Pdfium.FPDFText_LoadPage(page); if (text == IntPtr.Zero) { _searchTimer.Stop(); return; } var sh = Pdfium.FPDFText_FindStart(text, _searchText, _searchFlags, 0); if (sh == IntPtr.Zero) { _searchTimer.Stop(); return; } while (Pdfium.FPDFText_FindNext(sh)) { int idx = Pdfium.FPDFText_GetSchResultIndex(sh); int len = Pdfium.FPDFText_GetSchCount(sh); var ft = new PdfSearch.FoundText() { CharIndex = idx, CharsCount = len, PageIndex = _searchPageIndex }; _foundText.Add(ft); _forHighlight.Add(ft); } Pdfium.FPDFText_FindClose(sh); Pdfium.FPDFText_ClosePage(text); Pdfium.FPDF_ClosePage(page); UpdateResults(); _searchPageIndex++; }
/// <summary> /// Raises the System.Drawing.Printing.PrintDocument.EndPrint event. It is called /// when the last page of the document has printed. /// </summary> /// <param name="e">A System.Drawing.Printing.PrintEventArgs that contains the event data.</param> protected override void OnEndPrint(PrintEventArgs e) { base.OnEndPrint(e); if (_printHandle != IntPtr.Zero) { Pdfium.FPDFPRINT_Close(_printHandle); } _printHandle = IntPtr.Zero; }
public void SetupPDF() { pdf = new Pdfium(); TestParams filePath = LoadJson(); string file = filePath.pathToFile; Console.WriteLine("\nOpen PDF file: " + file); pdf.LoadFile(file); }
private PageRotate CalcRotation(IntPtr currentPage, bool isLandscape, ref double width, ref double height) { var rot = Pdfium.FPDFPage_GetRotation(currentPage); bool isRotated = (rot == PageRotate.Rotate270 || rot == PageRotate.Rotate90) || (width > height); if (AutoRotate && isRotated != isLandscape) { double tmp = width; width = height; height = tmp; return(rot == PageRotate.Rotate270 ? PageRotate.Rotate90 : PageRotate.Rotate270); } return(PageRotate.Normal); }
static private void InsertIntoDictionary(PdfDocument doc, PdfPage page, PdfImageObject image) { //Get page dictionary, list of indirect objects and original page content var pageDict = page.Dictionary; var list = PdfIndirectList.FromPdfDocument(doc); //Convert contents to array. PdfTypeArray array = ConvertContentsToArray(pageDict["Contents"], list, pageDict); //Get stream of image. IntPtr streamHandle = Pdfium.FPDFImageObj_GenerateStream(image.Handle, page.Handle); var stream = PdfTypeStream.Create(streamHandle); //Add image's stream into list of indirect objects and then add it to array. int num = list.Add(stream); array.AddIndirect(list, num); }
/// <summary> /// Raises the System.Drawing.Printing.PrintDocument.QueryPageSettings event. It /// is called immediately before each System.Drawing.Printing.PrintDocument.PrintPage event. /// </summary> /// <param name="e">A System.Drawing.Printing.QueryPageSettingsEventArgs that contains the event data.</param> protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e) { if (AutoRotate) { IntPtr currentPage = Pdfium.FPDF_StartLoadPage(_docForPrint, _pageForPrint); if (currentPage == IntPtr.Zero) { e.Cancel = true; return; } double width = Pdfium.FPDF_GetPageWidth(currentPage); double height = Pdfium.FPDF_GetPageHeight(currentPage); var rotation = Pdfium.FPDFPage_GetRotation(currentPage); bool isRotated = (/*rotation == PageRotate.Rotate270 || rotation == PageRotate.Rotate90 ||*/ width > height); e.PageSettings.Landscape = isRotated; if (currentPage != IntPtr.Zero) { Pdfium.FPDF_ClosePage(currentPage); } } base.OnQueryPageSettings(e); }
/// <summary> /// Raises the System.Drawing.Printing.PrintDocument.PrintPage event. It is called /// before a page prints. /// </summary> /// <param name="e"> A System.Drawing.Printing.PrintPageEventArgs that contains the event data.</param> protected override void OnPrintPage(PrintPageEventArgs e) { base.OnPrintPage(e); IntPtr hdc = IntPtr.Zero; IntPtr currentPage = IntPtr.Zero; try { if (e.Cancel) { return; } currentPage = Pdfium.FPDF_LoadPage(_docForPrint, _pageForPrint); if (currentPage == IntPtr.Zero) { e.Cancel = true; return; } double dpiX = e.Graphics.DpiX; double dpiY = e.Graphics.DpiY; double width, height; double x, y; CalcSize(currentPage, dpiX, dpiY, e.PageSettings.PrintableArea, e.PageSettings.Landscape, out width, out height, out x, out y); PageRotate rotation = CalcRotation(currentPage, e.PageSettings.Landscape, ref width, ref height, ref x, ref y); using (var page = PdfPage.FromHandle(_pdfDoc, currentPage, _pageForPrint)) { OnBeforeRenderPage(page, width, height, rotation); } hdc = e.Graphics.GetHdc(); Pdfium.SetWorldTransform(hdc, new FS_MATRIX(1, 0, 0, 1, x, y)); Pdfium.FPDF_RenderPage( hdc, currentPage, (int)0, (int)0, (int)(width), (int)(height), rotation, RenderFlags); //Print next page if (_pageForPrint < PrinterSettings.ToPage - (_useDP ? PrinterSettings.FromPage : 1)) { _pageForPrint++; e.HasMorePages = true; } } finally { if (hdc != IntPtr.Zero) { e.Graphics.ReleaseHdc(hdc); } hdc = IntPtr.Zero; if (currentPage != IntPtr.Zero) { Pdfium.FPDF_ClosePage(currentPage); } currentPage = IntPtr.Zero; } }
/// <summary> /// Raises the System.Drawing.Printing.PrintDocument.PrintPage event. It is called /// before a page prints. /// </summary> /// <param name="e"> A System.Drawing.Printing.PrintPageEventArgs that contains the event data.</param> /// <seealso href="https://pdfium.patagames.com/c-pdf-library/">C# Print PDF</seealso> protected override void OnPrintPage(PrintPageEventArgs e) { base.OnPrintPage(e); if (_pdfDoc == null) { throw new ArgumentNullException("Document"); } IntPtr hdc = IntPtr.Zero; IntPtr currentPage = IntPtr.Zero; try { if (e.Cancel) { return; } currentPage = Pdfium.FPDF_LoadPage(_docForPrint, _pageForPrint); if (currentPage == IntPtr.Zero) { e.Cancel = true; return; } double dpiX = e.Graphics.DpiX; double dpiY = e.Graphics.DpiY; double width = Pdfium.FPDF_GetPageWidth(currentPage) / 72 * dpiX; double height = Pdfium.FPDF_GetPageHeight(currentPage) / 72 * dpiY; double x, y; Rectangle clipRect; CalcSize(dpiX, dpiY, e.PageSettings.PrintableArea, e.MarginBounds, new PointF(e.PageSettings.HardMarginX, e.PageSettings.HardMarginY), e.PageSettings.Landscape, ref width, ref height, out x, out y, out clipRect); int ix = (int)x; int iy = (int)y; int iw = (int)width; int ih = (int)height; using (var page = PdfPage.FromHandle(_pdfDoc, currentPage, _pageForPrint, true)) OnBeforeRenderPage(e.Graphics, page, ref ix, ref iy, ref iw, ref ih, PageRotate.Normal); hdc = e.Graphics.GetHdc(); if (OriginAtMargins) { Pdfium.IntersectClipRect(hdc, clipRect.Left, clipRect.Top, clipRect.Right, clipRect.Bottom); } Pdfium.FPDF_RenderPage(hdc, currentPage, ix, iy, iw, ih, PageRotate.Normal, RenderFlags); if (hdc != IntPtr.Zero) { e.Graphics.ReleaseHdc(hdc); } hdc = IntPtr.Zero; using (var page = PdfPage.FromHandle(_pdfDoc, currentPage, _pageForPrint, true)) OnAfterRenderPage(e.Graphics, page, ix, iy, iw, ih, PageRotate.Normal); //Print next page if (_pageForPrint < PrinterSettings.ToPage - (_useDP ? PrinterSettings.FromPage : 1)) { _pageForPrint++; e.HasMorePages = true; } } finally { if (hdc != IntPtr.Zero) { e.Graphics.ReleaseHdc(hdc); } if (currentPage != IntPtr.Zero) { Pdfium.FPDF_ClosePage(currentPage); } } }