Free Support Forum - aspose.com

Problem with Aspose.PDF for Java

Replaced text still shows on PDF. Example and code in attachment.

Hi Tony,


Thanks for your inquiry. I am afraid your shared document(Notes.pdf) is corrupt. We will appreciate it if you please share your sample code and input/output document here again. We will investigate the issue and provide you information accordingly.

We are sorry for the inconvenience caused.

Best Regards,
@Override<br>    protected Boolean doInBackground() throws Exception {<br>        boolean lbReturn = true;<br><br>        try {<br>            // Check extension.<br>            if (FileUtility.isFileExtension(coSource, "PDF")) {<br>                // Replace text in PDF.<br>                doProcess(coSource);<br>            }<br>        } catch (Exception aoException) {<br>            // Set return.<br>            lbReturn = false;<br>        }<br><br>        return lbReturn;<br>    }<br><br>    /**<br>     * Helper method to replace text in PDF.<br>     * @param aoSource Source file.<br>     */<br>    private void doProcess(File aoSource) {<br>        // Open document.<br>        com.aspose.pdf.Document loDocument = new com.aspose.pdf.Document(aoSource.getPath());<br><br>        // Process file using tag.<br>        doProcess(loDocument);<br><br>        // Save document.<br>        loDocument.save(aoSource.getPath());<br><br>        // Close document.<br>        loDocument.close();<br>    }<br><br>    /**<br>     * Helper method to replace text in file using tags.<br>     * @param aoDocument Source document.<br>     */<br>    @SuppressWarnings("unchecked")<br>    private void doProcess(com.aspose.pdf.Document aoDocument) {<br>        // Set text search option to specify regular expression usage.<br>        com.aspose.pdf.TextSearchOptions loTextSearchOptions = new com.aspose.pdf.TextSearchOptions(true);<br><br>        // Create TextAbsorber object to find all instances of the input search phrase.<br>        com.aspose.pdf.TextFragmentAbsorber loTextFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber("<R=[a-zA-Z0-9]+?>", loTextSearchOptions);<br><br>        // Accept the absorber for first page of document.<br>        aoDocument.getPages().accept(loTextFragmentAbsorber);<br><br>        // Get the extracted text fragments into collection.<br>        com.aspose.pdf.TextFragmentCollection loTextFragmentCollection = loTextFragmentAbsorber.getTextFragments();<br><br>        // Loop through the fragments.<br>        for (com.aspose.pdf.TextFragment textFragment : (Iterable<com.aspose.pdf.TextFragment>)loTextFragmentCollection) {<br>            // Get tag.<br>            String lsTag = textFragment.getText();<br>            // Replacement text.<br>            String lsReplacement = lsTag.substring(lsTag.indexOf('=')+1, lsTag.length()-1);<br>            // Replace text in document.<br>            textFragment.setText(" " + lsReplacement);<br>        }<br>    }

Hi Tony,


Thanks for sharing the sample code and documents. I have tested the scenario using Aspose.Pdf for Java 10.6.1. However it is not keeping the replaced text but searching incorrect text so results in wrong replacement. I have logged a ticket PDFNEWJAVA-35089 in our issue tracking system for further investigation and resolution. We will keep you updated about the issue resolution progress.

We are sorry for the inconvenience caused.

Best Regards,