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.
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; } |
VB.NET |
Copy Code |
---|---|
Imports System.IO Imports System.Windows.Forms Imports ImageGear.Core Imports ImageGear.Formats Imports ImageGear.Formats.PDF ' This function will add a text element into the content of the PDF document. Public Sub AddTextToPDFContent(pdfDocument As ImGearPDFDocument) ' Select the first page of the current document. Dim page As ImGearPDFPage = DirectCast(pdfDocument.Pages(0), ImGearPDFPage) ' Get the content of the first page of the PDF document. Using content As ImGearPDEContent = (page).GetContent() ' Get the count of the elements on the page. Dim elementCount As Integer = content.ElementCount ' Make a new text element to add to the page content. Dim textElement As ImGearPDEText = CreateTextElement() ' Insert a Text Element into the content. content.AddElement(CInt(ImGearPDEInsertElement.AFTER_LAST), textElement) ' Set the page's PDE content back into the page object. page.SetContent() End Using End Sub ' This function returns a new font to use for the text element. Public Function CreateFont() As ImGearPDEFont ' Create a ImGearPDEFontAttrs to get the attributes for ImGearPDEFont and ImGearPDFSysFont. Dim findAttrs As New ImGearPDEFontAttrs() ' Set the font name. findAttrs.Name = New ImGearPDFAtom("Times-Roman") ' Set the font type. findAttrs.Type = New ImGearPDFAtom("Type1") ' Create the font. Dim font As New ImGearPDEFont(findAttrs, 0, 255, Nothing, Nothing, Nothing, _ Nothing, 0, 0, 0) Return font End Function ' This function returns a new text element that will be added to the PDF content. Public Function CreateTextElement() As ImGearPDEText Dim textElement As ImGearPDEText = Nothing ' Set the color space to DeviceRGB. Dim colorSpace As New ImGearPDEColorSpace(New ImGearPDFAtom("DeviceRGB")) ' Set the attributes for the graphic state. Dim gState As 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 = CInt(ImGearPDFFixedValues.TEN) gState.Flatness = CInt(ImGearPDFFixedValues.ONE) gState.LineWidth = CInt(ImGearPDFFixedValues.ONE) ' Create a transformation matrix of fixed numbers. Dim textMatrix As 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. Dim textToAdd As String = "This is the string of text that will be added" ' Create a font to use with the text element. Dim font As ImGearPDEFont = 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, Nothing, _ textMatrix, Nothing) Return textElement End Function |