Free Support Forum - aspose.com

Search and replace in a 1 page PDF document

Hello, I am attempting to perform a search and replace using the boiler plate code provided in the documentation. I can see the text that I want to replace in my pdf document, in fact, I copied and pasted that text into the code from the PDF file.

Here is the error that I am getting:

Value cannot be null.
Parameter name: stream

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: stream

Source Error:

Line 173:        Document pdfDocument = new Document(pdfTemplateName.ToString());
Line 174:        TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("FirstMiddleLast");
Line 175:        pdfDocument.Pages[1].Accept(textFragmentAbsorber);
Line 176:        TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
Line 177:        foreach (TextFragment textFragment in textFragmentCollection)


Source File: e:\inetpubNPB\wwwroot\npb\OLPP\olps_licensePrint.aspx.cs Line: 175

Stack Trace:

[ArgumentNullException: Value cannot be null.
Parameter name: stream]
   System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) +9488097
   System.IO.StreamReader..ctor(Stream stream) +30
   ...cctor() +302

[TypeInitializationException: The type initializer for ‘.’ threw an exception.]
..a( encodingType) +0
.a.a( encodingType) +658
•.˜.a() +67
•.…ctor( pdfFont) +54
.a.a( font) +91
.Œ.a( iPdfString) +26
..a() +441
.Œ.a( opLink, firstBlockOpLink, “ segmentBuilder, ‰ resources, Double xIndent, Double yIndent,  textState) +246
.“.a(Int32 opIndex, Int32 btOpIndex,  op,  textState) +378
.“.() +394
.”.a(Queue commandQueue, contentStream, ‰ resources) +56
.”.a( contents, ‰ resources) +63
.”.a() +74
.”…ctor( page) +59
Aspose.Pdf.Text.TextFragmentAbsorber.Visit(Page page) +32
Aspose.Pdf.Page.Accept(TextFragmentAbsorber visitor) +12
olps_licensePrint.createCertificate() in e:\inetpubNPB\wwwroot\npb\OLPP\olps_licensePrint.aspx.cs:175
olps_licensePrint.Page_Load(Object sender, EventArgs e) in e:\inetpubNPB\wwwroot\npb\OLPP\olps_licensePrint.aspx.cs:120
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207



Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

v

Here is the code that is generating the error:

Document pdfDocument = new Document(pdfTemplateName.ToString());

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("FirstMiddleLast");

pdfDocument.Pages[1].Accept(textFragmentAbsorber);

TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

foreach (TextFragment textFragment in textFragmentCollection)

{

//update text and other properties

textFragment.Text = FirstMiddleLastName.ToString();

//textFragment.TextState.Font = FontRepository.FindFont("Verdana");

//textFragment.TextState.FontSize = 22;

//textFragment.TextState.ForegroundColor =

//System.Drawing.Color.Blue;

//textFragment.TextState.BackgroundColor =

//System.Drawing.Color.Green;

}

textFragmentAbsorber.Phrase =

"LicenseNumber";

pdfDocument.Pages[1].Accept(textFragmentAbsorber);

textFragmentCollection = textFragmentAbsorber.TextFragments;

foreach (TextFragment textFragment in textFragmentCollection)

{

textFragment.Text = CertificateFullNumber.ToString();

}

Document pdfDocument = new Document(pdfTemplateName.ToString());

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("FirstMiddleLast");

pdfDocument.Pages[1].Accept(textFragmentAbsorber);

TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

foreach (TextFragment textFragment in textFragmentCollection)

{

//update text and other properties

textFragment.Text = FirstMiddleLastName.ToString();

//textFragment.TextState.Font = FontRepository.FindFont("Verdana");

//textFragment.TextState.FontSize = 22;

//textFragment.TextState.ForegroundColor =

//System.Drawing.Color.Blue;

//textFragment.TextState.BackgroundColor =

//System.Drawing.Color.Green;

}

textFragmentAbsorber.Phrase =

"LicenseNumber";

pdfDocument.Pages[1].Accept(textFragmentAbsorber);

textFragmentCollection = textFragmentAbsorber.TextFragments;

foreach (TextFragment textFragment in textFragmentCollection)

{

textFragment.Text = CertificateFullNumber.ToString();

}

The file that I am using to search in is attached.


This message was posted using Page2Forum (attachment) from How to Use New Aspose.Pdf for .NET API - Aspose.Pdf for .NET

Hi John,

Thanks for using our products.

I have tested the scenario using Aspose.Pdf for .NET 6.3.0 and as per my observations, I am unable to notice any problem. I have tried replacing the string FirstMiddleLast with Aspose and LicenseNumber with Aspose.License and resultant PDF is properly being generated. Please take a look over the attached resultant PDF document that I have generated. Can you please try using v6.3.0 and in case you still face any problem, please share some details regarding your working environment. We are really sorry for your inconvenience.