You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2016/04/27 19:29:33 UTC
svn commit: r1741295 -
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
Author: tilman
Date: Wed Apr 27 17:29:33 2016
New Revision: 1741295
URL: http://svn.apache.org/viewvc?rev=1741295&view=rev
Log:
PDFBOX-3280: do a deep clone, as suggested by Cornelis Hoeflake
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1741295&r1=1741294&r2=1741295&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Wed Apr 27 17:29:33 2016
@@ -42,6 +42,7 @@ import org.apache.pdfbox.io.RandomAccess
import org.apache.pdfbox.io.RandomAccessBufferedFileInputStream;
import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.io.ScratchFile;
+import org.apache.pdfbox.multipdf.PDFCloneUtility;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdfwriter.COSWriter;
import org.apache.pdfbox.pdmodel.common.COSArrayList;
@@ -502,34 +503,26 @@ public class PDDocument implements Close
* document and want to copy the contents to this document's scratch file then use this method otherwise just use
* the {@link #addPage} method.
*
- * Unlike {@link #addPage}, this method does a deep copy. If your page has annotations, and if
- * these link to pages not in the target document, then the target document might become huge.
- * What you need to do is to delete page references of such annotations. See
+ * Unlike {@link #addPage}, this method does a deep clone. This will be slower and have a larger
+ * memory footprint. However the deep clone is important to avoid resources getting lost if the
+ * source document is closed when the destination document is saved.
+ *
+ * If your page has annotations, and if these link to pages not in the target document, then the
+ * target document might become huge. What you need to do is to delete page references of such
+ * annotations. See
* <a href="http://stackoverflow.com/a/35477351/535646">here</a> for how to do this.
*
* @param page The page to import.
* @return The page that was imported.
- *
+ *
* @throws IOException If there is an error copying the page.
*/
public PDPage importPage(PDPage page) throws IOException
{
- PDPage importedPage = new PDPage(new COSDictionary(page.getCOSObject()), resourceCache);
- InputStream in = null;
- try
- {
- in = page.getContents();
- if (in != null)
- {
- PDStream dest = new PDStream(this, in, COSName.FLATE_DECODE);
- importedPage.setContents(dest);
- }
- addPage(importedPage);
- }
- catch (IOException e)
- {
- IOUtils.closeQuietly(in);
- }
+ PDFCloneUtility cloner = new PDFCloneUtility(this);
+ COSBase pageBase = cloner.cloneForNewDocument(page.getCOSObject());
+ PDPage importedPage = new PDPage((COSDictionary) pageBase, resourceCache);
+ addPage(importedPage);
return importedPage;
}