Translate PDF Objects in C#
There are times when users need to move a particular text, image, or shape within a PDF to another location. With IronPDF, we offer ways for developers to directly access a PDF object and translate it using our library.
Within this code example, we'll go through how you can translate objects with the PDF DOM easily with IronPDF.
5-Step Code to translate PDF Objects
- ChromePdfRenderer renderer = new ChromePdfRenderer();
- PdfDocument pdf = renderer.RenderHtmlAsPdf("Test");
- var objects = pdf.Pages.First().ObjectModel.TextObjects.First();
- objects.Translate = new System.Drawing.PointF(100,-100);
- pdf.SaveAs("translated.pdf");
Code Explanation
We first instantiate a ChromePdfRenderer
and then call RenderHtmlAsPdf
to render an HTML string into a PDF.
After rendering the text, we then access the PDF DOM object collection. We get the first page using Pages.First
, then access its ObjectModel
to find the TextObjects
collection. The TextObjects
collection holds all TextObject
instances within the PDF. We access the first element in this collection and assign it to the variable.
After obtaining the first TextObject
within the PDF (in our example, this would be the word "Test"), we then assign a new PointF(X, Y)
to the Translate
property. By assigning a new point, the position of the word shifts 100 points to the right and 100 points down along the X and Y axes.
Finally, after the text has been moved, we utilize the SaveAs
method to save the modified PDF.
Discover How to Effortlessly Translate PDF DOM - Visit Our Guide Now!