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();
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
int i = 1;
foreach (TextFragment textFragment in textFragmentCollection)
textFragment.Text = “Updated One”;
System.InvalidCastException was unhandled
Message=Unable to cast object of type ‘.’ to type ‘.’.
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 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)
Thank you for the sample code. I tried using your code to test your issue using Aspose.Pdf version 18.104.22.168, 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,
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.
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,
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 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 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