Skip to content

3.1 Document Creation Overview

OgreTransporter edited this page Mar 2, 2020 · 1 revision

The Test method below demonstrates the six steps described in the introduction for creating a PDF file. The method will be executed when you press on the “Article Example” button of the demo program. The following subsections describe in detail each step.

// Create article's example test PDF document
public void Test
        (
        bool Debug,
        string FileName
        )
{
    {
    // Step 1: Create empty document
    // Arguments: page width: 8.5", page height: 11", Unit of measure: inches
    // Return value: PdfDocument main class
    Document = new PdfDocument(PaperType.Letter, false, UnitOfMeasure.Inch, FileName);

    // for encryption test
    //		Document.SetEncryption(null, null, Permission.All & ~Permission.Print, EncryptionType.Aes128);

    // Debug property
    // By default it is set to false. Use it for debugging only.
    // If this flag is set, PDF objects will not be compressed, font and images will be replaced
    // by text place holder. You can view the file with a text editor but you cannot open it with PDF reader.
    Document.Debug = Debug;

    PdfInfo Info = PdfInfo.CreatePdfInfo(Document);
    Info.Title("Article Example");
    Info.Author("Uzi Granot");
    Info.Keywords("PDF, .NET, C#, Library, Document Creator");
    Info.Subject("PDF File Writer C# Class Library (Version 1.21.0)");

    // Step 2: create resources
    // define font resources
    DefineFontResources();

    // define tiling pattern resources
    DefineTilingPatternResource();

    // Step 3: Add new page
    Page = new PdfPage(Document);

    // Step 4:Add contents to page
    Contents = new PdfContents(Page);

    // Step 5: add graphices and text contents to the contents object
    DrawFrameAndBackgroundWaterMark();
    DrawTwoLinesOfHeading();
    DrawHappyFace();
    DrawBarcode();
    DrawPdf417Barcode();
    DrawImage();
    DrawChart();
    DrawTextBox();
    DrawBookOrderForm();

    // Step 6: create pdf file
    Document.CreateFile();

    // start default PDF reader and display the file
    Process Proc = new Process();
    Proc.StartInfo = new ProcessStartInfo(FileName);
    Proc.Start();

    // exit
    Return;
}
Clone this wiki locally