在pdf中添加附件注释并在Web应用程序jar中执行

时间:2018-05-22 16:31:49

标签: annotations itext attachment

我正在使用itext注释在pdf中实现添加附件注释,但非常有趣的是,由于Web应用程序框架的限制,我必须将实现代码和附件文件放在jar文件中。以下是实施的简要层次结构:

  1. package example.pdf
    • Implementation.java
    • attachment.doc
  2. 主要代码:

    protected void createAttachment(PdfWriter writer, Rectangle rect, String 
    templatePath, String fileName) throws IOException, DocumentException {
    // Get instruction document
    String embed = 
    getClass().getClassLoader().getResource(templatePath).getFile();
    
    // The fileName here is used to display in the attachment list of the 
    // document
    PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
                writer, embed, fileName, null);
    
    // The fileName here is used to display on the document
    PdfAnnotation attachment =
                PdfAnnotation.createFileAttachment(writer, rect, fileName, fs);
    
    // Specify the width and height of the icon area
    PdfAppearance app = writer.getDirectContent().createAppearance(200, 200);
    String wordIcon = 
    getClass().getClassLoader().getResource(WORD_ICON).getFile();
        Image img = Image.getInstance(wordIcon);
        img.scaleAbsolute(200, 200);
        img.setAbsolutePosition(0, 0);
    
        app.addImage(img);
        attachment.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app);
        writer.addAnnotation(attachment);
    }
    

    以上代码将由服务类调用:

    Rectangle rect = new Rectangle(220, 620, 250, 640);
        createAttachment(pdfWriter, rect, "pdf/attachment.doc", 
    "attachment.doc");
    

    它在我的IDE下正常工作,但是一旦我将它们作为jar(包括附件),它就会显示Web应用程序无法找到/附加附件。并告诉我该文件无法在系统路径中找到。

    我知道它可能是与类路径检测机制相关的问题(例如类加载器和加载方法),但它确实阻止我进一步移动,因为我没有找到任何可能的解决方案。

    例外:

    java.io.FileNotFoundException: C:\Projects\work\test-1.3.6.0- 
    SNAPSHOT.jar!\pdf\attachment.doc (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
    at java.net.URL.openStream(URL.java:1045)
    at com.lowagie.text.pdf.PdfFileSpecification.fileEmbedded(Unknown Source)
    at com.lowagie.text.pdf.PdfFileSpecification.fileEmbedded(Unknown Source)
    at com.lowagie.text.pdf.PdfFileSpecification.fileEmbedded(Unknown Source)
    at example.pdf.Implementation.createAttachment
    

1 个答案:

答案 0 :(得分:0)

问题解决了。 唯一的区别是输入流而不是文件名,类PdfFileSpecification有这样的接口。

        InputStream embedInputStream = 
        getClass().getClassLoader().getResourceAsStream(templatePath);

        byte[] buffer = new byte[1024];

        embedOutputStream = new ByteArrayOutputStream();
        int len = -1;
        while ((len = embedInputStream.read(buffer)) != -1) {
            embedOutputStream.write(buffer, 0, len);
        }

        // The fileName here is used to display in the attachment list of the document
        PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
                writer, null, fileName, embedOutputStream.toByteArray());

        // The fileName here is used to display on the document
        PdfAnnotation attachment =
                PdfAnnotation.createFileAttachment(writer, rect, fileName, fs);