←Select platform

DocumentEmfPage Class

Summary

Data for one Windows Enhanced Metafile (WMF) page to be added to a document file.

Syntax

C#
VB
C++
public class DocumentEmfPage : DocumentPage 
Public Class DocumentEmfPage  
   Inherits Leadtools.Forms.Documentwriters.DocumentPage 
public ref class DocumentEmfPage : public Leadtools.Forms.Documentwriters.DocumentPage  

Remarks

The DocumentEmfPage class derives from DocumentPage and contains the data for one EMF page to be added to a document file. It is used as a parameter to the DocumentWriter.AddPage or DocumentWriter.InsertPage methods used to add the page.

The DocumentEmfPage.EmfHandle property must contain a valid Windows Enhanced Meta File handle (EMF) that contains the visual representation of the page. This EMF handle can obtained through multiple sources as explained in LEADTOOLS Document Writers.

DocumentEmfPage.EmfHandle is used as is when DocumentWriter.AddPage or DocumentWriter.InsertPage is called and the framework does not delete it. You must use the Windows API DeleteEnhMetaFile to delete the object when it is no longer used.

The DocumentEmfPage.Image property is optional and is used only when the document being created is PDF with the Image/Text overlay option. To create a PDF document with image/text overlay, perform the following steps:

The Dots/Inch (DPI) of the page is the same as the DPI stored in the DocumentEmfPage.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). If you are using the PDF with image/text feature, you must set the DPI of the RasterImage object to the same DPI as the EMF handle using the RasterImage.XResolution and RasterImage.YResolution properties. To override this behavior and set a custom resolution, set the value of the DocumentOptions.DocumentResolution property to the desired final resolution. This value will be used instead of the resolution of the EMF handle.

The LEADTOOLS Document Writer supports creating documents with zero or more empty pages inside them. Use DocumentWriter.AddPage or DocumentWriter.InsertPage an DocumentEmptyPage with the dimension of the empty page 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 in any index desired. To use empty pages, make sure the DocumentOptions.PageRestriction property is set to DocumentPageRestriction.Relaxed.

For more information, refer to PdfDocumentOptions.

Example

This example will create EMF handles and then use them to create a multi-page PDF document.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Forms.DocumentWriters; 
using Leadtools.Forms.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 DocumentEmfPage(); 
      page.EmfHandle = GetPageEmf(pageNumber, pageWidth, pageHeight, resolution); 
      docWriter.AddPage(page); 
 
      if (page.EmfHandle != IntPtr.Zero) 
         DeleteEnhMetaFile(page.EmfHandle); 
   } 
 
   docWriter.EndDocument(); 
 
   // 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:\Users\Public\Documents\LEADTOOLS Images"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Forms.DocumentWriters 
Imports Leadtools.Forms.Ocr 
 
Public Sub DocumentEmfPageExample() 
   ' Output PDF file name 
   Dim pdfFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Example.pdf") 
 
   ' Document properties, each page is 8.5 by 11 inches at 300 DPI. Typical dimensions 
   Dim resolution As Integer = 300 
   Dim pageWidth As Integer = CInt(8.5 * resolution) 
   Dim pageHeight As Integer = CInt(11 * resolution) 
 
   ' Create the document writer 
   Dim docWriter As New DocumentWriter() 
   docWriter.BeginDocument(pdfFileName, DocumentFormat.Pdf) 
 
   For pageNumber As Integer = 1 To 2 
      Dim page As New DocumentEmfPage() 
      page.EmfHandle = GetPageEmf(pageNumber, pageWidth, pageHeight, resolution) 
      docWriter.AddPage(page) 
 
      If page.EmfHandle <> IntPtr.Zero Then 
         DeleteEnhMetaFile(page.EmfHandle) 
      End If 
   Next 
 
   docWriter.EndDocument() 
 
   ' Show the PDF file 
   System.Diagnostics.Process.Start(pdfFileName) 
End Sub 
 
Private Shared Function GetPageEmf(pageNumber As Integer, pageWidth As Integer, pageHeight As Integer, resolution As Integer) As IntPtr 
   ' Get the screen DC 
   Using graphicsScreen As Graphics = Graphics.FromHwndInternal(IntPtr.Zero) 
      Dim hdcScreen As IntPtr = graphicsScreen.GetHdc() 
 
      ' Calculate the EMF rectangle in 1/100 mm 
      Dim frameRect As New RectangleF( 
         0, 
         0, 
         CType((pageWidth * 2540.0F + resolution / 2.0) / resolution, Single), 
         CType((pageHeight * 2540.0F + resolution / 2.0) / resolution, Single)) 
 
      ' Create the EMF, GDI compatible 
      Using metaFile As New Metafile(hdcScreen, frameRect, MetafileFrameUnit.GdiCompatible, EmfType.EmfOnly, Nothing) 
         ' No need for this anymore 
         graphicsScreen.ReleaseHdc(hdcScreen) 
 
         ' Now create the graphics to draw in from the metafile 
         Using graphics As Graphics = graphics.FromImage(metaFile) 
            ' Get the EMF DPI 
            Dim header As MetafileHeader = metaFile.GetMetafileHeader() 
            Dim emfDpiX As Single = header.DpiX 
            Dim emfDpiY As Single = 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 
            Dim fontScale As Single = CType(resolution, Single) / Math.Max(graphics.DpiX, graphics.DpiY) 
 
            ' Ready, now we can draw... 
 
            ' Fill it with white 
            Dim rc As 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 
            Dim text As String = "This is page " + pageNumber.ToString() 
 
            Dim pointSize As Single = 20 * fontScale 
            ' fonts need to converted to scaled back, so ... 
            Using font As New Font("Arial", pointSize, FontStyle.Regular) 
               graphics.DrawString(text, font, Brushes.Black, 0, 0) 
            End Using 
 
            ' 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) 
         End Using 
 
         Return metaFile.GetHenhmetafile() 
      End Using 
   End Using 
End Function 
 
' GDI interop 
<DllImport("gdi32.dll")> 
Private Shared Function DeleteEnhMetaFile(hemf As IntPtr) As Integer 
End Function 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
End Class 

Requirements

Target Platforms

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Forms.DocumentWriters Assembly