Skip to content

1. Introduction

OgreTransporter edited this page Mar 2, 2020 · 3 revisions

The PDF File Writer C# class library PdfFileWriter allows you to create PDF files directly from your .net application. The library shields you from the details of the PDF file structure. To use the library, you need to add a reference to the attached PdfFileWriter.dll class library file, add a using PdfFileWriter statement in every source file that uses the library and include the PdfFileWriter.dll with your distribution. For more details go to 4. Installation. The code was developed using .NET Framework 4.6.2 and Visual Studio 2019.

Version 1.26.0 enhancements: Support for PDF XMP Metadata and support for QR Code ECI Assinment number.

The PDF File Writer C# class library supports the following PDF document's features:

Creating a PDF is a six steps process.

  1. Create one document object PdfDocument.
  2. Create resource objects such as fonts or images (i.e. PdfFont or PdfImage).
  3. Create page object PdfPage.
  4. Create contents object PdfContents.
  5. Add text and graphics to the contents object (using PdfContents methods). (Repeat steps 3, 4 and 5 for additional pages)
  6. Create your PDF document file by calling CreateFile method of PdfDocument.

Step 5 is where most of your programming effort will be spent. Adding contents is achieved by calling the methods of PdfContents class to render graphics and text. The contents class has a rich set (about 100) of methods for adding text and graphics to your document.

PdfDocument implements the IDisposable interface to release unmanaged resources. The CreateFile method calls Document.Dispose() after the PDF file is created. However, to ensure the release of resources you should wrap the PdfDocument creation and the final CreateFile with either a using statement or a try/catch block.

The demo program attached to this article is the test program developed to debug the library. The TestPdfFileWriter has six buttons on the main screen. Five buttons to produce examples of PDF files and one button to display all fonts available on your computer. The first button “Article Example” creates the PDF file displayed at the top of this article. Section 3. Development Guide by Example.

As stated before, the PdfFileWriter C# class library shields you from the complexities of the PDF file structure. However, good understanding of PDF file is always an advantage. Adobe PDF file specification document available from Adobe website: "PDF Reference, Sixth Edition, Adobe Portable Document Format Version 1.7 November 2006". It is an intimidating 1310 pages document. I would strongly recommend reading Chapter 4 Graphics and sections 5.2 and 5.3 of the Text chapter 5.

If you want to analyze the PDF files created by this project, or if you want to understand PDF file structure in general, you can use the demo program attached to my previous article "PDF File Analyzer With C# Parsing Classes". This article provides a concise overview of the PDF specifications.

Clone this wiki locally