Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,138
Thanks: 10 times Was thanked: 133 time(s) in 130 post(s)
|
Well ... The PdfPrintdocument does exactly the same thing. It renders to device context (dc) directly as in your example. For one exception, in your example dc is obtained by the CreateMetaFile function, but PdfPrintDocument get it from the target printer. You can easily reproduce the code you provided in .Net Code:[DllImport(@"gdi32.dll", EntryPoint = "CreateEnhMetaFileA", SetLastError = true)]
private static extern IntPtr CreateEnhMetaFileA(IntPtr hdc, [MarshalAs(UnmanagedType.LPStr)]string file, IntPtr rect, IntPtr desc);
[DllImport(@"gdi32.dll", EntryPoint = "CloseEnhMetaFile", SetLastError = true)]
private static extern IntPtr CloseEnhMetaFile(IntPtr hdc);
[DllImport(@"gdi32.dll", EntryPoint = "DeleteEnhMetaFile", SetLastError = true)]
private static extern bool DeleteEnhMetaFile(IntPtr hMetaFile);
Code:using (var doc = PdfDocument.Load(@"d:\0\1\review.pdf"))
{
var page = doc.Pages[0];
int width = (int)page.Width;
int height = (int)page.Height;
IntPtr hdc = CreateEnhMetaFileA(IntPtr.Zero, @"d:\9\test.emf", IntPtr.Zero, IntPtr.Zero);
using (var g = System.Drawing.Graphics.FromHdc(hdc))
{
g.SetClip(new Rectangle(0, 0, width, height));
g.FillRectangle(Brushes.White, 0, 0, width, height);
page.Render(hdc, 0, 0, width, height, PageRotate.Normal, RenderFlags.FPDF_PRINTING | RenderFlags.FPDF_ANNOT | RenderFlags.FPDF_NO_CATCH);
}
DeleteEnhMetaFile(CloseEnhMetaFile(hdc));
}
|