Search and replace text in PDF without using storage

Hello,

I am looking to search and replace text within a PDF but without using storage. Is this possible?

Submitted from: https://apps.saaspose.com/

Hi Olly,

Find and replace text within a PDF file without using storage is not supported by Saaspose.Pdf at the moment but this issue has been logged into our issue tracking system as SAASPDF-82 and we will update you as soon as this feature is implemented. We’re sorry for your inconvenience.

Please let us know in case of further comments or questions.

Regards,
Imran Rafique
Support Developer, Saaspose
Support Team
http://www.saaspose.com

Thanks!

I have tried to use it with storage (as it doesn't really matter) but your
PHP SDK examples don't seem to work?

The specific problem I am having is with the following code:

include $_SERVER['DOCUMENT_ROOT'] .
'/library/ThirdParty/saaspose/saaspose.php';
$folder = Folder();
$folder->UploadFile($_SERVER['DOCUMENT_ROOT']."/../contract/original/".$filename.".pdf","");
$pdfTextEditor = TextEditor($filename.".pdf");
$pdfTextEditor->ReplaceText("text-to-remove","",false);

However I am getting the following error:

Fatal error: Call to undefined function Folder()

Has the SDK changed? This has been copied from your documentation.

Regards
Olly Culverhouse

Centation
32A Downend Road
Downend
Bristol
BS16 5UJ

T: 0117 911 7913
http://www.centation.com/

Thanks!

I have tried to use it with storage (as it doesn’t really matter) but your
PHP SDK examples don’t seem to work?
On 20 Mar 2013 08:43, “Support” <
notifications-support@saaspose.zendesk.com > wrote:

**

I have actually managed to solve this issue and I am no longer getting any
errors.

However

It isn't replacing the text in the PDF. I have looked in the 'History' and
have seen a line mentioning the 'slides' API (which I don't use at all).
See attached image. The PDF I am getting back is identical to the one that
I uploaded.

My code is as follows:

$folder = new Folder();
$folder->UploadFile("tes.pdf","");
$pdfTextEditor = new TextEditor("tes.pdf");
$pdfTextEditor->ReplaceText("December","replaced-text",false);

$fileName = "tes.pdf";
$new = "tes2.pdf";
$output = $new;
$folder = new Folder();
if($folder->FileExists($fileName)) {
$fileStream = $folder->GetFile($fileName);
Utils::saveFile($fileStream, $output);
echo "File saved as " . $output . "
";
}

Any ideas?

Regards
Olly Culverhouse

Centation
32A Downend Road
Downend
Bristol
BS16 5UJ

T: 0117 911 7913
http://www.centation.com

> The specific problem I am having is with the following code:
>
> include $_SERVER['DOCUMENT_ROOT'] .
> '/library/ThirdParty/saaspose/saaspose.php';
> $folder = Folder();
>
> $folder->UploadFile($_SERVER['DOCUMENT_ROOT']."/../contract/original/".$filename.".pdf","");
> $pdfTextEditor = TextEditor($filename.".pdf");
> $pdfTextEditor->ReplaceText("text-to-remove","",false);
>
> However I am getting the following error:
>
> 'Fatal error: Call to undefined function Folder()'
>
> Has the SDK changed? This has been copied from your documentation.
>
>
> Regards
> Olly Culverhouse
>
> Centation
> 32A Downend Road
> Downend
> Bristol
> BS16 5UJ
>
> T: 0117 911 7913
> http://www.centation.com
>
>
> On 20 March 2013 08:56, Olly Culverhouse olly@centation.com wrote:
>
>> Thanks!
>>
>> I have tried to use it with storage (as it doesn't really matter) but
>> your PHP SDK examples don't seem to work?
>> On 20 Mar 2013 08:43, "Support" <
>> notifications-support@saaspose.zendesk.com wrote:
>>
>>> **
>>>

Hi Olly,

Thanks for the information. This is a PHP error, not the Saaspose exception. I’m able to reproduce the exception message when I tried your source code on my side. You need to update your source code like:

Incorrect: $folder = Folder();
Correct: $folder = new Folder();

Also, please follow up source code as narrated in these examples:

I hope this will help you. Please let us know in case of further assistance and comments.

Regards,
Imran Rafique
Support Developer, Saaspose
Support Team
http://www.saaspose.com

Hi,

Yea sorry, I actually said that I worked out what that problem was. Can you
please see my previous update regarding the fact that it seems to work but
nothing is replaced.

Thanks!

Regards
Olly Culverhouse

Centation
32A Downend Road
Downend
Bristol
BS16 5UJ

T: 0117 911 7913
http://www.centation.com

Hi Olly,

I’m sorry about missing your last post. I would like to update you that the UploadFile method takes two parameters. In the first parameter, you are passing only a file name “tes.pdf” but you need to pass a file name with a complete path e.g. “c:\temp\tes.pdf”. I’ve tested the source code as described here: https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-php/blob/master/tests/Aspose/PDF/PdfApiTest.php#L5261 and it is working fine on my side.

If you think it is not working for you then please share the problematic Pdf document here in this thread. We will take a closer look and guide you accordingly.

Regards,
Imran Rafique
Support Developer, Saaspose
Support Team
http://www.saaspose.com

Hi Imran,

Thanks for the response.

I think it replaces the text however whenever I try and download the
changed PDF it gives me the original PDF (not the one with the replaced
text).
I have been basing it from
Working with Files and Storage using Aspose.PDF Cloud|Documentation and my
code is as follows:

$fileName = "blah.pdf";
$outputPath = "/var/www/general/saaspose/replace/out/" . $fileName;
$folder = new Folder();
if($folder->FileExists($fileName)) {
$fileStream = $folder->GetFile($fileName);
Utils::saveFile($fileStream, $outputPath);
echo "File saved as " . $outputPath . "
";
} else {
echo "File does not exist
";
}

After I run it I get the following message:
File saved as /var/www/general/saaspose/replace/out/blah.pdf however it
is identical to the original with none of the replaced text.

The replaced text command should have changed all letter ‘e’ to ‘1’.
However as mentioned above, nothing has changed.

Thanks for your help! :slight_smile:

Regards
Olly Culverhouse

Centation
32A Downend Road
Downend
Bristol
BS16 5UJ

T: 0117 911 7913
http://www.centation.com

Hi Olly,

Thanks for the information. I would like to update you that after calling a ReplaceText method, you do not need to download output Pdf file by using the additional source code. You just need to set the file output location e.g. SaasposeApp::$OutPutLocation = "c:\\temp\\". ReplaceText method will download and save the output pdf file in the appropriate location with the same file name. Please take a look on the source code of ReplaceText method here:
https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-php/blob/master/tests/Aspose/PDF/PdfApiTest.php#L5261

Also, you can download a REST API example from Aspose.Pdf Cloud - API References where you do not need to use Saaspose PHP SDK. There is a single file that includes the required PHP source code. I hope this will help you.

Regards,
Imran Rafique
Support Developer, Saaspose
Support Team
http://www.saaspose.com

Thanks!

After a bit of fiddling with the REST client I have got further so that now
the PDF is created however the file size was 0 and it wasn't a valid PDF
file.
I echoed out the CURL response and I got '"Incorect file format"'. I can
assure you it is a PDF, it is attached.

This is now not using the SDK but I have downloaded that REST example at
https://docs.aspose.cloud/pdf/replace-occurrences-of-a-text-in-a-pdf-document/.
I haven't made any changes to it.

Any ideas?

Thanks!

Regards
Olly Culverhouse

Centation
32A Downend Road
Downend
Bristol
BS16 5UJ

T: 0117 911 7913
http://www.centation.com

Hi Olly,

Thanks for sharing the output PDF file. Did you try any other PDF file sample? I suspect that your input PDF file is signed. The document will become invalid if you will change a signed document. When I tried this REST API example on my side, I was able to replace text using my sample PDF document. I have attached sample input/output PDF files for your reference. I hope this will help you.

Regards,
Imran Rafique
Support Developer, Saaspose
Support Team
http://www.saaspose.com

Hi Imran,

Thanks!

After a bit of fiddling around I have managed to get it working. Thanks for
all your help.

Regards
Olly Culverhouse

Centation
32A Downend Road
Downend
Bristol
BS16 5UJ

T: 0117 911 7913
http://www.centation.com

Hi Olly,

It is nice to hear from you that you have solved this problem. Please feel free to ask any question if you have about the Saaspose products, we will be happy to help you.

Regards,
Imran Rafique
Support Developer, Saaspose
Support Team
http://www.saaspose.com