如何编辑pdf并在流期间将其放入zip中,然后使用IText和java下载?
我的使用案例是这样的:当客户端在pdf上单击下载时,我想使用Itext pdf编辑器在pdf上编辑/写入一些文本,然后压缩pdf,然后让它下载,所有这些都是在流期间进行的。我知道内存问题,如果pdf是大的等,这不会是一个问题,因为它像20-50kb。我在下载前使用字节数组在流中进行压缩,现在必须在压缩前运行pdfeditor方法,添加一些文本,然后让下载进行 以下是我目前的代码:如何编辑pdf并在流期间将其放入zip中,然后使用IText和java下载?,java,stream,itext,inputstream,Java,Stream,Itext,Inputstream,我的使用案例是这样的:当客户端在pdf上单击下载时,我想使用Itext pdf编辑器在pdf上编辑/写入一些文本,然后压缩pdf,然后让它下载,所有这些都是在流期间进行的。我知道内存问题,如果pdf是大的等,这不会是一个问题,因为它像20-50kb。我在下载前使用字节数组在流中进行压缩,现在必须在压缩前运行pdfeditor方法,添加一些文本,然后让下载进行 以下是我目前的代码: public class zipfolder { public static void m
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,最终实现了它的工作。将输入流传递给“操纵EPDF”并返回“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;
}
}如果要将数据保存在内存中,请不要写入
文件输出流
,而是使用ByteArrayOutputStream
。将此流用作manipulatedfd
参数,或返回它或从中提取的byte[]
作为方法结果。请不要在生产代码中使用gitlab.itextsupport.com
@AmedeeVanGasse我没有使用任何生产代码,只是简单地测试你的itext,因为它有一个现有的字段和所有你用byteArrayOutputStream玩了一段时间的东西。努力让它工作,谢谢。