Мое лучшее решение из StackOverflow 20084117
Код: Выделить всё
URL donorUrl = this.class.classLoader.getResource("files/DonorDocument.pdf")
File donorFile = new File(donorUrl.toURI());
PDDocument donorDocument = PDDocument.load(donorFile);
PDPage donorPage = donorDocument.getPage(0);
// trying to clone the page per SO 20084117
COSDictionary donorDictionary = donorPage.getCOSObject(); // they said getCOSDictionary but that is 404
COSDictionary newPageDictionary = new COSDictionary(donorDictionary);
newPageDictionary.removeItem(COSName.ANNOTS);
PDPage newPage = new PDPage(newPageDictionary);
targetDocument.addPage(newPage);
donorDocument.close();
// the following line will return a page which has been closed
targetDocument.getPage(document.numberOfPages - 1);
Код: Выделить всё
PDFCloneUtility cloner = new PDFCloneUtility(donorDocument);
COSBase clonedDOcument = cloner.cloneForNewDocument(donorPage.getCOSObject());
COSDictionary clonedDictionary = clonedDOcument as COSDictionary;
COSDictionary newPageDict = new COSDictionary(clonedDictionary);
PDPage newPage = new PDPage(newPageDict);
targetDocument.addPage(clonePage);
donorDocument.close();
// the following line will return a page which has been closed
targetDocument.getPage(document.numberOfPages - 1);
Код: Выделить всё
PDFCloneUtility cloner = new PDFCloneUtility(donorDocument);
PDPage clonePage = new PDPage();
cloner.cloneMerge(donorPage, clonePage);
targetDocument.addPage(clonePage);
donorDocument.close();
// the following line will return a page which has been closed
targetDocument.getPage(document.numberOfPages - 1);
Подробнее здесь: https://stackoverflow.com/questions/790 ... -the-donor