Unable to cast object of type '.' to type '.'

Hi,

While working with TextAbsorber i got this exception and couldn’t find the issue. I have shown the text in bold where its throwing exception.

My Code is:
Document openFile = new Document(@“C:\input_copy.pdf”);
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber();
openFile.Pages[2].Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
int i = 1;
foreach (TextFragment textFragment in textFragmentCollection)
{
textFragment.Text = “Updated One”;
}
openFile.Save();

Exception:
System.InvalidCastException was unhandled
Message=Unable to cast object of type ‘.‚’ to type ‘.Š’.
Source=Aspose.Pdf
StackTrace:
at ƒ.Œ.(› resources, ‡ standardFont1Name, String& resKey)
at „..(String unicodeString, › resources, † noCharacterAction, Boolean isEmbedded, ›& encodedString, & selectedFont, String& fontResourceKey)
at ‚..([] choosingStrategies, String unicodeString, › resources, † noCharacterAction, Boolean isEmbedded, ›& encodedText, & selectedFont, String& fontResourceKey)
at .‘.(String )
at Aspose.Pdf.Text.TextSegment.set_Text(String value)
at Aspose.Pdf.Text.TextFragment.set_Text(String value)
at AsposePdfDemo.Program.Main(String[] args) in F:\InternDemo\AsposePdfDemo\AsposePdfDemo\Program.cs:line 36
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Hi Mehul,

Thank you for the sample code. I tried using your code to test your issue using Aspose.Pdf version 6.4.2.0, I was unable to get any exception. Kindly use latest version of Aspose.Pdf, I hope this will works fine.

Please do let us know if you need any further assistance and kindly share source PDF file with us. This will help us to figure out the issue and reply back to you soon.

Thanks & Regards,

Hi Rashid,

Earlier i was using version 6.2.0. After your reply i downloaded latest version which is 6.4.0 from the download section of Aspose.Pdf but still issue dint get resolved.
Can you please let me know of there is anything else i need to do for that.

Thanks,
Mehul

Hi Mehul,

Please share the template PDF file you are using to show the issue. This will help us figure out the issue soon.

We apologize for your inconvenience.

Thanks & Regards,

Hi Rashid,

I’ve attached input file which i am using for my application. Hope it’ll help. When i am trying to read the value of that particular fragment, it is able to read it but it’s not updating the value of it.

Thanks,
Mehul


Hi Mehul,


Thanks for sharing the source file. I have tested the scenario and have observed the similar problem but further investigation has revealed that the problem is occurring because you are creating a blank constructor of TextFragmentAbsorber class and trying to update the original PDF document which you have accessed with Document object. I think if you update the code snippet as specified below, the problem will be resolved.
For your reference, I have also attached the resultant PDF that I have generated with Aspose.Pdf for .NET 6.4.2. We are really sorry for your inconvenience.

[C#]
Document openFile = new Document(@“d:\pdftest\input.pdf”);
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(“Problem”);
openFile.Pages[2].Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
int i = 1;
foreach (TextFragment textFragment in textFragmentCollection)
{
textFragment.Text = “Updated One”;
}
openFile.Save(“d:/pdftest/Updated-Document.pdf”);

Hi Nayyer,

Thanks for the reply. I deliberately left the constructor argument of TextAbsrorber. The reason being is , i wanted to extract all the text one by one using fragment and wanted to apply some logic on the extracted text and then replace it with the one extracted. If i put something in TextAbsorber constructor then it would search only for those having that value which i don’t want.

In short TextAbsorber and TextFragment provides some hierarchical structure to fetch the text in order it appears in the document so that i can extract them and then replace if necessary. Hope it helps. Let me know if you need some more information.


Thanks,
Mehul

Hello Mehul,


Thanks for sharing the details. As far as I have understood from your requirement, you are interested in extracting text from PDF file and would like to search particular text strings inside it and would like to replace them with new text. You do not want to search a single string inside the PDF, but would like to perform search against multiple strings. If so is the case, then I think you may consider creating an array of Text strings and then iterate through each item of the array, pass individual items of array as argument to TextFragmentAbsorber(…) constructor and search for it over particular page of the PDF document. If you need to iterate through the whole PDF file, you may consider using something like openFile.Pages.Accept(textFragmentAbsorber); instead of providing the specific page reference.

According to Document Object Model of Aspose.Pdf namespace, The TextFragmentAbsorber object is basically used in text search scenario. When the search is completed the occurrences are represented with TextFragment objects that the TextFragments collection contains. The TextFragment object provides access to the search occurrence text, text properties, and allows to edit text and change the text state (font, font size, color etc).

If I have still not properly understood your requirement, please feel free to contact.

Hi Nayyer,

Thanks a lot for reply. It’ll really help me. But my requirement is slightly different to what you understood. I want to read source document and say want to create a copy of it and call it target document which is say exact copy of source document but i need to modify few paragraphs or some other text.But i don’t know which are those unless each paragraph is processed through a logic. So in short i want to read each paragraph and pass it to my logic and if logic modifies it i will replace it with the paragraph passed to it.

I couldn’t find any way by which i can read pdf document in order it appears because i don’t want to reformat target document . Target document should be exact copy of the source except the modified text. And one more things is source can contain anything like table, images etc. I only have to process on text in the source, rest of the things should simply copied from source doc and put to appropriate place in target.

Hope this will make scenario clear. Let me know if there is any other way i can achieve this. I didn’t find any other way so going through following way:

Using stream, create a copy of source and call it target
Read target using Textabsorber so i’ll get texts line by line, find out where it ends so it’ll be a paragraph
Pass paragraph to logic and if it is modified, replace it with the paragraph sent as argument

Thanks,
Mehul