第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

深度克隆 PDPage 的正確方法是什么?

深度克隆 PDPage 的正確方法是什么?

冉冉說 2022-09-28 10:09:57
我正在使用PDFBOX v2,我正在嘗試克隆PD文檔的第一個(gè)PD頁面,以將其保留為新PD頁面的模板。第一頁,有一些我需要填寫的雜技字段。我嘗試了一些方法,但任何人都讓我想要實(shí)現(xiàn)。1)復(fù)制第一頁內(nèi)容,并在我需要新頁面時(shí)將其添加到文檔中。復(fù)制頁面,但accroform字段與其他頁面字段鏈接,如果我從第一頁修改字段值,則會顯示在其他頁面中。//Save in variable first page contentCOSDictionary pageContent = (COSDictionary)doc.getPage(0).getCOSObject();...//when i need insert new pagedoc.addPage(new PDPage(pageContent));2)克隆第一頁內(nèi)容,然后像第一種方法一樣添加到文檔中。復(fù)制頁面但沒有復(fù)制任何字段:/PDFCloneUtility cloner = new PDFCloneUtility(doc);COSDictionary pageContent = (COSDictionary)cloner.cloneForNewDocument(doc.getPage(0).getCOSObject());...//when i need insert new pagedoc.addPage(new PDPage(pageContent));那么,制作PDPage的深度副本的正確方法是什么,使accroform字段獨(dú)立于第一頁?謝謝!
查看完整描述

1 回答

?
慕慕森

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊

我得到了解決方案!


1)從一個(gè)空的pdf模板開始,只有1頁。打開模板文檔,填充常用數(shù)據(jù)并另存為內(nèi)存中的byte[]。



PDDocument templatedoc = PDDocument.load(new File(path));           

PDDocumentCatalog catalog = templatedoc.getDocumentCatalog();           


PDAcroFrom acroForm = catalog.getAcroForm());


... fill acroForm common data of all pages ...


ByteArrayOutputStream basicTemplate = new ByteArrayOutputStream();          

templatedoc.save(basicTemplate);


byte[] filledBasicTemplate = basicTemplate.toByteArray();

2)為每個(gè)需要的頁面生成新文檔。



List<PDDocument> documents = new ArrayList<PDDocument>();

PDDocument activeDoc;


for(int i = 0; i < 5; i++) {

  activeDoc = PDDocument.load(filledBasicTemplate);

  documents.add(activeDoc);


  ... fill acroform or you need in each page ...


}


3)將所有新文檔的首頁導(dǎo)入最終文檔并保存最終文檔。



PDDocument finalDoc = new PDDocument();


for(PDDocument currentDoc : documents) {

   ... fill common things like page numbers ...

   finalDoc.importPage(currentDoc.getPage(0));

}


finalDoc.save(new File(path));



... close all documents after save the final document ...


它可能不是最優(yōu)化的代碼,但它可以工作。


查看完整回答
反對 回復(fù) 2022-09-28
  • 1 回答
  • 0 關(guān)注
  • 162 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號