From 8287d6e8d206401172cf53ab85aa5cdb4f92f4b2 Mon Sep 17 00:00:00 2001 From: Dave Cattermole Date: Fri, 16 Apr 2021 09:15:14 +0100 Subject: [PATCH] added FPDF_RenderPage and Samples/Test.Print --- PDFiumSharp/PDFium.tt | 5 ++ PDFiumSharp/PdfPage.cs | 15 ++++ Samples/Test.Print/App.config | 6 ++ Samples/Test.Print/Program.cs | 43 ++++++++++ Samples/Test.Print/Properties/AssemblyInfo.cs | 36 ++++++++ Samples/Test.Print/Test.Print.csproj | 82 +++++++++++++++++++ 6 files changed, 187 insertions(+) create mode 100644 Samples/Test.Print/App.config create mode 100644 Samples/Test.Print/Program.cs create mode 100644 Samples/Test.Print/Properties/AssemblyInfo.cs create mode 100644 Samples/Test.Print/Test.Print.csproj diff --git a/PDFiumSharp/PDFium.tt b/PDFiumSharp/PDFium.tt index eb7f6f3..46cf27e 100644 --- a/PDFiumSharp/PDFium.tt +++ b/PDFiumSharp/PDFium.tt @@ -91,6 +91,11 @@ var Imports = new[] { Documentation = new string[] { "" }, Attributes = new string[] { } }, + new { Method = "void FPDF_RenderPage(IntPtr dc, FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y, PageOrientations rotation, RenderingFlags flags)", + AccessModifier = "public", + Documentation = new string[] { "" }, + Attributes = new string[] { } }, + new { Method = "void FPDF_RenderPageBitmap(FPDF_BITMAP bitmap, FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y, PageOrientations rotation, RenderingFlags flags)", AccessModifier = "public", Documentation = new string[] { "" }, diff --git a/PDFiumSharp/PdfPage.cs b/PDFiumSharp/PdfPage.cs index f4f1f00..d9a3a9b 100644 --- a/PDFiumSharp/PdfPage.cs +++ b/PDFiumSharp/PdfPage.cs @@ -74,6 +74,21 @@ public PageOrientations Orientation internal static PdfPage Load(PdfDocument doc, int index) => new PdfPage(doc, PDFium.FPDF_LoadPage(doc.Handle, index), index); internal static PdfPage New(PdfDocument doc, int index, double width, double height) => new PdfPage(doc, PDFium.FPDFPage_New(doc.Handle, index, width, height), index); + /// + /// Renders the page to a device context (dc) + /// + /// The bitmap to which the page is to be rendered. + /// The destination rectangle in . + /// The orientation at which the page is to be rendered. + /// The flags specifying how the page is to be rendered. + public void Render(IntPtr renderTarget, (int left, int top, int width, int height) rectDest, PageOrientations orientation = PageOrientations.Normal, RenderingFlags flags = RenderingFlags.None) + { + if (renderTarget == IntPtr.Zero) + throw new ArgumentNullException(nameof(renderTarget)); + + PDFium.FPDF_RenderPage(renderTarget, this.Handle, rectDest.left, rectDest.top, rectDest.width, rectDest.height, orientation, flags); + } + /// /// Renders the page to a /// diff --git a/Samples/Test.Print/App.config b/Samples/Test.Print/App.config new file mode 100644 index 0000000..731f6de --- /dev/null +++ b/Samples/Test.Print/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Samples/Test.Print/Program.cs b/Samples/Test.Print/Program.cs new file mode 100644 index 0000000..ee2168e --- /dev/null +++ b/Samples/Test.Print/Program.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PDFiumSharp; +using System.IO; + +using System.Drawing; +using System.Drawing.Printing; + +namespace TestPrint +{ + class Program + { + + static void Main(string[] args) + { + using (var doc = new PdfDocument("TestDoc.pdf", "password")) + { + PrintDocument printDoc = new PrintDocument(); + //printDoc.PrinterSettings.PrinterName = "Some Printer Name"; // Use a named printer rather than default + printDoc.PrintController = new StandardPrintController(); // Use StandardPrintController to prevent "page I of N" box being shown in WIndows + int i = 0; + printDoc.PrintPage += new PrintPageEventHandler((sender, ev) => { + if (i < doc.Pages.Count) { + PdfPage page = doc.Pages[i]; + ev.Graphics.PageUnit = GraphicsUnit.Pixel; + Rectangle r = Rectangle.Round(ev.Graphics.VisibleClipBounds); + page.Render(ev.Graphics.GetHdc(), (r.Left, r.Top, r.Width, r.Height), PageOrientations.Normal, RenderingFlags.Printing); + } + i++; + if (i >= doc.Pages.Count) + ev.HasMorePages = false; + else + ev.HasMorePages = true; + }); + printDoc.Print(); + } + Console.ReadKey(); + } + } +} diff --git a/Samples/Test.Print/Properties/AssemblyInfo.cs b/Samples/Test.Print/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..db70c36 --- /dev/null +++ b/Samples/Test.Print/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Test.Console")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Test.Console")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("c07538e4-238c-42ac-bc19-8afa66bd6bc2")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Samples/Test.Print/Test.Print.csproj b/Samples/Test.Print/Test.Print.csproj new file mode 100644 index 0000000..b61acfd --- /dev/null +++ b/Samples/Test.Print/Test.Print.csproj @@ -0,0 +1,82 @@ + + + + + Debug + AnyCPU + {ADDA41B4-261D-416B-B2BB-5363DF5A621C} + Exe + Test.Print + Test.Print + v4.6.1 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + false + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + TestDoc.pdf + PreserveNewest + + + + + + pdfium_x64.dll + PreserveNewest + + + pdfium_x86.dll + PreserveNewest + + + + + {782d0c5a-2a13-480c-af88-6dadc726566e} + PDFiumSharp + + + + + 4.3.0 + + + 4.3.0 + + + + \ No newline at end of file