This tutorial shows how to create a new PDF document and add to it the first page from PDF files using the LEADTOOLS SDK in a C# Windows console application.
Overview | |
---|---|
Summary | This tutorial covers how to create a new PDF document and add pages to it using SVG in a C# Windows Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (3 KB) |
Platform | C# Windows Console Application |
IDE | Visual Studio 2017, 2019 |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Create Documents with Document Writers - Console C# tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.
If using NuGet references, this tutorial requires the following NuGet package:
Leadtools.Document.Sdk
If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Bmp.dll
Leadtools.Codecs.Cmp.dll
Leadtools.Codecs.Fax.dll
Leadtools.Codecs.Jb2.dll
Leadtools.Codecs.Raw.dll
Leadtools.Codecs.Tif.dll
Leadtools.Document.dll
Leadtools.Document.Writer.dll
Leadtools.Pdf.dll
Leadtools.Svg.dll
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.
There are two types of runtime licenses:
Note
Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.
With the project created, the references added, and the license set, coding can begin.
In the Solution Explorer, open Program.cs
. Add a new method called CreatePdfDocument()
and call it inside the Main()
method under SetLicense();
. Add the below code to create a new PDF file and add the first page of each PDF in a given directory to the new PDF.
// Add to using block
using System;
using System.IO;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Document.Writer;
static void CreatePdfDocument()
{
using (RasterCodecs codecs = new RasterCodecs())
{
string dir = @"C:\LEADTOOLS22\Resources\Images";
int pageNumber = 1;
string[] pdfFiles = Directory.GetFiles( dir, "*.pdf");
DocumentFormat format = DocumentFormat.Pdf;
string outFile = Path.Combine(dir, "DocumentWriters." + DocumentWriter.GetFormatFileExtension(format));
codecs.Options.RasterizeDocument.Load.Resolution = 300;
DocumentWriter docWriter = new DocumentWriter();
PdfDocumentOptions pdfOptions = docWriter.GetOptions(format) as PdfDocumentOptions;
pdfOptions.DocumentType = PdfDocumentType.PdfA;
pdfOptions.ImageOverText = true;
docWriter.SetOptions(format, pdfOptions);
// Begin a new PDF document
docWriter.BeginDocument(outFile, format);
// Add the pages
foreach (string file in pdfFiles)
{
DocumentWriterSvgPage page = new DocumentWriterSvgPage();
page.SvgDocument = codecs.LoadSvg(file, pageNumber, null);
if (pdfOptions.ImageOverText)
{
// If we are using image/text, then load the overlay raster image
page.Image = codecs.Load(file, pageNumber);
}
// Add the page to the created PDF document
docWriter.AddPage(page);
Console.WriteLine($"Added page {pageNumber} from {Path.GetFileNameWithoutExtension(file)}\n");
// Dispose of resources
if (page.SvgDocument != null)
page.SvgDocument.Dispose();
if (page.Image != null)
page.Image.Dispose();
}
// Finalized document to disk
docWriter.EndDocument();
Console.WriteLine("PDF document saved successfully!");
}
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application runs and creates a new PDF file and adds the first page of each PDF file in a given directory using SVG and Document Writers.
This tutorial showed how to create documents using the Document Writers. It also covered how to use the DocumentWriter
, PdfDocumentOptions
, DocumentWriterSvgPage
, and RasterCodecs
classes.