Skip to content
OgreTransporter edited this page Feb 28, 2020 · 5 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:

  • Graphics: drawing lines, rectangles, polygons, Bezier curves, foreground and background color, patterns and shading. Section 2.1 Coordinate System.
  • Decimal separator note for users in world regions using comma to denote fraction. See section 2.2 Decimal Separator.
  • Drawing text. Section 2.3 Language Support, fonts and character sets.
  • Drawing images: drawing raster (Bitmap) images and vector (Metafile) images. Section 2.4. Image Support.
  • Barcode: support for Barcode 128, Barcode 39, Barcode interleaved 2 of 5, Barcode EAN13 and Barcode UPC-A. Section 2.5 Barcode Support.
  • QR Code: support for two dimensions barcode. Section 2.6 QR Code Support.
  • PDF417 barcode. Section 2.7 PDF417 Barcode.
  • Web Link: Web link interactive support. Section 2.8 Web Link Support.
  • Bookmarks: Support for document outlines. Section 2.9 Bookmark Support.
  • Charts: Support for Microsoft Charting. Section 2.10 Charting Support.
  • Print to PDF: Create a PDF document from PrintDocument process. Section 2.11 PrintDocument Support.
  • Display data tables. Section 2.12 Data Table Support
  • Play video files. Section 2.13 Play Video Files
  • Play sound files. Section 2.14 Play Sound Files
  • Attach data files. Section 2.15 Attach Data Files
  • Reorder pages. Section 2.16 Reorder Pages
  • PDF document output to a file or to a stream. Section 2.17 PDF Document Output.
  • PDF document information dictionary. The PDF reader displays this information in the Description tab of the document properties. The information includes: Title, Author, Subject, Keywords, Created date and time, Modified date and time, the Application that produced the file, the PDF Producer. Section 2.18 Document Information Dictionary.
  • Memory control: Write contents information of completed pages to output file and free unused memory with garbage collector. Section 2.19. Memory Control.
  • Draw artwork defined by System.Windows.Media.PathGeometry class. Input argument can be text string or PathGeometry class. Section 2.20 Windows Presentation Foundation WPF
  • Transparency or opaqueness is available for painting shapes, lines, text and images. Your application can set the alpha component of color for all graphics and text. Section 2.21 Transparency, Opacity, Alpha Color Component and Blending
  • Blend. The library supports the PDF color blending scheme. Blending defines how the color of a new item painted over a previous item is handled. Section 2.21 Transparency, Opacity, Alpha Color Component and Blending
  • Document Links and Named Destination. Section 2.22 Document Links and Named Destination.
  • Encryption: support for AES-128 encryption. Section 2.23 Encryption Support.
  • Sticky Notes 2.24 Sticky Notes or Text Annotation.
  • Layers or Optional Content. 2.25 Layers or Optional Content.
  • Initial document display. 2.26 Initial Document Display.
  • XMP Metadata. 2.27 XMP Metadata.

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