Take the following steps to start a project and to add some code that creates an EMF file, adds some shapes and text, then uses LEADTOOLS Document Writers to convert the EMF file to PDF:
Start Visual Studio .NET.
Choose File->New->Project from the menu.
In the New Project dialog box, choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "Windows Application" in the Templates List.
Type the project name as "DocumentWritersTutorial" in the Project Name field, and then click OK. If desired, type a new location for your project or select a directory using the Browse button, and then click OK.
In the Solution Explorer window, right-click on the References folder, and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the ".NET" tab, browse to the "<LEADTOOLS_INSTALLDIR> \Bin\DotNet\Win32" folder, and select the following DLLs:
Click the Select button and then press the OK button to add the above DLLs to the application.
Make sure Form1 is in design view. Go to the toolbox (View->Toolbox from the menu) and add the following controls to the form:
Type | Name | Text |
---|---|---|
Button | convertEmfToPdfButton | Convert EMF to PDF |
CheckBox | imageOverTextCheckBox | Image over text |
CheckBox | pdfACheckBox | pdf/a |
Switch to Form1 code view (right-click Form1 in the solution explorer then select View Code) and add the following lines at the beginning of the file:
[Visual Basic]
Imports System.IO
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.Forms.DocumentWriters
[C#]
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Forms.DocumentWriters;
Add the following code snippet to Form1:
[Visual Basic]
' Windows API functions used in this demo
<DllImport("gdi32.dll")> _
Private Shared Function GetEnhMetaFile(ByVal lpszMetaFile As String) As IntPtr
End Function
<DllImport("gdi32.dll")> _
Private Shared Function DeleteEnhMetaFile(ByVal hemf As IntPtr) As Boolean
End Function
Private codecs As RasterCodecs ' RasterCodecs object to use when loading TIF files
[C#]
// Windows API functions used in this demo
[DllImport("gdi32.dll")]
private static extern IntPtr GetEnhMetaFile(string lpszMetaFile);
[DllImport("gdi32.dll")]
private static extern bool DeleteEnhMetaFile(IntPtr hemf);
private RasterCodecs codecs; // RasterCodecs object to use when loading TIF files
Add the following code to the Form1 constructor (in Visual Basic, you can copy/paste the whole Sub New code from here):
[Visual Basic]
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' Replace with your own key
Dim MY_LICENSE_FILE As String = "d:\temp\TestLic.lic"
' Unlock the Document Writers support
Dim MY_DOCWRTDEVELOPER_KEY As String = "xyz123abc"
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DOCWRTDEVELOPER_KEY)
' Unlock the Document Writers PDF support
Dim MY_DOCPDFDEVELOPER_KEY As String = "abc123xyz"
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DOCPDFDEVELOPER_KEY)
' Unlock the Document support
Dim MY_DOCDEVELOPER_KEY As String = "123xyzabc"
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DOCDEVELOPER_KEY)
' Initialize RasterCodecs
codecs = new RasterCodecs()
End Sub
[C#]
public Form1()
{
InitializeComponent();
// Please replace with your own key
string MY_LICENSE_FILE = "d:\\temp\\TestLic.lic";
// Unlock Document Writers support
string MY_DOCWRTDEVELOPER_KEY = "xyz123abc";
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DOCWRTDEVELOPER_KEY);
// Unlock Document Writers PDF support
string MY_DOCPDFDEVELOPER_KEY = "abc123xyz";
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DOCPDFDEVELOPER_KEY);
// Unlock Document support
string MY_DOCDEVELOPER_KEY = "123xyzabc";
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DOCDEVELOPER_KEY);
// Initialize RasterCodecs
codecs = new RasterCodecs();
}
Add the following code as the OnFormClosed method for Form1 to clean up the resources used in the demo:
[Visual Basic]
Protected Overrides Sub OnFormClosed(ByVal e As FormClosedEventArgs)
' Clean up
codecs.Dispose()
End Sub
[C#]
protected override void OnFormClosed(FormClosedEventArgs e)
{
// Clean up
codecs.Dispose();
}
Add the following code for the convertEmfToPdfButton (Convert EMF to PDF) control's Click handler:
[Visual Basic]
Private Sub convertEmfToPdfButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles convertEmfToPdfButton.Click
' Get the output file name
Dim pdfFileName As String
Using dlg As New SaveFileDialog()
' Get the PDF friendly name and extension to use in the common dialog
Dim friendlyName As String = DocumentWriter.GetFormatFriendlyName(DocumentFormat.Pdf)
Dim extension As String = DocumentWriter.GetFormatFileExtension(DocumentFormat.Pdf)
dlg.Filter = String.Format("{0}(*.{1})|*.{1}|All Files|*.*", friendlyName, extension)
If (dlg.ShowDialog(Me) = DialogResult.OK) Then
pdfFileName = dlg.FileName
Else
Return
End If
End Using
' Create an instance of the LEADTOOLS DocumentWriter
Dim docWriter As New DocumentWriter()
' Set the PDF options
Dim pdfOptions As PdfDocumentOptions = DirectCast(docWriter.GetOptions(DocumentFormat.Pdf), PdfDocumentOptions)
Dim imageOverText As Boolean = imageOverTextCheckBox.Checked
If (imageOverText) Then
pdfOptions.ImageOverText = True
Else
pdfOptions.ImageOverText = False
End If
If (pdfACheckBox.Checked) Then
pdfOptions.DocumentType = PdfDocumentType.PdfA
Else
pdfOptions.DocumentType = PdfDocumentType.Pdf
End If
docWriter.SetOptions(DocumentFormat.Pdf, pdfOptions)
' Begin a new PDF document
docWriter.BeginDocument(pdfFileName, DocumentFormat.Pdf)
' Add the pages
' LEADTOOLS ships with Ocr1.emf to Ocr4.emf in the images folder
Dim imagesFolder As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "LEADTOOLS Images")
For page As Integer = 1 To 4
' Load the EMF
Dim emfFileName As String = Path.Combine(imagesFolder, String.Format("Ocr{0}.emf", page))
Dim tifFileName As String = Path.Combine(imagesFolder, String.Format("Ocr{0}.tif", page))
' Add the page
Dim docPage As DocumentPage = DocumentPage.Empty
docPage.EmfHandle = GetEnhMetaFile(emfFileName)
If (imageOverText) Then
docPage.Image = codecs.Load(tifFileName)
Else
docPage.Image = Nothing
End If
docWriter.AddPage(docPage)
' Clean up
If docPage.EmfHandle <> IntPtr.Zero Then
DeleteEnhMetaFile(docPage.EmfHandle)
End If
If Not IsNothing(docPage.Image) Then
docPage.Image.Dispose()
End If
Next
' Finish the document
docWriter.EndDocument()
' We successfuly created the PDF, ask the user if they want to view it
Dim question As String = String.Format("PDF document was successfully created at {0}\n\nDo you want to view it now?", pdfFileName)
If MessageBox.Show(Me, question, "DocumentWriter", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
Process.Start(pdfFileName)
End If
End Sub
[C#]
private void convertEmfToPdfButton_Click(object sender, EventArgs e)
{
// Get the output file name
string pdfFileName;
using(SaveFileDialog dlg = new SaveFileDialog())
{
// Get the PDF friendly name and extension to use in the common dialog
string friendlyName = DocumentWriter.GetFormatFriendlyName(DocumentFormat.Pdf);
string extension = DocumentWriter.GetFormatFileExtension(DocumentFormat.Pdf);
dlg.Filter = string.Format("{0}(*.{1})|*.{1}|All Files|*.*", friendlyName, extension);
if(dlg.ShowDialog(this) == DialogResult.OK)
pdfFileName = dlg.FileName;
else
return;
}
// Create an instance of the LEADTOOLS DocumentWriter
DocumentWriter docWriter = new DocumentWriter();
// Set the PDF options
PdfDocumentOptions pdfOptions = docWriter.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions;
bool imageOverText = imageOverTextCheckBox.Checked;
if(imageOverText)
pdfOptions.ImageOverText = true;
else
pdfOptions.ImageOverText = false;
if(pdfACheckBox.Checked)
pdfOptions.DocumentType = PdfDocumentType.PdfA;
else
pdfOptions.DocumentType = PdfDocumentType.Pdf;
docWriter.SetOptions(DocumentFormat.Pdf, pdfOptions);
// Begin a new PDF document
docWriter.BeginDocument(pdfFileName, DocumentFormat.Pdf);
// Add the pages
// LEADTOOLS ships with Ocr1.emf to Ocr4.emf in the images folder
string imagesFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"LEADTOOLS Images");
for(int page = 1; page <= 4; page++)
{
// Load the EMF
string emfFileName = Path.Combine(imagesFolder, string.Format("Ocr{0}.emf", page));
string tifFileName = Path.Combine(imagesFolder, string.Format("Ocr{0}.tif", page));
// Add the page
DocumentPage docPage = DocumentPage.Empty;
docPage.EmfHandle = GetEnhMetaFile(emfFileName);
if(imageOverText)
docPage.Image = codecs.Load(tifFileName);
else
docPage.Image = null;
docWriter.AddPage(docPage);
// Clean up
if(docPage.EmfHandle != IntPtr.Zero)
DeleteEnhMetaFile(docPage.EmfHandle);
if(docPage.Image != null)
docPage.Image.Dispose();
}
// Finish the document
docWriter.EndDocument();
// We successfuly created the PDF, ask the user if they want to view it
string question = string.Format("PDF document was successfully created at {0}\n\nDo you want to view it now?", pdfFileName);
if(MessageBox.Show(this, question, "DocumentWriter", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Process.Start(pdfFileName);
}
}
Build and Run the program to test it.
Click the "Convert EMF To PDF" button. Try with different options for the PDF file and check the results.
For information on using the LEADTOOLS Document Writers, see Programming with the LEADTOOLS Document Writers.
NOTE: If you encounter an "Invalid File Format" or "Feature Not Supported" exception, please refer to the topic Invalid File Format/Feature Not Supported.