Read and Change Page Objects |
ExpertPdf 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, colors and opacity. Objects can be moved, rotated, resized, removed, duplicated, brought to the front or sent to the back, recolored, 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). 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.
TextObject - 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.
ImageObject - the pixel size; GetImageBytes returns the picture as PNG.
PathObject - lines, rectangles and curves: filled or outlined, the line width and the points.
FormObject - a group of objects drawn together.
The object model is available for the pages of a loaded document.
Document document = new Document("in.pdf"); PageObjectModel model = document.Pages[0].ObjectModel; foreach (TextObject text in model.GetTextObjects()) Console.WriteLine(text.Text + " at " + text.Bounds); TextObject title = model.GetTextObjects()[0]; title.ReplaceText("Annual Report 2026"); title.FillColor = new PdfColor(0, 70, 140); model.GetImageObjects()[0].Translate(0, 20); document.Save("out.pdf"); document.Close();