[英]merge multiple pdfs in order
嘿,很抱歉,您發布的帖子過長,語言不好,如果有不必要的細節,
我使用excel文檔從一個pdf模板創建了多個1page pdf
我現在有了
像這樣的東西
tempfile0.pdf
tempfile1.pdf
tempfile2.pdf
...
我試圖使用itext5將所有文件合並為一個pdf
但它的結果是,結果pdf中的頁面不符合我希望的示例順序
第一頁中的tempfile0.pdf
臨時文件1。 int 2000頁
這是即時通訊使用的代碼。
我使用的程序是:
1從哈希圖中填充
2將填寫的表單另存為一個pdf
3將所有文件合並為一個pdf
public void fillPdfitext(int debut,int fin) throws IOException, DocumentException {
for (int i =debut; i < fin; i++) {
HashMap<String, String> currentData = dataextracted[i];
// textArea.appendText("\n"+pdfoutputname +" en cours de preparation\n ");
PdfReader reader = new PdfReader(this.sourcePdfTemplateFile.toURI().getPath());
String outputfolder = this.destinationOutputFolder.toURI().getPath();
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputfolder+"\\"+"tempcontrat"+debut+"-" +i+ "_.pdf"));
// get the document catalog
AcroFields acroForm = stamper.getAcroFields();
// as there might not be an AcroForm entry a null check is necessary
if (acroForm != null) {
for (String key : currentData.keySet()) {
try {
String fieldvalue=currentData.get(key);
if (key=="ADRESSE1"){
fieldvalue = currentData.get("ADRESSE1")+" "+currentData.get("ADRESSE2") ;
acroForm.setField("ADRESSE", fieldvalue);
}
if (key == "IMEI"){
acroForm.setField("NUM_SERIE_PACK", fieldvalue);
}
acroForm.setField(key, fieldvalue);
// textArea.appendText(key + ": "+fieldvalue+"\t\t");
} catch (Exception e) {
// e.printStackTrace();
}
}
stamper.setFormFlattening(true);
}
stamper.close();
}
}
這是合並的代碼
public void Merge() throws IOException, DocumentException
{
File[] documentPaths = Main.objetapp.destinationOutputFolder.listFiles((dir, name) -> name.matches( "tempcontrat.*\\.pdf" ));
Arrays.sort(documentPaths, NameFileComparator.NAME_INSENSITIVE_COMPARATOR);
byte[] mergedDocument;
try (ByteArrayOutputStream memoryStream = new ByteArrayOutputStream())
{
Document document = new Document();
PdfSmartCopy pdfSmartCopy = new PdfSmartCopy(document, memoryStream);
document.open();
for (File docPath : documentPaths)
{
PdfReader reader = new PdfReader(docPath.toURI().getPath());
try
{
reader.consolidateNamedDestinations();
PdfImportedPage pdfImportedPage = pdfSmartCopy.getImportedPage(reader, 1);
pdfSmartCopy.addPage(pdfImportedPage);
}
finally
{
pdfSmartCopy.freeReader(reader);
reader.close();
}
}
document.close();
mergedDocument = memoryStream.toByteArray();
}
FileOutputStream stream = new FileOutputStream(this.destinationOutputFolder.toURI().getPath()+"\\"+
this.sourceDataFile.getName().replaceFirst("[.][^.]+$", "")+".pdf");
try {
stream.write(mergedDocument);
} finally {
stream.close();
}
documentPaths=null;
Runtime r = Runtime.getRuntime();
r.gc();
}
我的問題是如何在生成的pdf中保持文件順序相同
這是因為文件的命名。 您的代碼new FileOutputStream(outputfolder + "\\\\" + "tempcontrat" + debut + "-" + i + "_.pdf")
將產生:
在tempcontrat0-11_.pdf之前將tempcontrat0-1000_.pdf放置的位置 ,因為在合並之前要按字母順序對其進行排序。
最好使用org.apache.commons.lang.StringUtils
或java.text.DecimalFormat
leftPad()方法將填充字符保留為0
字符,並使其類似於tempcontrat0-000000.pdf , tempcontrat0-000001.pdf , ... tempcontrat0-9999999.pdf 。
而且,您也可以做得更簡單,跳過表格中的寫入,然后從文件中讀取內容,並在表單填寫后立即合並文檔,這樣會更快。 但這取決於要合並的文件數量和大小,以及您擁有的內存量。
因此,您可以將填充的文檔保存到ByteArrayOutputStream
然后在stamper.close()
之后stamper.close()
流中的字節創建新的PdfReader
,然后為該閱讀器調用pdfSmartCopy.getImportedPage()
。 簡而言之,它看起來像:
// initialize
PdfSmartCopy pdfSmartCopy = new PdfSmartCopy(document, memoryStream);
for (int i = debut; i < fin; i++) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
// fill in the form here
stamper.close();
PdfReader reader = new PdfReader(out.toByteArray());
reader.consolidateNamedDestinations();
PdfImportedPage pdfImportedPage = pdfSmartCopy.getImportedPage(reader, 1);
pdfSmartCopy.addPage(pdfImportedPage);
// other actions ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.