PTContentReplacer

@interface PTContentReplacer : NSObject

ContentReplacer is a utility class for replacing content (text and images) in existing PDF (template) documents.

Users can replace content in a PDF page using the following operations:

  • Replace an image that exists in a target rectangle with a replacement image.
  • Replace text that exists in a target rectangle with replacement text.
  • Replace all instances of a specially marked string with replacement string.

The following code replaces an image in a target region. This code also replaces the text [NAME] and [JOB_TITLE] with John Smith and Software Developer respectively. Notice the square braces (‘[’ and ‘]’) on the target strings in the original PDFDoc. These square braces are not included in the actual function calls below, as they’re implicitly added.

 PDFDoc doc("../../TestFiles/BusinessCardTemplate.pdf");
 doc.InitSecurityHandler();
 ContentReplacer replacer;
 Page page = doc.GetPage(1);
 Image img = Image::Create(doc, "../../TestFiles/peppers.jpg");
 replacer.AddImage(page.GetMediaBox(), img.GetSDFObj());
 replacer.AddString("NAME", "John Smith");
 replacer.AddString("JOB_TITLE", "Software Developer");
 replacer.Process(page);
  • Create a new ContentReplacer object, to which replacement rules will be added. The same object can be used to ‘Process’ multiple pages.

    Declaration

    Objective-C

    - (instancetype)init;

    Swift

    init!()
  • Replace the image that best fits into ‘target_region’ with ‘replacement_image’.

    Note

    The best fit is the image that closest matches ‘target_region’. For example if there are two images on the page, one taking up all of the page, and the other smaller, and the smaller one has similar dimensions and position of ‘target_region’, then the smaller image would be replaced, not the larger. Furthermore, if ‘target_region’ encloses multiple images, then only the image with the largest area in ‘target_region’ will be replaced.

    Declaration

    Objective-C

    - (void)AddImage:(PTPDFRect *)target_region
        replacement_image:(PTObj *)replacement_image;

    Swift

    func addImage(_ target_region: PTPDFRect!, replacement_image: PTObj!)

    Parameters

    target_region

    - The rectangle defining the area in which an image that best fits the rectangle will be replaced by ‘replacement_image’.

    replacement_image

    - The ‘SDF.Obj’ of a ‘PDF.Image’ object.

  • All text inside ‘target_region’ will be deleted and replaced with ‘replacement_text’.

    Note

    The ‘replacement_text’ will be styled in the same font/color/style that is used by the original text. If there are multiple font styles, the most prevalent style will be used. Also, the ‘replacement_text’ will wrap within the ‘target_region’, but if it is too long, the overflow text will not be visible, and no surrounding content will be affected.

    Declaration

    Objective-C

    - (void)AddText:(PTPDFRect *)target_region
        replacement_text:(NSString *)replacement_text;

    Swift

    func addText(_ target_region: PTPDFRect!, replacement_text: String!)

    Parameters

    target_region

    - The rectangle defining the area in which all text will be replaced by ‘replacement_text’.

    replacement_text

    - The new text that will replace the existing text in ‘target_region’.

  • Any text of the form [template_text] will be replaced by replacement_text.

    Note

    Only text wrapped in ‘[’ and ‘]’ will be checked, and if it matches ‘template_text’, then ‘template_text’ and the surrounding square braces will be replaced by ‘replacement_text’. For example AddString(TITLE, Doctor) will replace any text consisting of [TITLE] with Doctor.

    Declaration

    Objective-C

    - (void)AddString:(NSString *)template_text
        replacement_text:(NSString *)replacement_text;

    Swift

    func add(_ template_text: String!, replacement_text: String!)

    Parameters

    template_text

    - The text to remove.

    replacement_text

    - The new text that will appear in place of ‘template_text’.

  • Change the delimiters from ‘[’ and ‘]’ to arbitary strings.

    Note

    While empty strings are allowed as delimiters, a warning is displayed. Otherwise there are no restrictions. For example, after SetMatchStrings(<<, >>), AddString(TITLE, Doctor) will replace any text consisting of <></q> with <q>Doctor</q>. Similarly, after SetMatchStrings(<q>Beginning…</q>, <q>…ending.</q>), AddString(<q>TITLE</q>, <q>Doctor</q>) will replace <q>Beginning…TITLE…ending.</q> with <q>Doctor</q>.</p> </div> </div> <div class="declaration"> <h4>Declaration</h4> <div class="language"> <p class="aside-title">Objective-C</p> <pre class="highlight objective_c"><code><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">SetMatchStrings</span><span class="p">:(</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">start_str</span> <span class="nf">end_str</span><span class="p">:(</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">end_str</span><span class="p">;</span></code></pre> </div> <div class="language"> <p class="aside-title">Swift</p> <pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">setMatchStrings</span><span class="p">(</span><span class="n">_</span> <span class="nv">start_str</span><span class="p">:</span> <span class="kt">String</span><span class="o">!</span><span class="p">,</span> <span class="nv">end_str</span><span class="p">:</span> <span class="kt">String</span><span class="o">!</span><span class="p">)</span></code></pre> </div> </div> <div> <h4>Parameters</h4> <table class="graybox"> <tbody> <tr> <td> <code> <em>start_str</em> </code> </td> <td> <div> <p>- The starting delimiter string.</p> </div> </td> </tr> <tr> <td> <code> <em>end_str</em> </code> </td> <td> <div> <p>- The ending delimiter string.</p> </div> </td> </tr> </tbody> </table> </div> </section> </div> </li> <li class="item"> <div> <code> <a name="/c:objc(cs)PTContentReplacer(im)Process:"></a> <a name="//apple_ref/objc/Method/-Process:" class="dashAnchor"></a> <a class="token" href="#/c:objc(cs)PTContentReplacer(im)Process:">-Process:</a> </code> </div> <div class="height-container"> <div class="pointer-container"></div> <section class="section"> <div class="pointer"></div> <div class="abstract"> <p>Apply the replacement instructions to the target page. Subsequent calls to ‘Process’ can be made on other pages, and it will apply the same rules.</p> </div> <div class="declaration"> <h4>Declaration</h4> <div class="language"> <p class="aside-title">Objective-C</p> <pre class="highlight objective_c"><code><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">Process</span><span class="p">:(</span><span class="n"><a href="../Classes/PTPage.html">PTPage</a></span> <span class="o">*</span><span class="p">)</span><span class="nv">page</span><span class="p">;</span></code></pre> </div> <div class="language"> <p class="aside-title">Swift</p> <pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">process</span><span class="p">(</span><span class="n">_</span> <span class="nv">page</span><span class="p">:</span> <span class="kt"><a href="../Classes/PTPage.html">PTPage</a></span><span class="o">!</span><span class="p">)</span></code></pre> </div> </div> <div> <h4>Parameters</h4> <table class="graybox"> <tbody> <tr> <td> <code> <em>page</em> </code> </td> <td> <div> <p>- The page to apply the content replacement instructions to.</p> </div> </td> </tr> </tbody> </table> </div> </section> </div> </li> </ul> </div> </section> </section> <section id="footer"> <p>© 2020 <a class="link" href="https://www.pdftron.com/" target="_blank" rel="external">PDFTron Systems, Inc.</a>. All rights reserved. (Last updated: 2020-06-17)</p> </section> </article> </div> </body> </div> </html>