Im getting an error while getting hyperlinks from PDF file in this line
PdfDictionary AnnotationAction = (PdfDictionary)AnnotationDictionary.Get(PdfName.A);
Getting below mention exception, somebody please help on this.
Unable to cast object of type
'iTextSharp.text.pdf.PRIndirectReference' to type
'iTextSharp.text.pdf.PdfDictionary'.
Instead of
PdfDictionary AnnotationAction = (PdfDictionary)AnnotationDictionary.Get(PdfName.A);
please try
PdfDictionary AnnotationAction = AnnotationDictionary.GetAsDict(PdfName.A);
In case of your document the value of the /A key does not seem to be a dictionary immediately but instead a reference to a dictionary. This reference has to be resolved. GetAsDict does this and the cast under the hood.
Related
I am using iTextSharp with C# to fill an editable PDF. Everything was working fine, but suddenly I am getting an error on the PdfStamper line.
PdfReader pdfReader = new PdfReader(File.ReadAllBytes(pdfTemplatePath));
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create,FileAccess.Write));
AcroFields pdfFormFields = pdfStamper.AcroFields;
The error is
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=itextsharp
StackTrace:
at iTextSharp.text.Version.GetInstance()
I have checked the file paths and everything - that is all correct. I have also made sure there are no permission issues.
Can someone please let me what might be causing this to throw a null reference exception.
Thanks in advance.
I want to extract text from an PDF document page wise and I am using itext. I used the example code from their website:
PdfReader reader = new PdfReader(pathToFile);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
TextExtractionStrategy strategy = parser.processContent(page, new SimpleTextExtractionStrategy());
The method processContent gives me a NullPointerException. What did I do wrong?
This is the stacktrace I get when using version 5.5.0 with this file:
java.lang.NullPointerException
at com.itextpdf.text.pdf.parser.PdfReaderContentParser.processContent(PdfReaderContentParser.java:82)
at com.itextpdf.text.pdf.parser.PdfReaderContentParser.processContent(PdfReaderContentParser.java:105)
at org.languageresources.PDFExtraktor.extractTextFromPage(PDFExtractor.java:100)
Given the code sniplet and sample document you gave I tried to reproduce the issue but to no avail, text extraction worked fine.
Furthermore, the stacktrace given:
java.lang.NullPointerException
at com.itextpdf.text.pdf.parser.PdfReaderContentParser.processContent(PdfReaderContentParser.java:82)
at com.itextpdf.text.pdf.parser.PdfReaderContentParser.processContent(PdfReaderContentParser.java:105)
at org.languageresources.PDFExtraktor.extractTextFromPage(PDFExtractor.java:100)
does not match the alleged version 5.5.0 because PdfReaderContentParser.java:82 in that version is an empty line and PdfReaderContentParser.java:105 does not exist: Back then that file was only 85 lines in size.
Assuming, though, that you use the current version 5.5.9, the stacktrace makes sense, in that version PdfReaderContentParser.java:82 is the second of these lines:
PdfDictionary pageDic = reader.getPageN(pageNumber);
PdfDictionary resourcesDic = pageDic.getAsDict(PdfName.RESOURCES);
and pageDic indeed can be null if there is no page pageNumber.
Thus, please check that page, the page number in question, is between 1 and reader.getNumberOfPages() inclusive.
I am trying to convert a word document(.docx) into PDF. I am trying to pass Stream object into load document method of RichEditDocumentServer,but my bad ,I am getting a blank pdf. I tried passing the file path in load document method ,which worked fine. But my requirement has to meet with stream object. Can anyone help me to fix the issue. A sample code has been added below.
private Stream ConvertToPdf(Stream fileStream)
{
RichEditDocumentServer server = new RichEditDocumentServer();
fileStream.Seek(0, SeekOrigin.Begin);
server.LoadDocument(fileStream, DocumentFormat.Doc);
Stream convertStream = new MemoryStream();
server.ExportToPdf(convertStream);
convertStream.Seek(0, SeekOrigin.Begin);
return convertStream;
}
it worked with the below link
https://www.devexpress.com/support/center/Question/Details/T340655#answer-9c3224dd-383a-4faa-9672-9b34e36c1c7a
server.LoadDocument(fileStream, DocumentFormat.OpenXml);
I am unable to fire an "onchange" event in mshtml. Can you please tell me what I am doing wrong here.
HTMLSelectElement element = (HTMLSelectElement)this.HTMLDocument.all.item(controlId, 0);
IHTMLElement e = element as IHTMLElement;
IHTMLDocument4 doc = e.document as IHTMLDocument4;
object dummy = null;
object eventObj = doc.CreateEventObject(ref dummy);
HTMLSelectElementClass se = element as HTMLSelectElementClass;
se.FireEvent("onchange", ref eventObj);
I am getting variable "se" as null. I got this piece of code from another link http://www.itwriting.com/phorum/read.php?3,1507
Can anyone help me with this.
Thanks,
Sam
Runtime Callable Wrapper objects generated by COM calls like HTMLDocument.all.item can translate interface casting to QueryInterface calls. But the RCW does not know how to convert to a managed class like HTMLSelectElementClass, thus it returns null.
Instead of casting to HTMLSelectElementClass, cast to IHTMLElement3 to call fireEvent.
By the way, your code does not work in IE11 mode as document.all is deprecated. Use IHTMLDocument3::getElementById instead.
I had tried all those which Sheng mentioned but didn't work.
This issue was solved by injecting javascript code for "onchange" and executing it. It worked.
hoping that someone can see the flaw in my code to merge to PDF-a documents using ITextSharp. Currently it complains about missing metadata which PDF-a requires.
Document document = new Document();
MemoryStream ms = new MemoryStream();
using (PdfACopy pdfaCopy = new PdfACopy(document, ms, PdfAConformanceLevel.PDF_A_1A))
{
document.Open();
using (PdfReader reader = new PdfReader("Doc1.pdf"))
{
pdfaCopy.AddDocument(reader);
}
using (PdfReader reader = new PdfReader("doc2.pdf"))
{
pdfaCopy.AddDocument(reader);
}
}
The exact error received is
Unhandled Exception: iTextSharp.text.pdf.PdfAConformanceException: The document catalog dictionary of a PDF/A conforming file shall contain
the Metadata key
I was hoping that the 'document catalog dictionary' would be copied as well, but I guess the 'new Document()' creates an empty non-conforming document or something.
Thanks! Hope you can help
Wouter
You need to add this line:
copy.CreateXmpMetadata();
This will create some default XMP metadata. Of course: if you want to create your own XMP file containing info about the documents you're about to merge, you can also use:
copy.XmpMetadata = myMetaData;
where myMetaData is a byte array containing a correct XMP stream.
I hope you understand that iText can't automatically create the correct metadata. Providing metadata is something that needs human attention.