Skip to main content

Document Generation

  • 5 minutes to read

This topic describes how to generate a document layout from scratch.

Create a document that has no pages by creating a PdfDocumentProcessor object and calling one of the PdfDocumentProcessor.CreateEmptyDocument overload methods.

These methods can be called using a stream, a file path (see the code snippet below), creation options (represented by an instance of the PdfCreationOptions class) or save options (represented by an instance of the PdfSaveOptions class).

Populate a document with graphic content:

  • Draw graphics on a page by creating a PdfGraphics object using the PdfDocumentProcessor.CreateGraphics method and calling the Draw method for corresponding elements (for example, the PdfGraphics.DrawString overload method draws a text string at the specified location with the specified SolidBrush and Font objects).

    Note

    You need to reference the DevExpress.Pdf.Drawing assembly to draw graphic content on a page because it requires an instance of the PdfGraphics class.

  • Render a page with created graphics by calling the PdfDocumentProcessor.RenderNewPage overload method.

View Example: Generate a Document Layout from Scratch

using DevExpress.Drawing;
using DevExpress.Pdf;
using System.Drawing;

static void Main(string[] args) {
     using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
          // Create an empty document.
          processor.CreateEmptyDocument("..\\..\\Result.pdf");

          // Create and draw PDF graphics.
          using (PdfGraphics graph = processor.CreateGraphics())
          {
          DrawGraphics(graph);

          // Render a page with graphics.
          processor.RenderNewPage(PdfPaperSize.Letter, graph);
          }
     }
}

static void DrawGraphics(PdfGraphics graph) {
     // Draw text lines on the page. 
     DXSolidBrush black = (DXSolidBrush)DXBrushes.Black;
     DXFont font1 = new DXFont("Times New Roman", 32, DXFontStyle.Bold);
     graph.DrawString("PDF Document Processor", font1, black, 180, 150);

     DXFont font2 = new DXFont("Arial", 20);
     graph.DrawString("Display, Print and Export PDF Documents", font2, black, 168, 230);

     DXFont font3 = new DXFont("Arial", 10);
     graph.DrawString("The PDF Document Processor is a non-visual component " +
                    "that provides the application programming interface of the PDF Viewer.", font3, black, 16, 300);
}

See the PDF Graphics topics for more information about PDF Graphics.

The PDF Document API provides additional settings to customize document generation. See the sections below.

Embed Fonts

The PDF Document API component creates a document with embedded fonts by default.

To prohibit embedding all fonts, set the PdfCreationOptions.DisableEmbeddingAllFonts property to true and pass a PdfCreationOptions object containing this setting as a parameter to one of the PdfDocumentProcessor.CreateEmptyDocument overload methods.

Use the PdfCreationOptions.NotEmbeddedFontFamilies property to specify which font families should not be embedded in a document.

Tip

If you get the No usable version of the ICU libraries was found Aborted message when you try to run your application on Linux, register the DXEXPORT_ICU_VERSION_OVERRIDE environment variable and set it to the current library version, as shown below:

export DXEXPORT_ICU_VERSION_OVERRIDE=65.1?

Specify a Document’s Compatibility Mode

The following compatibility modes are supported:

  • PDF (ISO 32000-1:2008 ) - default mode
  • PDF/A-1b (ISO 19005-1)
  • PDF/A-2b (ISO 19005-2:2011)
  • PDF/A-3b (ISO 19005-3:2012)

To create a PDF/A-compatible document, set the PdfCreationOptions.Compatibility property to either PdfCompatibility.PdfA1b, PdfCompatibility.PdfA2b or PdfCompatibility.PdfA3b. Then, call the PdfDocumentProcessor.CreateEmptyDocument overload method and pass a PdfCreationOptions object containing this setting as a parameter.

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    processor.CreateEmptyDocument("..\\..\\Result.pdf", new PdfCreationOptions()
    {
        Compatibility = PdfCompatibility.PdfA2b
    });
}

PDF/A has the following limitations:

  • Non-embedded fonts are not supported;
  • PDF/A-1b and PDF/A-2b documents cannot contain file attachments;
  • Encryption is forbidden;
  • Transparency is not allowed in a PDF/A-1b document (all transparency information is ignored, and no exceptions are raised).

Specify Encryption Settings and Signature

The PDF Document API can protect a document with user and owner passwords. These passwords are used to prevent users from accessing or modifying PDF documents. All required settings to protect a document are contained in the PdfEncryptionOptions object, which can be accessed via the PdfSaveOptions.EncryptionOptions property. To encrypt the document with these settings, call the PdfDocumentProcessor.CreateEmptyDocument overload method and pass the PdfSaveOptions object containing these settings as a parameter. See the Document Protection topic for more information.

Use the PdfSaveOptions.Signature property to specify a signature. See the Sign Document topic to learn more.

See Also