This is the code:
try {
com.lowagie.text.pdf.PdfReader reader = new
com.lowagie.text.pdf.PdfReader("D:\\images\\3 Layers.pdf");
String res = "D:\\images\\layers.pdf";
com.lowagie.text.Document document = new
com.lowagie.text.Document();
com.lowagie.text.pdf.PdfWriter writer =
com.lowagie.text.pdf.PdfWriter.getInstance(document,
new java.io.FileOutputStream(res));
document.open();
com.lowagie.text.pdf.PdfStamper stamper = new
com.lowagie.text.pdf.PdfStamper(reader, new java.io.FileOutputStream(res));
com.lowagie.text.pdf.PdfContentByte cb =
writer.getDirectContent();
Map<String, com.lowagie.text.pdf.PdfLayer> map =
stamper.getPdfLayers();
for (Iterator<String> it = map.keySet().iterator();
it.hasNext(); ) {
String key = (String)it.next();
com.lowagie.text.pdf.PdfLayer layer =
(com.lowagie.text.pdf.PdfLayer)map.get(key);
System.out.println(key + " : layer is on " +
layer.isOn() + " is on
panel " + layer.isOnPanel());
if (!layer.isOn()) {
continue;
}
cb.beginLayer(layer);
cb.endLayer();
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
The code issues a null pointer exception on document.close():
java.lang.NullPointerException
at com.lowagie.text.pdf.PdfWriter.getNewObjectNumber(Unknown Source)
at com.lowagie.text.pdf.PRIndirectReference.toPdf(Unknown Source)
at com.lowagie.text.pdf.PdfDictionary.toPdf(Unknown Source)
at com.lowagie.text.pdf.PdfDictionary.toPdf(Unknown Source)
at com.lowagie.text.pdf.PdfDictionary.toPdf(Unknown Source)
at com.lowagie.text.pdf.PdfIndirectObject.writeTo(Unknown Source)
at com.lowagie.text.pdf.PdfWriter$PdfBody.add(Unknown Source)
at com.lowagie.text.pdf.PdfWriter$PdfBody.add(Unknown Source)
at com.lowagie.text.pdf.PdfWriter$PdfBody.add(Unknown Source)
at com.lowagie.text.pdf.PdfWriter.addToBody(Unknown Source)
at com.lowagie.text.pdf.PdfPages.addPage(Unknown Source)
at com.lowagie.text.pdf.PdfWriter.add(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.newPage(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.close(Unknown Source)
at com.lowagie.text.Document.close(Unknown Source)
at TestDriver.PDF(TestDriver.java:105)
at TestDriver.main(TestDriver.java:75)
Why? What should I do to prevent this exception?
--
View this message in context:
http://itext-general.2136553.n4.nabble.com/Preserving-layers-when-placing-PDFs-as-image-tp2171120p4624388.html
Sent from the iText - General mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php