ImageGear .NET v25.0 - Updated March 4, 2021
Edit PDF Content and Fonts
User Guide > How to Work with... > PDF > How to... > Manage PDF Content > Edit PDF Content and Fonts

The ImageGear API provides content editing functionality for creating, accessing, and editing PDF page content objects. With this API you can work with a page's content as a list of objects, such as images, text, forms. You can retain, modify, and save their data and properties.  

Document font support includes:

The code snippet below adds a text element to the content of a PDF. This example demonstrates creating a font, using the content in a PDF, and creating a text element.

To add Unicode text to a PDF, see the example at ImGearPDEText.Add.

PDF support needs to be initialized first for this snippet to work. To get familiar with initializing IGNET, initializing PDF support, loading a PDF, saving a PDF, and terminating PDF support, try any one of the tutorials.
C#
Copy Code
        using System;
        using System.IO;
        using System.Windows.Forms;
 
        using ImageGear.Core;
        using ImageGear.Formats;
        using ImageGear.Formats.PDF;
 
        // This function will add a text element into the content of the PDF document.
        public void AddTextToPDFContent(ImGearPDFDocument pdfDocument)
        {
            // Select the first page of the current document.
            ImGearPDFPage page = (ImGearPDFPage)pdfDocument.Pages[0];
 
            // Get the content of the first page of the PDF document.
            using (ImGearPDEContent content = (page).GetContent())
            {
                // Get the count of the elements on the page.
                int elementCount = content.ElementCount;
 
                // Make a new text element to add to the page content.
                ImGearPDEText textElement = CreateTextElement();
 
                // Insert a Text Element into the content.
                content.AddElement((int)ImGearPDEInsertElement.AFTER_LAST, textElement);
                // Set the page's PDE content back into the page object.
                page.SetContent();
            }
        }
 
        // This function returns a new font to use for the text element.
        public ImGearPDEFont CreateFont()
        {
            // Create a ImGearPDEFontAttrs to get the attributes for ImGearPDEFont and ImGearPDFSysFont.
            ImGearPDEFontAttrs findAttrs = new ImGearPDEFontAttrs();
 
            // Set the font name.
            findAttrs.Name = new ImGearPDFAtom("Times-Roman");
            // Set the font type.
            findAttrs.Type = new ImGearPDFAtom("Type1");
 
            // Create the font.
            ImGearPDEFont font = new ImGearPDEFont(findAttrs, 0, 255, null, null, null, null, 0, 0, 0);
 
            return font;
        }
 
        // This function returns a new text element that will be added to the PDF content.
        public ImGearPDEText CreateTextElement()
        {
            ImGearPDEText textElement = null;
 
            // Set the color space to DeviceRGB.
            ImGearPDEColorSpace colorSpace = new ImGearPDEColorSpace(new ImGearPDFAtom("DeviceRGB"));
           
            // Set the attributes for the graphic state.
            ImGearPDEGraphicState gState = new ImGearPDEGraphicState();
 
            // Set the color values.
            // R.
            gState.FillColorSpec.Value.Color[0] = 0;
            // G.
            gState.FillColorSpec.Value.Color[1] = 128 * 255;
            // B.
            gState.FillColorSpec.Value.Color[2] = 255 * 255;
 
            gState.MiterLimit = (int)ImGearPDFFixedValues.TEN;
            gState.Flatness = (int)ImGearPDFFixedValues.ONE;
            gState.LineWidth = (int)ImGearPDFFixedValues.ONE;
 
            // Create a transformation matrix of fixed numbers.
            ImGearPDFFixedMatrix textMatrix = new ImGearPDFFixedMatrix();
            textMatrix.A = ImGearPDF.IntToFixed(20);
            textMatrix.D = ImGearPDF.IntToFixed(20);
            textMatrix.H = ImGearPDF.IntToFixed(100);
            textMatrix.V = ImGearPDF.IntToFixed(700);
 
            // Create a string of text.
            string textToAdd = "This is the string of text that will be added";
 
            // Create a font to use with the text element.
            ImGearPDEFont font = CreateFont();
 
            // Create a new text element.
            textElement = new ImGearPDEText();
            // Add the textToAdd into the element using the settings from above.
            textElement.Add(ImGearPDETextFlags.RUN, 0, textToAdd, font, gState, null, textMatrix, null);
 
            return textElement;
        }