We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

PdfContentEditor.ReplaceText

Why does the TextReplace method close the stream? Is there a workwround? How can I use this method? When is a release scheduled that will address this issue? Sorry for all of the questions...I am frustrated.
This message was posted using Aspose.Live 2 Forum

Hello Donnie,

Thanks for using our products.

Save method in PdfContentEditor class has two overloaded instances i.e. one takes String location for saving the PDF document once any operation has been performed and the second method takes Stream object to save the updated PDF document. As per my observations, I am unable to notice that Stream object is automatically closed if we call the ReplaceText method. However, when we call the Save method with Stream object as argument, we need to manually close the Stream object. Please take a look over the following sample code snippet to replace the text in PDF document.

[C#]

// Create PDFContentEditor instance
PdfContentEditor editor = new PdfContentEditor();
// Bind the source input PDF over which update activity needs to be performed
editor.BindPdf(@"d:/pdftest/TextOpacity_Test.pdf");
// Replace the text DRAFT over PDF document with different string
editor.ReplaceText("DRAFT", "WordPpt WordPpt WordPpt WordPpt WordPpt WordPpt ");
// finaly save the udpated PDF document.
editor.Save(@"d:/pdftest/TextOpacity_Test_updated.pdf");

I have observed that ReplaceText is not working correctly in current release version of Apsose.Pdf.Kit. For the sake of correction, I have logged this problem as PDFKITNET-20802 in our issue tracking system. We will further look into the details of this problem and will keep you updated on status of correction. Meanwhile, I have asked the development team to share any ETA regarding its resolution. Soon you will be updated with the appropriate information.

In case I have not properly understood your requirement or you have any further query, please feel free to contact. We apologize for your inconvenience.

Then please explain to me why the following code (VB):

Dim Doc As New PdfContentEditor 'Create instance of PdfContentEditor

Doc.BindPdf(fullPath) 'Load Pdf into PdfContentEditor

Doc.ReplaceText("REPLACE", "Look at my temporary Pdf File!") 'Replace Text

Doc.Save(TempPath) ' Create temporary Pdf

Produces the following error (Every time):

System.ObjectDisposedException was caught
Message="Cannot access a closed Stream."
ObjectName=""
Source="mscorlib"
StackTrace:
at System.IO.__Error.StreamIsClosed() at System.IO.MemoryStream.get_Length() at Aspose.Pdf.Kit.PdfContentEditor.Save(Stream outputStream) at Aspose.Pdf.Kit.PdfContentEditor.Save(String outputFile) at ebuilderapplication.FileView.StreamFile() in C:\VSS\e-Builder 7.6\ebuilder\da2\Documents\FileView.aspx.vb:line 167
InnerException:

Is this not the equivalent of what you are telling me to do? If I remove the line that calls the ReplaceText Method, the Editor saves a perfect (unchanged) copy of the original file. SOMETHING in ReplaceText method is closing the stream. If you read my original post, you will see that I have tried this with an input and output filestream, a memorystream, and calling the method every way I know how. Always the same results. What gives?

Hello Donnie,

I think you are trying to save the updated PDF document to the closed Stream object or the object is not properly initialized. May be you can try saving the updated document to some physical location over the system.

In case the problem still persists, please share the source PDF document so that we can test the scenario at our end. We apologize for your inconvenience.

Please explain how it can be a problem with the stream or an object initialization error when the following code works fine:

Dim Doc As New PdfContentEditor 'Create instance of PdfContentEditor

Doc.BindPdf(fullPath) 'Load Pdf into PdfContentEditor

Doc.Save(TempPath) ' Create temporary Pdf

Here is a larger scale view of the code in the module I am working with:

Dim fullPath As String = "\\Root\Datastore\AppName\Pdf\DocumentName.pdf"

'Generate temporary file name. Append integer until filename is unique

TempPath = fullPath.Insert(fullPath.LastIndexOf(".pdf"), "[temp]")

Do While System.IO.File.Exists(TempPath)

TempPath = TempPath.Insert(TempPath.LastIndexOf(".pdf"), "[1]")

Loop

'Assign license

Dim Lic As New Aspose.Pdf.Kit.License

Lic.SetLicense("Aspose.Total.lic")

Dim Doc As New PdfContentEditor 'Create instance of PdfContentEditor

Doc.BindPdf(fullPath) 'Load Pdf into PdfContentEditor

Doc.ReplaceText("LEASE", "Look at my temporary Pdf File!") 'Replace Text

Doc.Save(TempPath) ' Create temporary Pdf

REMINDER: When I REMOVE the call Doc.ReplaceText(), the PDF is loaded from the original filepath and copied to the new file path perfectly. The stream closing error only occurs if I call the ReplaceText() Method AFTER binding to the original PDF, and BEFORE Saving the NEW PDF. Make sense?

The following code (utilizing filestream objects) also throws the same exception:

'Assign license

Dim Lic As New Aspose.Pdf.Kit.License

Lic.SetLicense("Aspose.Total.lic")

Dim Doc As New PdfContentEditor 'Create instance of PdfContentEditor

Dim Instream As FileStream = IO.File.OpenRead(fullPath) 'Create and open the input filestream

Doc.BindPdf(Instream) 'Load Pdf into PdfContentEditor via input filestream

Doc.ReplaceText("LEASE", "Look at my temporary Pdf File!") 'Replace Text within the Pdf

Dim OutStream As FileStream = IO.File.OpenWrite(TempPath) ' Create and open the output filestream

Doc.Save(OutStream) ' Save the altered Pdf to a temp file location via output filestream

Instream.Close() ' Close the input filestream

OutStream.Close() ' Close the output filestream

Hi,

I have tested the scenario using Aspose.Pdf.Kit for .NET 4.9.0 and I am unable to notice any problem. I have used the attached input file Aspose-Test.pdf as source PDF and the resultant PDF updated-document.pdf is also in attachment. Please take a look.