Replacing text & images in PDFs on Server/Desktop
To find text or images and replace it in a PDF.
PDFDoc doc = new PDFDoc(filename);
ContentReplacer replacer = new ContentReplacer();
Page page = doc.GetPage(1);
// replace an image on the page
Rect target_region = page.GetMediaBox();
Image img = Image.Create(doc, imagename);
replacer.AddImage(target_region, img.GetSDFObj());
// replace a text placeholder
replacer.AddString("NAME", "John Smith");
// replace text in a given region
string text = "hello world";
replacer.AddText(target_region, text);
replacer.Process(page);PDFDoc doc(filename);
ContentReplacer replacer;
Page page = doc.GetPage(1);
// replace an image on the page
Rect target_region = page.GetMediaBox();
Image img = Image::Create(doc, imagename);
replacer.AddImage(target_region, img.GetSDFObj());
// replace a text placeholder
replacer.AddString("NAME", "John Smith");
// replace text in a given region
UString text("hello world");
replacer.AddText(target_region, text);
replacer.Process(page);Replace PDF text or images Full code sample which shows how to use pdftron.PDF.ContentReplacer to search and replace text strings and images in existing PDF (e.g. business cards and other PDF templates). Code sample is available in C++, C#, Java, Python, Go, PHP, Ruby & VB.
Last updated
Was this helpful?