To find text or images and replace it in a PDF.
1PDFDoc doc = new PDFDoc(filename);
2ContentReplacer replacer = new ContentReplacer();
3Page page = doc.GetPage(1);
4
5// replace an image on the page
6Rect target_region = page.GetMediaBox();
7Image img = Image.Create(doc, imagename);
8replacer.AddImage(target_region, img.GetSDFObj());
9
10// replace a text placeholder
11replacer.AddString("NAME", "John Smith");
12
13// replace text in a given region
14string text = "hello world";
15replacer.AddText(target_region, text);
16replacer.Process(page);
1PDFDoc doc(filename);
2ContentReplacer replacer;
3Page page = doc.GetPage(1);
4
5// replace an image on the page
6Rect target_region = page.GetMediaBox();
7Image img = Image::Create(doc, imagename);
8replacer.AddImage(target_region, img.GetSDFObj());
9
10// replace a text placeholder
11replacer.AddString("NAME", "John Smith");
12
13// replace text in a given region
14UString text("hello world");
15replacer.AddText(target_region, text);
16replacer.Process(page);
1doc := NewPDFDoc(filename)
2replacer := NewContentReplacer()
3page := doc.GetPage(1)
4
5// replace an image on the page
6targetRegion := page.GetMediaBox()
7img := ImageCreate(doc, imagename)
8replacer.AddImage(targetRegion, img.GetSDFObj())
9
10// replace a text placeholder
11replacer.AddString("NAME", "John Smith")
12
13// replace text in a given region
14replacer.AddText(targetRegion, "Hello World");
15replacer.Process(page)
1PDFDoc doc = new PDFDoc(filename);
2ContentReplacer replacer = new ContentReplacer();
3Page page = doc.getPage(1);
4
5// replace an image on the page
6Rect target_region = page.GetMediaBox();
7Image img = Image.create(doc, imagename);
8replacer.addImage(target_region, img.getSDFObj());
9
10// replace a text placeholder
11replacer.addString("NAME", "John Smith");
12
13// replace text in a given region
14String text = "hello world";
15replacer.addText(target_region, text);
16replacer.process(page);
1async function main() {
2 const doc = await PDFNet.PDFDoc.createFromURL(filename);
3 const replacer = await PDFNet.ContentReplacer.create();
4 const page = await doc.getPage(1);
5
6 // replace an image on the page
7 const target_region = await page.getMediaBox();
8 const img = await PDFNet.Image.createFromURL(doc, imagename);
9 await replacer.addImage(target_region, await img.getSDFObj());
10
11 // replace a text placeholder
12 await replacer.addString('NAME', 'John Smith');
13
14 // replace text in a given region
15 const text = "hello world";
16 await replacer.addText(target_region, text);
17 await replacer.process(page);
18}
19PDFNet.runWithCleanup(main);
1PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
2PTContentReplacer *replacer = [[PTContentReplacer alloc] init];
3PTPage *page = [doc GetPage: 1];
4
5// replace an image on the page
6PTPDFRect * target_region = [page GetMediaBox];
7PTImage *img = [PTImage Create: [doc GetSDFDoc] filename: imagename];
8[replacer AddImage: target_region replacement_image: [img GetSDFObj]];
9
10// replace a text placeholder
11[replacer AddString: @"NAME" replacement_text: @"John Smith"];
12
13// replace text in a given region
14NSString* text = @"hello world";
15[replacer AddText: target_region replacement_text: text];
16[replacer Process: page];
1$doc = new PDFDoc($filename);
2$replacer = new ContentReplacer();
3$page = $doc->GetPage(1);
4
5// replace an image on the page
6$target_region = $page->GetMediaBox();
7$img = Image::Create($doc->GetSDFDoc(), $imagename);
8$replacer->AddImage($target_region, $img->GetSDFObj());
9
10// replace a text placeholder
11$replacer->AddString("NAME", "John Smith");
12
13// replace text in a given region
14$text = "hello world";
15$replacer->AddText($target_region, "hello world");
16$replacer->Process($page);
1doc = PDFDoc(filename)
2replacer = ContentReplacer()
3page = doc.GetPage(1)
4
5# replace an image on the page
6target_region = page.GetMediaBox()
7img = Image.Create(doc.GetSDFDoc(), imagename)
8replacer.AddImage(target_region, img.GetSDFObj())
9
10# replace a text placeholder
11replacer.AddString("NAME", "John Smith")
12
13# replace text in a given region
14text = "hello world"
15replacer.AddText(target_region, "hello world")
16replacer.Process(page)
1doc = PDFDoc.new(filename)
2replacer = ContentReplacer.new()
3page = doc.GetPage(1)
4
5# replace an image on the page
6target_region = page.GetMediaBox()
7img = Image.Create(doc.GetSDFDoc(), imagename)
8replacer.AddImage(page.GetMediaBox(), img.GetSDFObj())
9
10# replace a text placeholder
11replacer.AddString("NAME", "John Smith")
12
13# replace text in a given region
14text = "hello world"
15replacer.AddText(target_region, "hello world")
16replacer.Process(page)
1Dim doc As PDFDoc = New PDFDoc(filename)
2Dim replacer As ContentReplacer = New ContentReplacer()
3Dim page As Page = doc.GetPage(1)
4
5' replace an image on the page
6Dim target_region As Rect = page.GetMediaBox()
7Dim img As Image = Image.Create(doc.GetSDFDoc(), imagename)
8replacer.AddImage(page.GetMediaBox(), img.GetSDFObj())
9
10' replace a text placeholder
11replacer.AddString("NAME", "John Smith")
12
13' replace text in a given region
14Dim text As String = "hello world"
15replacer.AddText(target_region, "hello world")
16replacer.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).
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales