Class: ContentReplacer

Core.PDFNet. ContentReplacer


new ContentReplacer()

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);

Extends

Methods


<static> create()

Create a new ContentReplacer object, to which replacement rules will be added. The same object can be used to 'Process' multiple pages.
Returns:
A promise that resolves to an object of type: "PDFNet.ContentReplacer"
Type
Promise.<Core.PDFNet.ContentReplacer>

addImage(target_region, replacement_image)

Replace the image that best fits into 'target_region' with 'replacement_image'.
Parameters:
Name Type Description
target_region Core.PDFNet.Rect The rectangle defining the area in which an image that best fits the rectangle will be replaced by 'replacement_image'.
replacement_image Core.PDFNet.Obj The 'SDF.Obj' of a 'PDF.Image' object. 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.
Returns:
Type
Promise.<void>

addString(template_text, replacement_text)

Any text of the form "[template_text]" will be replaced by "replacement_text".
Parameters:
Name Type Description
template_text string The text to remove.
replacement_text string The new text that will appear in place of 'template_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".
Returns:
Type
Promise.<void>

addText(target_region, replacement_text)

All text inside 'target_region' will be deleted and replaced with 'replacement_text'.
Parameters:
Name Type Description
target_region Core.PDFNet.Rect The rectangle defining the area in which all text will be replaced by 'replacement_text'.
replacement_text string The new text that will replace the existing text in 'target_region'. 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.
Returns:
Type
Promise.<void>

destroy()

Destructor
Inherited From:
Returns:
Type
Promise.<void>

process(page)

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.
Parameters:
Name Type Description
page Core.PDFNet.Page The page to apply the content replacement instructions to.
Returns:
Type
Promise.<void>

setMatchStrings(start_str, end_str)

Change the delimiters from '[' and ']' to arbitary strings.
Parameters:
Name Type Description
start_str string The starting delimiter string.
end_str string The ending delimiter string. 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 "<>" with "Doctor". Similarly, after SetMatchStrings("Beginning...", "...ending."), AddString("TITLE", "Doctor") will replace "Beginning...TITLE...ending." with "Doctor".</td> </tr> </tbody> </table> <dl class="details"> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">Promise.<void></span> </dd> </dl> </dd> <hr> <dt> <h4 class="name" id="takeOwnership"><span class="type-signature"></span>takeOwnership()</h4> </dt> <dd> <div class="description"> Take the ownership of this object, so that PDFNet.runWithCleanup won't destroy this object. </div> <dl class="details"> <dt class="inherited-from method-doc-label method-doc-details-label">Inherited From:</dt> <dd class="inherited-from"> <ul class="dummy"> <li> <a href="Core.PDFNet.Destroyable.html#takeOwnership">Core.PDFNet.Destroyable#takeOwnership</a> </li> </ul> </dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">void</span> </dd> </dl> </dd> </dl> </article> </section> </div> </div> <div class="clearfix"></div> <div class="col-md-3"> <div id="toc" class="col-md-3 hidden-xs hidden-sm hidden-md"></div> </div> </div> </div> <div class="modal fade" id="searchResults"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title">Search results</h4> </div> <div class="modal-body"></div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div> <footer> <span class="jsdoc-message"> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.11</a> on 2023-02-16T10:58:44-08:00 using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>. </span> </footer> <script src="scripts/docstrap.lib.js"></script> <script src="scripts/toc.js"></script> <script type="text/javascript" src="scripts/fulltext-search-ui.js"></script> <script> $( function () { $( "[id*='$']" ).each( function () { var $this = $( this ); $this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) ); } ); $( ".tutorial-section pre, .readme-section pre, pre.prettyprint.source" ).each( function () { var $this = $( this ); var example = $this.find( "code" ); exampleText = example.html(); var lang = /{@lang (.*?)}/.exec( exampleText ); if ( lang && lang[1] ) { exampleText = exampleText.replace( lang[0], "" ); example.html( exampleText ); lang = lang[1]; } else { var langClassMatch = example.parent()[0].className.match(/lang\-(\S+)/); lang = langClassMatch ? langClassMatch[1] : "javascript"; } if ( lang ) { $this .addClass( "sunlight-highlight-" + lang ) .addClass( "linenums" ) .html( example.html() ); } } ); Sunlight.highlightAll( { lineNumbers : false, showMenu : true, enableDoclinks : true } ); $.catchAnchorLinks( { navbarOffset: 10 } ); $( "#toc" ).toc( { anchorName : function ( i, heading, prefix ) { return $( heading ).attr( "id" ) || ( prefix + i ); }, selectors : "#toc-content h1,#toc-content h2,#toc-content h3,#toc-content h4", showAndHide : false, smoothScrolling: true } ); $( "#main span[id^='toc']" ).addClass( "toc-shim" ); $( '.dropdown-toggle' ).dropdown(); $( "table" ).each( function () { var $this = $( this ); $this.addClass('table'); } ); } ); </script> <!--Navigation and Symbol Display--> <!--Google Analytics--> <script> </script> <script type="text/javascript"> $(document).ready(function() { SearcherDisplay.init(); }); </script> </body> </html>