![]() | Working With Page Objects |
This topic contains the following sections:
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.
PDF provides five types of graphics objects:
The PdfPathObject The PdfPathObject is an arbitrary shape made up of straight lines, rectangles, and cubic Bézier curves. A path may intersect itself and may have disconnected sections and holes.
The PdfTextObject consists of one character string that identify sequences of glyphs to be painted. Like a path, text can be stroked, filled, or used as a clipping boundary.
The PdfShadingObject describes a geometric shape whose color is an arbitrary function of position within the shape. A shading can also be treated as a color when painting other graphics objects; it is not considered to be a separate graphics object in that case.
The PdfImageObject consist of a rectangular array of color samples to be painted;
The PdfFormObject is used to group graphical elements together as a unit for various purposes.
// ... 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. } } }
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(); }
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(); } } }