iText - clickable image should open ms word attachment

后端 未结 1 928
星月不相逢
星月不相逢 2021-01-22 03:51

How can I make an image clickable so the attached ms word document opens? I have some PDFs here where there are some images (ms word icon with the ms word file name beneath the

相关标签:
1条回答
  • 2021-01-22 04:44

    Please take a look at section 12.6.4.4 in ISO-32000-1 (that is the PDF specification). That section is titled "Embedded Go-To Actions":

    enter image description here

    As you've found out, the behavior you describe is by specification. The GoToE action is for jumping to and form a PDF file that is embedded in another PDF file. Other document formats aren't supported because.

    Your only option is to introduce a file attachment annotation instead of an Embedded file along with a GoToE action. See for instance the FileAttachmentAnnot example:

    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        Rectangle rect = new Rectangle(36, 700, 136, 800);
        PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
                writer, PATH, "test.docx", null);
        PdfAnnotation attachment =
                PdfAnnotation.createFileAttachment(writer, rect, "Click me" , fs);
        PdfAppearance app = writer.getDirectContent().createAppearance(100, 100);
        Image img = Image.getInstance(IMG);
        img.scaleAbsolute(100, 100);
        img.setAbsolutePosition(0, 0);
        app.addImage(img);
        attachment.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app);
        writer.addAnnotation(attachment);
        document.close();
    }
    

    In this example, we create a PdfAnnotation and we define a custom appearance for this annotation (instead of the pin or the paperclip symbol). I used an image because that's what you seem to want. Check out the result here (this works with Adobe Reader, but not all PDF viewers support this).

    0 讨论(0)
提交回复
热议问题