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.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); } } }