TX Text Control .NET for Windows Forms

Create a PDF

Topic

A document in the TextControl becomes a PDF with one call of Save, and SaveSettings adds what a PDF carries beyond its pages: a title and an author, a password, permissions, or PDF/A conformance for archiving. Starting from the editor of your first Windows Forms application, the steps below save the document as PDF in each of these ways and open the result in the default PDF reader.

Prerequisites

For each of the following sections, add a Button from the toolbox to the form and double-click it in the designer to create its Click handler. The code goes into Form1.cs, which needs these using directives:

using System.Diagnostics;
using TXTextControl;

Save the Document as PDF

The following handler saves the document of textControl1 with a few document properties to report.pdf in the user's Documents folder and opens the file:

private void createPdfButton_Click(object sender, EventArgs e)
{
    SaveSettings saveSettings = new SaveSettings()
    {
        Author = "Tim Typer",
        DocumentTitle = "Quarterly Report",
        DocumentSubject = "Finance",
        DocumentKeywords = new string[] { "report", "Q2", "finance" },
        CreatorApplication = "My Windows Forms Application",
        CreationDate = DateTime.Now,
        LastModificationDate = DateTime.Now
    };

    string path = Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
        "report.pdf");

    textControl1.Save(path, StreamType.AdobePDF, saveSettings);

    // Open the PDF in the default PDF reader
    Process.Start(new ProcessStartInfo(path) { UseShellExecute = true });
}

The code works as follows:

Run the application, type some text and click the button. The PDF reader opens report.pdf with the text you typed.

Its document properties, which PDF readers show in their Document Properties dialog box, contain the title, author, subject and keywords of the SaveSettings:

The Document Properties dialog box of Adobe Acrobat for a PDF created with TX Text Control, showing its title, author, subject, keywords, creation and modification dates and the application.

Let the User Choose the File

Called with a StreamType and no file name, Save opens the Save As dialog box with the filter of that format, and SaveSettings.SavedFile then contains the file the user chose. Load and Save Documents builds Save As this way. A further overload adds options to the dialog box, such as a button to enter a PDF password. See Supported File Formats.

Create a PDF/A Document

PDF/A is the standard for long-term archiving, and TX Text Control saves it as PDF/A-3a with StreamType.AdobePDFA. A PDF/A document embeds its fonts, so the document should use only fonts that can be embedded:

private void createPdfAButton_Click(object sender, EventArgs e)
{
    // Use only fonts that can be embedded in the PDF
    textControl1.FontSettings.EmbeddableFontsOnly = true;

    textControl1.Save("archive.pdf", StreamType.AdobePDFA);
}

FontSettings.EmbeddableFontsOnly restricts the document to embeddable fonts. PDF/A does not allow encryption, so a PDF/A document has no password. See PDF/A.

Protect the PDF With Passwords

A PDF can have two passwords: the user password is needed to open the document, and the master password protects its permissions, such as printing or copying text.

private void protectedPdfButton_Click(object sender, EventArgs e)
{
    SaveSettings saveSettings = new SaveSettings()
    {
        UserPassword = "user1234",
        MasterPassword = "owner1234",
        DocumentAccessPermissions = DocumentAccessPermissions.AllowLowLevelPrinting |
                                    DocumentAccessPermissions.AllowExtractContents
    };

    textControl1.Save("protected.pdf", StreamType.AdobePDF, saveSettings);
}

When the file is opened, the PDF reader asks for the user password:

The Password dialog box of a PDF reader, stating that the file is protected and asking for the Document Open Password.

PDF readers enforce the permissions, but they are no protection against every program. See Passwords and Access Permissions.

Create a PDF Without the Editor

To convert a document the user does not see, use a ServerTextControl, which has the same Load and Save methods and takes the same SaveSettings. Convert a File Without the Editor shows the code.

Next Steps