Products | Support | Email a link to this topic. | Send comments on this topic. | Back to Introduction | Help Version 19.0.6.22
|
[C#]
using System.IO;
using System.Diagnostics;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Forms.DocumentWriters;
[Visual Basic]
Imports System.IO
Imports System.Diagnostics
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.Forms.DocumentWriters
[C#]
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);
[Visual Basic]
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)