Read and Change Page Objects | |
SelectPdf Library for .NET reads and changes what is drawn on the pages of an existing PDF document through ObjectModel: every text, image, path and group of a page, in the order it is painted, with its position, colours and opacity. Objects can be moved, rotated, resized, removed, duplicated, brought to the front or sent to the back, recoloured, and text can be replaced. Changes are written when the document is saved.
Coordinates are in points with the origin at the top-left corner of the page as it is displayed (after its rotation), like every other coordinate of the library. Each change is applied to the page at once; objects obtained earlier keep pointing at the same content after changes made through the model.
A text object holds the text drawn by one text instruction of the page. Documents created with the library keep each line or paragraph together, while documents converted from HTML, and many others, usually draw each character separately, so a word spans several text objects there.
PdfTextObject - the text, the font name and size, whether the font is embedded and how the glyphs are painted. ReplaceText writes new text from the same starting point, in the object's font where it can draw every character, else in an installed font that can.
PdfImageObject - the pixel size and bits per component; GetImageBytes returns the picture as PNG.
PdfPathObject - lines, rectangles and curves: filled or outlined, the line width and the points.
PdfFormObject - a group of objects drawn together; its children follow it in the page's list and are in its own Objects.
Every object has Bounds, FillColor, StrokeColor, Opacity and Layer, and the methods Translate, Rotate, Scale, Remove, Duplicate, BringToFront and SendToBack. The model finds objects with HitTest, GetObjectsInRectangle and GetObjectsInLayer.
The object model is available for the pages of a loaded document, in the Full edition.
PdfDocument document = new PdfDocument("in.pdf"); PdfPageObjectModel model = document.Pages[0].ObjectModel; // list the text of the page foreach (PdfTextObject text in model.GetTextObjects()) Console.WriteLine(text.Text + " at " + text.Bounds); // replace a title, move the logo and remove the other images PdfTextObject title = model.GetTextObjects()[0]; title.ReplaceText("Annual Report 2026"); title.FillColor = new PdfColor(0, 70, 140); PdfImageObject[] images = model.GetImageObjects(); images[0].Translate(0, 20); for (int i = 1; i < images.Length; i++) images[i].Remove(); document.Save("out.pdf"); document.Close();