How do I do string replacement on tokens that are already in the document?

Posted by: jgregory on 21 September 2020, 2:28 am EST

  • Posted 21 September 2020, 2:28 am EST

    I have a system where users can upload a pdf document, where they include tokens to replace with strings.

    How do I do this using the GcPdfDocument, I see the documentation for documents-api-word for find and replace, but not for documents-api-pdf.

    Thanks
  • Replied 21 September 2020, 3:56 pm EST

    Hi James,

    There is no direct way to find and replace the text using GcPdf.
    You can use the Find Text demo sample to find the string:
    https://www.grapecity.com/documents-api-pdf/demos/features/analyze/find-text/code-cs

    I have modified the sample to place the custom text in the place of existing text using the DrawString method of Graphics class.

    Regards,
    Prabhat Sharma.
    FindText.zip
  • Replied 21 September 2020, 8:15 pm EST

    Thank you for providing me with an example.

    How does that work in terms of formatting, will that cause formatting issues on the pdf if a token is replaced and it needs to go to a new line.

    Also I see you have hard coded the font size, I'm guessing its possible to abstract the styling of the token to replace and use that on the new replacement text.

    I think the better solution maybe to allow docx file uploads only, do the string replacement and then convert it to a pdf using your api's.

    Thanks
  • Replied 22 September 2020, 7:21 pm EST

    Hi James,

    >>How does that work in terms of formatting, will that cause formatting issues on the pdf if a token is replaced and it needs to go to a new line.
    Yes, it will cause the formatting issue in this situation, and as I told you that it is just a workaround so it may be possible that it will not work in some scenarios.

    >>I think the better solution maybe to allow docx file uploads only, do the string replacement and then convert it to a pdf using your api's.
    Yes, you can use the GcWord API to do achieve your desired result.

    If you need any other help, please let us know.

    Regards,
    Prabhat Sharma.
  • Replied 19 October 2020, 7:57 pm EST

    I noticed that the GcWordApi is a separate payment plan from the PdfApi, and all I really need is a way of replacing tokens in a pdf file. I am guessing that a full GcWordApi license is going to be required? and this is not achievable with the PdfApi?

    I have tried using replace with the pdf API but I just seem to get loads of formatting issues, especially when dealing with tables, or where there is 2 paragraphs split down the middle of the page.

    If the replacement string is to long it tends to just overlay text that is below when the replacement string wraps.

    Is there any other solutions you can recommend for achieving the desired result?
  • Replied 20 October 2020, 8:21 pm EST

    Hi James,

    Yes, GcWordApi has a separate payment plan from the GcPDf. Please see the following link for more information:
    https://www.grapecity.com/pricing

    We are discussing the issue with the developers and will you know once we get the update.
    [Internal Tracking ID: DOC-2404]

    Regards,
    Prabhat Sharma.
  • Marked as Answer

    Replied 21 October 2020, 3:54 am EST

    Hi Gregory,

    The PDF format does not have a notion of a paragraphs or any "reflowable" text fragments. PDF operators allow to render a line of text at most, no text wrapping is supported. So generally speaking, it is impossible to replace a text fragment in a PDF with an arbitrary fragment (that may be physically larger) without recreating the whole document. While we (GcPdf) do have plans to provide some form of text replacement in the future, it is not here yet and even when done it might not work in all cases - again, if a replacement if physically larger than what it replaces, it may not produce the expected results.

    In your particular case, if you know the physical layout of your PDFs, you might be able to 'wipe out' the text fragments containing your tags (either by filling them with white, or better yet by redacting the areas), and render regenerated texts with tags replaced with custom content, over the old fragments' bounds. In other words, if you have a PDF with a multi-line text paragraph (containing the tags), and know the maximum bounds of that paragraph - using GcPdf you can:
    1) Redact out the area under the max bounds;
    2) Use TextLayout to render the new text (paragraphs) into that area.
    This approach will handle replacements longer than tags well - unless the new paragraph becomes larger than the max area allowed for it, in which case it will be clipped by TextLayout.

    Hope this helps,
    Dmitry.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels