我在iText的第一次使用,我有这个问题。如何将由ByteArrayOutputStream表示的单页文档插入另一个文档?
我创造了这个stampaFattureMultiple()方法串联的的ArrayList listaFatture集合内检索的PDF文件的一些文件。正如您所看到的PDF文档存储在由listaFatture.get(i).getPdf()表示的Blob字段内。好吧,这工作正常,PDF文档正确连接。
public void stampaFattureMultiple(ArrayList<Fattura> listaFatture) {
ByteArrayOutputStream docPDF = null;
ByteArrayOutputStream currentPdfBAOS = null;
InputStream blobinstream = null;
/** The resulting PDF file: */
String result = "D:/XYZ/fatture-concatenate.pdf";
// STEP 1 Creazione del documento in formato A4 e senza margini:
com.itextpdf.text.Document document = new com.itextpdf.text.Document(com.itextpdf.text.PageSize.A4, 0, 0, 0, 0);
try {
// STEP 2: Make copies of PDF documents. Documents can be edited after reading and before writing them out
//PdfCopy copy = new PdfCopy(document, pdfResult);
docPDF = new ByteArrayOutputStream();
//PdfCopy copy = new PdfCopy(document, docPDF);
PdfCopy copy = new PdfCopy(document, new FileOutputStream(result));
// STEP 3:
document.open();
// Concatena tutti i PDF delle fatture reperite:
for (int i = 0; i < listaFatture.size(); i++) {
// Obtain the current Blob object representing the PDF:
Blob currentPdfBlob = listaFatture.get(i).getPdf();
// Put the current PDF Blob content into the current ByteArrayOutputStream:
if(currentPdfBlob!=null){
blobinstream = currentPdfBlob.getBinaryStream();
int chunk = 1024;
byte[] buffer = new byte[chunk];
int length = -1;
currentPdfBAOS = new ByteArrayOutputStream();
while ((length = blobinstream.read(buffer)) != -1) {
currentPdfBAOS.write(buffer, 0, length);
}
currentPdfBAOS.flush();
}
ByteArrayOutputStream currentFatturaTestataBasos = stampaTestataFatturaPdf(listaFatture.get(i));
//document.newPage();
// STEP 4: reader for the i document:
ByteArrayInputStream currentPdfBAIS = new ByteArrayInputStream(currentPdfBAOS.toByteArray());
PdfReader currentPdfReader = new PdfReader(currentPdfBAIS);
PdfImportedPage page;
PdfCopy.PageStamp stamp;
for (int currentPageIndex = 0; currentPageIndex < currentPdfReader.getNumberOfPages();) {
page = copy.getImportedPage(currentPdfReader, ++currentPageIndex);
copy.addPage(page);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
document.close();
}
}
正如你可以看到做这个任务我以下列方式进行:
我在listaFatture集合内的所有文件迭代和我建立从启动相关的PDF文档在斑点对象:
Blob currentPdfBlob = listaFatture.get(i).getPdf();
然后,我创建了读者这个documnt,通过:
PdfReader currentPdfReader = new PdfReader(currentPdfBAIS);
所以我读这个读者和我复制文档内的页面。
好吧,它工作正常。问题是,在每个文档之前,我必须插入一个从这个生成的特殊页面stampaTestataFatturaPdf()方法返回一个ByteArrayOutputStream代表一个单页文档。
所以我插入此行复制当前PDF页面之前:
ByteArrayOutputStream currentFatturaTestataBasos = stampaTestataFatturaPdf(listaFatture.get(i));
但我不知道如何插入由currentFatturaTestataBasos,我生成文档里面所代表的网页的想法。
你能给我一些帮助和一些建议吗?
TNX
好的tnx,用你的dea解决了;-) – 2015-02-23 16:07:03