Return-Path: X-Original-To: apmail-pdfbox-commits-archive@www.apache.org Delivered-To: apmail-pdfbox-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5D6251928D for ; Wed, 27 Apr 2016 17:29:20 +0000 (UTC) Received: (qmail 64969 invoked by uid 500); 27 Apr 2016 17:29:20 -0000 Delivered-To: apmail-pdfbox-commits-archive@pdfbox.apache.org Received: (qmail 64944 invoked by uid 500); 27 Apr 2016 17:29:20 -0000 Mailing-List: contact commits-help@pdfbox.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@pdfbox.apache.org Delivered-To: mailing list commits@pdfbox.apache.org Received: (qmail 64935 invoked by uid 99); 27 Apr 2016 17:29:20 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 27 Apr 2016 17:29:20 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 0B7C01A12BC for ; Wed, 27 Apr 2016 17:29:19 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.999 X-Spam-Level: X-Spam-Status: No, score=0.999 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-0.001] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id tsD3CVeLy3rm for ; Wed, 27 Apr 2016 17:29:16 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTP id C570A5F201 for ; Wed, 27 Apr 2016 17:29:16 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 420CFE031A for ; Wed, 27 Apr 2016 17:30:00 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 29C4F3A0216 for ; Wed, 27 Apr 2016 17:29:15 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1741294 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Date: Wed, 27 Apr 2016 17:29:14 -0000 To: commits@pdfbox.apache.org From: tilman@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160427172915.29C4F3A0216@svn01-us-west.apache.org> Author: tilman Date: Wed Apr 27 17:29:14 2016 New Revision: 1741294 URL: http://svn.apache.org/viewvc?rev=1741294&view=rev Log: PDFBOX-3280: do a deep clone, as suggested by Cornelis Hoeflake Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1741294&r1=1741293&r2=1741294&view=diff ============================================================================== --- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original) +++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Wed Apr 27 17:29:14 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 * here 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; }