Click or drag to resize

Page Objects

This topic contains the following sections:

Overview

Page object is a feature that allows developers to work with text, path, image objects. Patagames PDF SDK provides APIs to add and delete PDF objects in a page and set specific attributes. Using page object, developers can create PDF page from object contents. Other possible usages of page object include adding headers and footers to PDF documents, adding an image logo to each page, or generating a custom view of annotations.

Open in full size

Page Objects
Enumerate all page objects on a PDF page
C#
    // ...
    using (var doc = PdfDocument.Load("sample.pdf"))
    {
        PdfPage page = doc.Pages[0];
        //Recursively list and process all page objects on the page.
        EnumerateAllPageObjects(page.PageObjects);
    }
    // ...

public void EnumerateAllPageObjects(PdfPageObjectsCollection collection)
{
    foreach (PdfPageObject obj in collection)
    {
        if (obj is PdfFormObject)
        {
            var formObject = obj as PdfFormObject;
            EnumerateAllPageObjects(formObject.PageObjects);
        }
        else if (obj is PdfTextObject)
        {
            var textObject = obj as PdfTextObject;
            // process text object.
        }
        else if (obj is PdfImageObject)
        {
            var imageObject = obj as PdfImageObject;
            // process image object.
        }
        else if (obj is PdfPathObject)
        {
            var pathObject = obj as PdfPathObject;
            // process path object.
        }
        else if (obj is PdfShadingObject)
        {
            var shadingObject = obj as PdfShadingObject;
            // process shading object.
        }
    }
}
Create a text object in a PDF page
C#
using (var doc = PdfDocument.Load("sample.pdf"))
{
    PdfPage page = doc.Pages[0];

    // Vertical and horizontal position in user space coordinate system
    float xPos = 50;
    float yPos = 150;

    // Create fonts used for text objects
    PdfFont font = PdfFont.CreateStock(doc, FontStockNames.Arial);
    float fontSize = 12;

    // Create text oject and add it on the page
    PdfTextObject textObject = PdfTextObject.Create("Sample text", xPos, yPos, font, fontSize);
    page.PageObjects.Add(textObject);

    //Generate page content
    page.GenerateContent();
}
Create an image object in a PDF page
C#
using (var doc = PdfDocument.Load("sample.pdf"))
{
    PdfPage page = doc.Pages[0];

    // Vertical and horizontal position in user space coordinate system
    float xPos = 50;
    float yPos = 150;

    // Create a System.Drawing.Bitmap from the specified file.
    using (var image = Bitmap.FromFile("sample.png", true) as Bitmap)
    {
        // Calculate size of image in PDF points
        FS_SIZEF size = new FS_SIZEF(
            image.Width * 72 / image.HorizontalResolution,
            image.Height * 72 / image.VerticalResolution);

        //Create a PdfBitmap from the specified image.
        using (PdfBitmap pdfBitmap = PdfBitmap.FromBitmap(image))
        {
            // Create image oject and add it on the page
            var imageObject = PdfImageObject.Create(doc, pdfBitmap, xPos, yPos);
            page.PageObjects.Add(imageObject);

            //set image matrix
            imageObject.Matrix = new FS_MATRIX(size.Width, 0, 0, size.Height, 0, 0);

            //Generate page content
            page.GenerateContent();
        }
    }
}