public IntPtr EmfHandle { get; set; }
An IntPtr structure that contains the unmanaged handle to the Windows Enhanced Meta File object.
The EmfHandle property must contain a valid Windows Enhanced Meta File handle (EMF)--it cannot be IntPtr.Zero. This EMF handle is used in the DocumentWriter.AddPage or DocumentWriter.InsertPage methods to create the visual representation of the new page added to the document being created.
The LEADTOOLS Document Writer toolkit will not use the EmfHandle after the call to DocumentWriter.AddPage or DocumentWriter.InsertPage returns. This handle needs to be deleted to free the resources associated with it (for example, using the Windows API DeleteEnhMetaFile).
The Windows Enhanced Meta File (EMF) objects can be obtained from multiple sources as explained in LEADTOOLS Document Writers.
The Dots/Inch (DPI) of the page is the same as the DPI stored in the EmfHandle property. Therefore, to create a page with 300 DPI, you must add a document page with an EMF that has a DPI of 300 (both horizontally or vertically although the LEADTOOLS Document Writer supports different values for the DPI). When using the PDF with image and text feature, set the DPI of the RasterImage object to the same DPI as the EMF handle using the RasterImage.XResolution and RasterImage.YResolution properties.
The LEADTOOLS Document Writer supports creating documents with zero or more empty pages inside them. Simply pass a page of type DocumentWriterEmptyPage to DocumentWriter.AddPage or DocumentWriter.InsertPage methods. The dimension of the empty page must be set before hand in the DocumentOptions.EmptyPageWidth and DocumentOptions.EmptyPageHeight and its resolution set to DocumentOptions.EmptyPageResolution. As many empty pages as desired can be added and in any index desired. To use empty pages, make sure the DocumentOptions.PageRestriction property is set to DocumentPageRestriction.Relaxed.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Document.Writer;
using Leadtools.Ocr;
public void DocumentEmfPageExample()
{
// Output PDF file name
var pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, "Example.pdf");
// Document properties, each page is 8.5 by 11 inches at 300 DPI. Typical dimensions
var resolution = 300;
var pageWidth = (int)(8.5 * resolution);
var pageHeight = (int)(11 * resolution);
// Create the document writer
var docWriter = new DocumentWriter();
docWriter.BeginDocument(pdfFileName, DocumentFormat.Pdf);
for (var pageNumber = 1; pageNumber <= 2; pageNumber++)
{
var page = new DocumentWriterEmfPage();
page.EmfHandle = GetPageEmf(pageNumber, pageWidth, pageHeight, resolution);
docWriter.AddPage(page);
if (page.EmfHandle != IntPtr.Zero)
DeleteEnhMetaFile(page.EmfHandle);
}
docWriter.EndDocument();
// To show the PDF file
// System.Diagnostics.Process.Start(pdfFileName);
}
private static IntPtr GetPageEmf(int pageNumber, int pageWidth, int pageHeight, int resolution)
{
// Get the screen DC
using (var graphicsScreen = Graphics.FromHwndInternal(IntPtr.Zero))
{
var hdcScreen = graphicsScreen.GetHdc();
// Calculate the EMF rectangle in 1/100 mm
var frameRect = new RectangleF(
0,
0,
((float)pageWidth * 2540 + resolution / 2) / resolution,
((float)pageHeight * 2540 + resolution / 2) / resolution);
// Create the EMF, GDI compatible
using (var metaFile = new Metafile(hdcScreen, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfOnly, null))
{
// No need for this anymore
graphicsScreen.ReleaseHdc(hdcScreen);
// Now create the graphics to draw in from the metafile
using (var graphics = Graphics.FromImage(metaFile))
{
// Get the EMF DPI
MetafileHeader header = metaFile.GetMetafileHeader();
var emfDpiX = header.DpiX;
var emfDpiY = header.DpiY;
// We must convert each value we want from pixels to EMF coordinates, so use a scale
graphics.ScaleTransform(emfDpiX / resolution, emfDpiY / resolution);
// To draw font, we must use this scale value to convert from point size
var fontScale = (float)resolution / Math.Max(graphics.DpiX, graphics.DpiY);
// Ready, now we can draw...
// Fill it with white
var rc = new RectangleF(0, 0, pageWidth, pageHeight);
graphics.FillRectangle(Brushes.White, rc);
// Put a frame
rc = new RectangleF(1, 1, pageWidth - 2, pageHeight - 2);
graphics.DrawRectangle(Pens.DarkGreen, rc.X, rc.Y, rc.Width, rc.Height);
// Draw some text
var text = "This is page " + pageNumber.ToString();
float pointSize = 20 * fontScale;
// fonts need to converted to scaled back, so ...
using (var font = new Font("Arial", pointSize, FontStyle.Regular))
graphics.DrawString(text, font, Brushes.Black, 0, 0);
// Add some vector objects, red line
rc = new Rectangle(50, 50, pageWidth - 100, pageHeight - 100);
graphics.DrawLine(Pens.Red, rc.Left, rc.Top, rc.Right, rc.Bottom);
// blue rect somewhere
rc = new Rectangle(100, 100, 150, 100);
graphics.FillRectangle(Brushes.Blue, rc);
}
return metaFile.GetHenhmetafile();
}
}
}
// GDI interop
[DllImport("gdi32.dll")]
private static extern int DeleteEnhMetaFile(IntPtr hemf);
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";
}