Used version: pdfbox-0.8.0

Hi,

I'm trying to generate a RubberStamp with a custom appearance in a PDF, using the pdfbox. I wrote the following test-code, but it throws an exception at the line, where I am trying to save the document. After testing and searching for a whole day, I could not find any example nor do I have any further ideas.

Please give me a hint, how I can give a RubberStamp a custom apperance, using an external jpg (that should be included in the PDF afterwards).

Here is the test-code I wrote:
(I came to this solution by analyzing the structure of a manually set custom RubberStamp)

------------ Used test code ------------
//Generating a new document and adding a new page
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);

//Get all annotations
List annotations = page.getAnnotations();

//Generate a new RubberStamp
PDAnnotationRubberStamp aRubber = new PDAnnotationRubberStamp();
aRubber.setName("fancy_name");
aRubber.setContents("This is the content of the generated stamp!");

//Generate a custom appearance for the RubberStamp
FileInputStream fin = new FileInputStream("fancyImage.jpg");

PDJpeg mypic = new PDJpeg(document,fin);

PDResources myResources = new PDResources();
myResources.getXObjects().put("Im0", mypic);

COSStream s = new COSStream(document.getDocument().getScratchFile());

PDAppearanceStream myDic = new PDAppearanceStream(s);           
myDic.setResources(myResources);

PDAppearanceDictionary appearance = new PDAppearanceDictionary(new COSDictionary());
appearance.setNormalAppearance(new HashMap<String, COSObjectable>());
appearance.getNormalAppearance().put("default", myDic);

//Set the appearance of the RubberStamp to the custom look
aRubber.setAppearance(appearance);

//Define and set the target rectangle
PDRectangle myrect = new PDRectangle();
myrect.setUpperRightX(250);
myrect.setUpperRightY(550);
myrect.setLowerLeftX(275);
myrect.setLowerLeftY(575);
aRubber.setRectangle(myrect);

//Add the new RubberStamp to the document
annotations.add(aRubber);

// Finally write the new file and close it
document.save("output_final.pdf");
document.close();
------------ /Used test code ------------


When running the code, I get the following excpetion:


------------ Exception ------------
Exception in thread "main" org.apache.pdfbox.exceptions.COSVisitorException
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:939)
        at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:201)
        at 
org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:430)
        at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:361)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:768)
        at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:379)
        at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1070)
        at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:893)
        at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:874)
        at Annotation.main(Annotation.java:177)
java.lang.NullPointerException
        at org.apache.pdfbox.cos.COSStream.getFilteredStream(COSStream.java:136)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:913)
        at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:201)
        at 
org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:430)
        at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:361)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:768)
        at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:379)
        at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1070)
        at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:893)
        at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:874)
        at Annotation.main(Annotation.java:177)
------------ /Exception ------------

Thanks for all your help!
Greetings,
Erik

Reply via email to