Aspose for Java

Hello, i have a concern about the product i purchased, i have some issues that would like to comment. http://www.aspose.com/docs/display/pdfjava/Replace+Text+in+Pages+of+a+PDF+Document this link on your website shows how to replace text on all pages, but cpu memory goes at 90 or 100% with only one 5-8 pages pdf, the pdf size is no more than 300kb, so im wondering why is this memory leak. is there a fix?

Hi Fernando,


Thanks for contacting support.

The time taken by API to perform any specific operation depends upon the structure and complexity of source/input PDF document. Can you please share the resource PDF file along with code snippet, so that we can test the scenario in our environment.

We are sorry for this inconvenience.
...
..
pattern = Pattern.compile("name:.*?(\\w+\\s\\w+)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.UNICODE_CASE);
matcher = pattern.matcher(extractedText);
while (matcher.find()) {
String toReplace = matcher.group(1).toString();
//System.out.println(toReplace);
pdfDocument = ReplaceTextInPDF(pdfDocument, toReplace, Spaces, false,extractedText);
...
...



private Document ReplaceTextInPDF(Document pdfDocument, String textToSearch, String textToReplace, boolean isPattern, String FileContent)
{
//Pattern pattern = Pattern.compile(textToSearch);
//Matcher matcher = pattern.matcher(FileContent);
//if (matcher.find())
//{
com.aspose.pdf.TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber(textToSearch);
if (isPattern)
{
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.setTextSearchOptions(textSearchOptions);
}
pdfDocument.getPages().accept(textFragmentAbsorber);
com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
int total = textFragmentCollection.size();
for(int i = 1; i<=total ; i++)
{
TextFragment textFragment = textFragmentCollection.get_Item(i);
textFragment.setText(textToReplace);
}
//}
return pdfDocument;
}


The marked lines are high cpu consuming, when i try to run from 4 to 8 threads running the files, cpu gets 100%

Hi Fernando,

Thanks for sharing the code snippet.

I have tested the scenario with Aspose.Pdf for Java 10.1.0 while using the following code snippet and I am unable to notice any time consumption issue. Can you please share the complete code snippet so that we can again test the scenario in our environment.

[Java]

String extractedText = "TRANSACTION";

Pattern pattern = Pattern.compile("name:.*?(\\w+\\s\\w+)",
    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.UNICODE_CASE);

Matcher matcher = pattern.matcher(extractedText);

while (matcher.find()) {
    String toReplace = matcher.group(1).toString();
    //System.out.println(toReplace);
    com.aspose.pdf.Document pdfDocument = ReplaceTextInPDF(
        new com.aspose.pdf.Document("c:/pdftest/sampleJava.pdf"),
        toReplace, false, extractedText);
}

private static Document ReplaceTextInPDF(Document pdfDocument, String textToSearch, boolean isPattern, String FileContent){
    //Pattern pattern = Pattern.compile(textToSearch);
    //Matcher matcher = pattern.matcher(FileContent);
    //if (matcher.find()) {
    com.aspose.pdf.TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber(textToSearch);
    if (isPattern) {
        com.aspose.pdf.TextSearchOptions textSearchOptions = new com.aspose.pdf.TextSearchOptions(true);
        textFragmentAbsorber.setTextSearchOptions(textSearchOptions);
    }
    pdfDocument.getPages().accept(textFragmentAbsorber);
    com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
    int total = textFragmentCollection.size();
    for (int i = 1; i<=total ; i++) {
        TextFragment textFragment = textFragmentCollection.get_Item(i);
        textFragment.setText("textToReplace");
    }
    //}
    return pdfDocument;
}

Ok, i will upload a portion of code for you to verify that. Also i have another concern. I open a PDF document, and do not do any changes to it, saves to a specific location, and the images dissapear, just text remains. is there a way to avoid this?

Hi Fernando,

I have tested the scenario using the sample file shared in 617249 and I am unable to notice any issue. The images properly appear in the resultant file. Can you please confirm if you are using a similar file or facing a problem with another document?

[Java]

//Open document
Document doc = new Document("c:\\sample(1).pdf");

//Save resultant document
doc.save("c:\\ReSaved_output.pdf");