Free Support Forum - aspose.com

Aspose.Pdf replace text is not working

Hi,
I wrote a class that has been wrapping aspose.pdf features to our application. I am trying to replace pdf text to some other text. But it is not replacing the text completely. Capital letters and numbers are not replacing. I tried a lot of various method to resolve this issue.
But it is not working. I have Aspose.Total licence. Can you help me to resolve ?

My sample method to replace text in wrapper class:

public void ReplaceText(string targetText, string sourceText)
{
Document document = new Document(“some_doc.pdf”);
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(targetText);
document.getPages().accept(textFragmentAbsorber);

TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
TextFragment textFragment = textFragmentCollection.get_Item(1);

textFragment.setText(sourceText);

document.save(“new_doc.pdf”);
}

I am also attaching the source pdf file.
In the pdf file i want to replace text “name123” to “NAME123”.

Hi Sumilon,


Thanks for using our API’s.

I have tested the scenario and I am able to
notice the same problem. For the sake of correction, I have logged this problem
as PDFNEWJAVA-35204 in our issue tracking system. We will
further look into the details of this problem and will keep you updated on the
status of correction. Please be patient and spare us little time. We are sorry
for this inconvenience.

Hi,


I have the same issue. I’m using a REST API in PHP to replace text in a PDF, but it doesn’t replace capitals, numbers and special characters. It does replace lower case characters however. Also, Acrobat Reader tells me the PDF contains an error.

Any news on how to fix this?

Hi,


I’m using a REST API in PHP to replace text in a PDF, but it doesn’t replace capitals, numbers or special characters. It does replace lower case characters however. Also, Acrobat Reader tells me the PDF contains an error. The API method I’m using is ‘replaceTextList’.

Can somebody help me with this?

I found a similar issue on this forum. The issue was assigned to PDFNEWJAVA-35204

Hi Roel,


Thanks for contacting support.

The earlier discussed issue in this thread is logged against Aspose.Pdf for Java. However you appear to be using Aspose.Pdf for Cloud and the text update/replace related scenarios appear to be document specific, therefore we request you to please share your input document and code snippet which you are using, so that we can further test this scenario in our environment. We are sorry for this inconvenience.
Thank you for your response. I attached the input file (Contracttemplate_test.pdf) and output file (contract_template1476362223.pdf).

PHP code:
-------------
// Replace text
$fieldsArray = array('TextReplaces' => array(
array('OldValue' => '', 'NewValue' => 'Roel Jansen', 'Regex' => 'false'),
array('OldValue' => '', 'NewValue' => date('d-m-Y'), 'Regex' => 'false'),
array('OldValue' => '', 'NewValue' => '1234,56', 'Regex' => 'false'),
));
$json = json_encode($fieldsArray);

$fileName = 'Contracttemplate_test.pdf';

$strURI = "http://api.aspose.com/v1.1/pdf/" . $fileName . "/replaceTextList";
$signedURI = $this->sign($strURI, $sAppID, $sAppKey);
$responseStream = $this->processCommand($signedURI, "POST", "json", $json);

// Save PDF
$strURI = "http://api.aspose.com/v1.1/storage/file/" . $fileName;
$signedURI = $this->sign($strURI, $sAppID, $sAppKey);
$responseStream = $this->processCommand($signedURI, "GET", "", "");
...
-------------

Hi Roel,


Thanks for sharing the details.

We are testing the scenario and will get back to you soon.

RoelTS:
Thank you for your response. I attached the input file (Contracttemplate_test.pdf) and output file (contract_template1476362223.pdf).

PHP code:
-------------
// Replace text
$fieldsArray = array(‘TextReplaces’ => array(
array(‘OldValue’ => ‘’, ‘NewValue’ => ‘Roel Jansen’, ‘Regex’ => ‘false’),
array(‘OldValue’ => ‘’, ‘NewValue’ => date(‘d-m-Y’), ‘Regex’ => ‘false’),
array(‘OldValue’ => ‘’, ‘NewValue’ => ‘1234,56’, ‘Regex’ => ‘false’),
));
$json = json_encode($fieldsArray);

$fileName = ‘Contracttemplate_test.pdf’;

$strURI = “http://api.aspose.com/v1.1/pdf/” . $fileName . “/replaceTextList”;
$signedURI = $this->sign($strURI, $sAppID, $sAppKey);
$responseStream = $this->processCommand($signedURI, “POST”, “json”, $json);

// Save PDF
$strURI = “http://api.aspose.com/v1.1/storage/file/” . $fileName;
$signedURI = $this->sign($strURI, $sAppID, $sAppKey);
$responseStream = $this->processCommand($signedURI, “GET”, “”, “”);
-------------
Hi Roel,

Thanks for the intimation.

I have logged the above stated problem as an investigation ticket in our issue tracking system as PDFCLOUD-38. We will further look into the details of this problem and will keep you updated on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

@sumilon @RoelTS

Thank you for being patient.

We have investigated the ticket reported by you PDFJAVA-35204 and would like to update you that, the embedded Arial font is subset and does not contain characters “H”, “A”, “N”, “K” and “S”. Chars are present, we are able to select them and copy, but we cannot see them because the glyph information in the current embedded font is absent for some characters. The characters will appear if we do not use the embedded font and it will be present on the system:

textFragment.getTextState().getFont().setEmbedded(false);

You can also embed all used fonts:

for(Font f:document.getFontUtilities().getAllFonts())
{
    f.setEmbedded(true);
}

We hope this will be helpful. Please feel free to contact us if you need any further assistance.