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

时间:2018-02-16 16:25:31

标签: java stream itext inputstream

我的用例是这样的:当客户点击pdf上的下载时,我想使用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;
            }
        }

1 个答案:

答案 0 :(得分:0)

所以玩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;

}

}