我如何编辑 pdf 并将其放入 zip 中,然后使用 IText 和 java 下载?

How can i edit pdf and put it inside zip during stream then download using IText and java?

我的用例是这样的:当客户端点击下载 pdf 时,我想 edit/write 使用 Itext pdf 编辑器将一些文本添加到 pdf 上,然后压缩 pdf 然后让它下载,所有期间流。我知道如果 pdf 很大等内存问题,这不会成为问题,因为它大约 20-50kb。在使用字节数组下载工作之前,我在流中进行了压缩,现在必须在压缩之前也运行制作pdfeditor方法,添加一些文本然后让下载发生。

到目前为止,这是我的代码:

    public class zipfolder {

        public static void main(String[] args) {
            try {
                System.out.println("opening connection");
                URL url = new URL("http://gitlab.itextsupport.com/itext/sandbox/raw/master/resources/pdfs/form.pdf");
                InputStream in = url.openStream();
                // FileOutputStream fos = new FileOutputStream(new
                // File("enwiki.png"));
                PdfEditor writepdf = new PdfEditor();

                writepdf.manipulatePdf(url, dest, "field"); /// where i belive i
                                                            /// should execute the
                                                            /// editor function ?

                File f = new File("test.zip");

                ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(f));
                ZipEntry entry = new ZipEntry("newform.pdf");
                zos.putNextEntry(entry);

                System.out.println("reading from resource and writing to file...");
                int length = -1;
                byte[] buffer = new byte[1024];// buffer for portion of data from
                                                // connection
                while ((length = in.read(buffer)) > -1) {
                    zos.write(buffer, 0, length);
                }
                zos.close();
                in.close();
                System.out.println("File downloaded");
            } catch (Exception e) {
                System.out.println("Error");
                e.printStackTrace();
            }

         }
         }

        public class PdfEditor {
            public String insertFields (String field, String value) {
                return field + " " + value;
                // System.out.println("does this work :" + field);
            }

            // public static final String SRC = "src/resources/source.pdf";
            // public static final String DEST = "src/resources/Destination.pdf";
            //
            // public static void main(String[] args) throws DocumentException,
            // IOException {
            // File file = new File(DEST);
            // file.getParentFile().mkdirs();
            // }

            public String manipulatePdf(URL src, String dest, String field) throws Exception {
                System.out.println("test");
                try {
                    PdfReader reader = new PdfReader(src);
                    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
                    AcroFields form = stamper.getAcroFields();
                    Item item = form.getFieldItem("Name");
                    PdfDictionary widget = item.getWidget(0);
                    PdfArray rect = widget.getAsArray(PdfName.RECT);
                    rect.set(2, new PdfNumber(rect.getAsNumber(2).floatValue() + 20f));
                    String value = field;
                    form.setField("Name", value);
                    form.setField("Company", value);
                    stamper.close();
                } catch (Exception e) {
                    System.out.println("Error in manipulate");
                    System.out.println(e.getMessage());
                    throw e;
                }
                return field;
            }
        }

所以玩了 ByteArrayOutputStream,终于成功了。将输入流传递给 'manipulatepdf' 并返回 'bytedata'.

public ByteArrayOutputStream manipulatePdf(InputStream in, String field) throws Exception {

    System.out.println("pdfediter got hit");

    ByteArrayOutputStream bytedata = new ByteArrayOutputStream();
    try {
        PdfReader reader = new PdfReader(in);

        PdfStamper stamper = new PdfStamper(reader, bytedata);
        AcroFields form = stamper.getAcroFields();
        Item item = form.getFieldItem("Name");
        PdfDictionary widget = item.getWidget(0);
        PdfArray rect = widget.getAsArray(PdfName.RECT);
        rect.set(2, new PdfNumber(rect.getAsNumber(2).floatValue() + 20f));
        String value = field;
        form.setField("Name", value);
        form.setField("Company", value);
        stamper.close();
    } catch (Exception e) {
        System.out.println("Error in manipulate");
        System.out.println(e.getMessage());
        throw e;
    }
    return bytedata;
}

public String editandzip (String data, String Link) {
    try {
        System.out.println("opening connection");
        URL url = new URL(Link);
        InputStream in = url.openStream();

        System.out.println("in : "+ url);

        //String data  = "working ok with main";
        PdfEditor writetopdf = new PdfEditor();
        ByteArrayOutputStream bao = writetopdf.manipulatePdf(in, data);
        byte[] ba = bao.toByteArray();

        File f = new File("C:/Users/JayAcer/workspace/test/test.zip");


        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(f));
        ZipEntry entry = new ZipEntry("newform.pdf");
        entry.setSize(ba.length);
        zos.putNextEntry(entry);
        zos.write(ba);


        zos.close();
        in.close();
        System.out.println("File downloaded");
    } catch (Exception e) {
        System.out.println("Error");
        e.printStackTrace();
    }
    return data;

}

}