Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-607

Problem setting custom appearance on PDAnnotationRubberStamp

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Not A Problem
    • 0.8.0-incubator, 1.0.0
    • 0.8.0-incubator, 1.0.0
    • PDModel
    • None
    • Mac OS X 10.5.8 / J2SE-1.5

    Description

      I'm trying to generate a RubberStamp with a custom appearance in a PDF. I wrote the following test-code: it saves the document and I can find the image inside the pdf, by examinating the structures. But the Rubberstamp isn't shown within the PDF when opened with the Reader.

      I made a second PDF using Acrobat Pro with the same stamp - there I can see the stamp when opening with a Reader.

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

      //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());
      s.createUnfilteredStream();

      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();

      Attachments

        1. example2.pdf
          64 kB
          Erik Scholtz
        2. output_final.pdf
          25 kB
          Erik Scholtz

        Activity

          People

            Unassigned Unassigned
            scholtz Erik Scholtz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: