Take the following steps to start a project and to add some code that uses SVG to convert a Microsoft Word DOCX file to PDF:
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.
Add the following lines at the beginning of the file:
using System.IO;
using System.Diagnostics;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Forms.DocumentWriters;
Imports System.IO
Imports System.Diagnostics
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.Forms.DocumentWriters
Add the following code snippet to the Main method:
var inFileName = @"C:\users\Public\Documents\LEADTOOLS Images\Leadtools.docx";
var format = DocumentFormat.Pdf;
// Create the output file name
var dir = Path.GetDirectoryName(inFileName);
var outFileName = Path.Combine(dir, "DocumentWriterTutorial." + DocumentWriter.GetFormatFileExtension(format));
var codecs = new RasterCodecs();
// Set 300 as the default resolution for loading document files
codecs.Options.RasterizeDocument.Load.Resolution = 300;
// Get the number of pages of the input file
var pageCount = codecs.GetTotalPages(inFileName);
Console.WriteLine("Number of pages {0}", pageCount);
// Create an instance of the LEADTOOLS DocumentWriter
var docWriter = new DocumentWriter();
// Change this file to create a PDF file with overlay image
var imageOverText = false;
// Set the options
if (format == DocumentFormat.Pdf)
{
// If PDF, set to PDF/A
var pdfOptions = docWriter.GetOptions(format) as PdfDocumentOptions;
pdfOptions.DocumentType = PdfDocumentType.PdfA;
if (imageOverText)
pdfOptions.ImageOverText = true;
docWriter.SetOptions(format, pdfOptions);
}
// Begin a new PDF document
docWriter.BeginDocument(outFileName, format);
// Add the pages
for (var pageNumber = 1; pageNumber <= pageCount; pageNumber++)
{
// Load the page as SVG
Console.WriteLine("Loading page {0}", pageNumber);
var page = new DocumentSvgPage();
page.SvgDocument = codecs.LoadSvg(inFileName, pageNumber, null);
if (imageOverText)
{
// If we are using image/text, then load the overlay raster image
page.Image = codecs.Load(inFileName, pageNumber);
}
// Add the page to the current document
Console.WriteLine("Adding page {0}", pageNumber);
docWriter.AddPage(page);
if (page.SvgDocument != null)
page.SvgDocument.Dispose();
if (page.Image != null)
page.Image.Dispose();
}
// Done, finish
Console.WriteLine("Converting to final format");
docWriter.EndDocument();
codecs.Dispose();
Console.WriteLine("Viewing the result");
// Open the document in the default viewer
Process.Start(outFileName);
Dim inFileName As String = "C:\users\Public\Documents\LEADTOOLS Images\Leadtools.docx"
Dim format As DocumentFormat = DocumentFormat.Pdf
' Create the output file name
Dim dir As String = Path.GetDirectoryName(inFileName)
Dim outFileName As String = Path.Combine(dir, "DocumentWriterTutorial." + DocumentWriter.GetFormatFileExtension(format))
Dim codecs As New RasterCodecs
' Set 300 as the default resolution for loading document files
codecs.Options.RasterizeDocument.Load.Resolution = 300
' Get the number of pages of the input file
Dim pageCount As Integer = codecs.GetTotalPages(inFileName)
Console.WriteLine("Number of pages {0}", pageCount)
' Create an instance of the LEADTOOLS DocumentWriter
Dim docWriter As New DocumentWriter()
' Change this file to create a PDF file with overlay image
Dim imageOverText As Boolean = False
' Set the options
If format = DocumentFormat.Pdf Then
' If PDF, set to PDF/A
Dim pdfOptions = DirectCast(docWriter.GetOptions(format), PdfDocumentOptions)
' pdfOptions.DocumentType = PdfDocumentType.PdfA
If imageOverText Then pdfOptions.ImageOverText = True
docWriter.SetOptions(format, pdfOptions)
End If
' Begin a new PDF document
docWriter.BeginDocument(outFileName, format)
' Add the pages
For pageNumber As Integer = 1 To pageCount
' Load the page as SVG
Console.WriteLine("Loading page {0}", pageNumber)
Dim page As New DocumentSvgPage()
page.SvgDocument = codecs.LoadSvg(inFileName, pageNumber, Nothing)
If imageOverText Then
' If we are using image/text, then load the overlay raster image
page.Image = codecs.Load(inFileName, pageNumber)
End If
' Add the page to the current document
Console.WriteLine("Adding page {0}", pageNumber)
docWriter.AddPage(page)
If Not IsNothing(page.SvgDocument) Then page.SvgDocument.Dispose()
If Not IsNothing(page.Image) Then page.Image.Dispose()
Next
' Done, finish
Console.WriteLine("Converting to final format")
docWriter.EndDocument()
codecs.Dispose()
Console.WriteLine("Viewing the result")
' Open the document in the default viewer
Process.Start(outFileName)
Build and Run the program to test it.
Products |
Support |
Feedback: Tutorial: Document Writers Tutorial |
Introduction |
Help Version 19.0.2017.6.21
|
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
Your email has been sent to support! Someone should be in touch! If your matter is urgent please come back into chat.
Chat Hours:
Monday - Friday, 8:30am to 6pm ET
Thank you for your feedback!
Please fill out the form again to start a new chat.
All agents are currently offline.
Chat Hours:
Monday - Friday
8:30AM - 6PM EST
To contact us please fill out this form and we will contact you via email.