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.
Related
I'm working on a program to remove text from a specified area of a pdf.
It works well on most pdfs, but I've found it falls over with some pdfs which contain graphics using Indexed colorspace - it only works on CMYK or RGB. I'm afraid I'm really clueless on this subject so could really use some help.
Here's my code:
Dim source_file as String ="c:\test pdf\test.pdf"
Dim destination_file as String = ="c:\test pdf\output.pdf"
Dim reader As PdfReader = New PdfReader(source_file)
Using outputPdfStream As Stream = New FileStream(destination_file, FileMode.Create, FileAccess.Write, FileShare.None)
Dim stamper = New PdfStamper(reader, outputPdfStream)
Dim Locs As New List(Of PdfCleanUpLocation)
Locs.Add(New PdfCleanUpLocation(1, New Rectangle(97.0F, 405.0F, 480.0F, 445.0F), BaseColor.WHITE))
Dim oCleaner As New PdfCleanUpProcessor(Locs, stamper)
oCleaner.CleanUp()
stamper.Close()
reader.Close()
End Using
The error I'm getting is:
iTextSharp.text.exceptions.UnsupportedPdfException: 'The color space [/Indexed, /DeviceCMYK, 73, 13 0R] is not supported'
This comes up at the oCleaner.CleanUp() line
For reference, I originally extracted the code from the below link where someone was trying to do something similar, but a lot more involved, a few years ago:
https://www.vbforums.com/showthread.php?831051-RESOLVED-Confusion-converting-C-code
If anyone can suggest a way of getting this to work with pdfs featuring Indexed colorspace graphics I'd be extremely grateful!
Thanks for reading!
I have a Jasper Report which creates a PDF in Java Spring. I have been trying to change the zoom level for hours and have not been successful. Whenever I open the pdf's using Adobe reader, its 149% (and coworkers is even worse). There was a similar question which did not help.
I have tried the following property names and none of them have worked
"zoom"
"net.sf.jasperreports.viewer.zoom"
"net.sf.jasperreports.viewer.zoom"
com.jaspersoft.studio.viewer.zoom
com.jaspersoft.studio.unit.viewer.zoom
The values I have tried are
0.5
1.1
2
I have checked my Adobe Reader properties and zoom is set to default, and accessibility is also off.
As Villat indicated in comment one way to set zoom level is "this.zoom=50;"
You can do this either by indicating it in jrxml
<property name="net.sf.jasperreports.export.pdf.javascript" value="this.zoom=50;"/>
or
by setting it to the SimplePdfExporterConfiguration if exporting from java
....
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setPdfJavaScript("this.zoom=50;");
exporter.setConfiguration(configuration);
However
It is up to the reader (application used to open pdf), to decide if it will/can execute the javascript.
For example in standard Adobe Acrobat Reader DC a user can manually turn this off under menu Edit>>Preferences
Furthermore, if the reader is already open it seems to not always like to change the zoom level through javascript, my installed reader works properly only if it opens with the pdf.
Alternative solution
If you are exporting in java you can post elaborate the pdf adding a OpenAction, see Bruno Lowagie's answer https://stackoverflow.com/a/24095098/5292302
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0,
reader.getPageSize(1).getHeight(), 0.75f);
PdfAction action = PdfAction.gotoLocalPage(1, pdfDest, stamper.getWriter());
stamper.getWriter().setOpenAction(action);
stamper.close();
reader.close();
}
Hence once exported, you call a similar method, if memory allows it you can also do this in memory using a ByteArrayOutputStream or similar.
This solution is more reliable, but in the the end it's always up to to the reader that user is using if it will be respected or not.
Using the iText PDF libraries (v7), does anyone have any advice on how to remove 'Document-level' JavaScripts from PDFs? I have figured out how to remove Page-Level JavaScripts, but cannot seem to figure out how to remove those at the document-level. Thank you.
I got this resolved and below is the snippet of code (C#) in case anyone else needs it:
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE), new PdfWriter(TARGET));
PdfCatalog pdfCat = pdfDoc.GetCatalog();
PdfDictionary names = pdfCat.GetPdfObject().GetAsDictionary(PdfName.Names);
names.Remove(PdfName.JavaScript);
pdfDoc.Close();
While upgrading from iText 2.1.3 to iText 2.1.7 I noticed a change in behavior. The input PDF file is coming from Adobe Indesign and the goal is to change the Application and PDFProducer properties to something I want.
Below is the relevant piece of code :
PdfStamper pdfStamper = null;
PdfReader pdfReader = null;
pdfReader = new PdfReader(new RandomAccessFileOrArray(source), null);
HashMap info = pdfReader.getInfo();
info.put( "Producer", "Myself");
info.put( "Creator", "Myself");
OutputStream outputStream = new FileOutputStream(source+"stamped.pdf");
pdfStamper = new PdfStamper(pdfReader, outputStream, '\0', true); // append mode
pdfStamper.close();
pdfReader.close();
So, before, using itext 2.1.3, after running a pdf through the code above, I would see the Application and PDFProducer changed to "Myself" when opening the file in Adobe Reader (File, Properties). That's what I expect
With itext 2.1.7, the Application property does not change and the PDFProducer property has the text "modified using iText 2.1.7 by ..." appended.
Using iText 5.4.5, the Application property is changed but the PDFProducer update behaves just like in 2.1.7
Any idea what happened and if there is something I can do to fix this?
thanks
Cristian
I am working in asp .net mvc3.
I have these statements in controller class:
PdfWriter.GetInstance(doc, new FileStream((Request.PhysicalApplicationPath + "\\Receipt5.pdf"),
FileMode.Create));
doc.Open();
PdfPTable table = new PdfPTable(2);
table.AddCell("tt[0]");
table.AddCell("tt[1]");
doc.close();
All time my values are changing but in pdf sometimes showing old result. please tell me what should i do for it that whenever i press done button then new pdf document should generate.
i am using iTextSharp to generate pdf.
It seems that you're not able to replace the old file cause it is locked.
Try to delete it and see what happens.
Anyway, consider that if more than one user tries to print the same document you can have a concurrency problem.
I would suggest you to use a generated file name:
var newFile = System.IO.Path.Combine(Request.PhysicalApplicationPath, Guid.NewGuid().ToString() + ".pdf");