Author: tilman Date: Sun Jul 2 15:01:24 2023 New Revision: 1910742 URL: http://svn.apache.org/viewvc?rev=1910742&view=rev Log: PDFBOX-5627: subset when doing incremental test + test, as suggested by DvdM
Added: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSIncrement.java (with props) 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=1910742&r1=1910741&r2=1910742&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 Sun Jul 2 15:01:24 2023 @@ -1366,13 +1366,8 @@ public class PDDocument implements Close throw new IOException("Cannot save a document which has been closed"); } - // subset designated fonts - for (PDFont font : fontsToSubset) - { - font.subset(); - } - fontsToSubset.clear(); - + subsetDesignatedFonts(); + // save PDF COSWriter writer = new COSWriter(output); try @@ -1385,6 +1380,15 @@ public class PDDocument implements Close } } + private void subsetDesignatedFonts() throws IOException + { + for (PDFont font : fontsToSubset) + { + font.subset(); + } + fontsToSubset.clear(); + } + /** * Save the PDF as an incremental update. This is only possible if the PDF was loaded from a * file or a stream, not if the document was created in PDFBox itself. There must be a path of @@ -1410,6 +1414,7 @@ public class PDDocument implements Close public void saveIncremental(OutputStream output) throws IOException { + subsetDesignatedFonts(); COSWriter writer = null; try { @@ -1458,6 +1463,7 @@ public class PDDocument implements Close */ public void saveIncremental(OutputStream output, Set<COSDictionary> objectsToWrite) throws IOException { + subsetDesignatedFonts(); if (pdfSource == null) { throw new IllegalStateException("document was not loaded from a file or a stream"); @@ -1515,6 +1521,7 @@ public class PDDocument implements Close */ public ExternalSigningSupport saveIncrementalForExternalSigning(OutputStream output) throws IOException { + subsetDesignatedFonts(); if (pdfSource == null) { throw new IllegalStateException("document was not loaded from a file or a stream"); Added: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSIncrement.java URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSIncrement.java?rev=1910742&view=auto ============================================================================== --- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSIncrement.java (added) +++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSIncrement.java Sun Jul 2 15:01:24 2023 @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.pdfbox.cos; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; + +import junit.framework.TestCase; + +import java.io.IOException; + +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.PDPageContentStream; +import org.apache.pdfbox.pdmodel.common.PDRectangle; +import org.apache.pdfbox.pdmodel.font.PDFont; +import org.apache.pdfbox.pdmodel.font.PDType0Font; +import org.junit.Test; + +public abstract class TestCOSIncrement extends TestCase +{ + /** + * Check that subsetting takes place in incremental saving. + * + */ + @Test + public void testSubsetting() throws IOException + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + PDDocument document = new PDDocument(); + PDPage page = new PDPage(PDRectangle.A4); + document.addPage(page); + document.save(baos); + document.close(); + + document = PDDocument.load(baos.toByteArray()); + + page = document.getPage(0); + + PDFont font = PDType0Font.load(document, TestCOSIncrement.class.getResourceAsStream( + "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf")); + + PDPageContentStream contentStream = new PDPageContentStream(document, page); + + contentStream.beginText(); + contentStream.setFont(font, 12); + contentStream.newLineAtOffset(75, 750); + contentStream.showText("Apache PDFBox"); + contentStream.endText(); + contentStream.close(); + + COSDictionary catalog = document.getDocumentCatalog().getCOSObject(); + catalog.setNeedToBeUpdated(true); + COSDictionary pages = catalog.getCOSDictionary(COSName.PAGES); + pages.setNeedToBeUpdated(true); + page.getCOSObject().setNeedToBeUpdated(true); + + document.saveIncremental(new FileOutputStream("target/PDFBOX-5627.pdf")); + document.close(); + + document = PDDocument.load(new File("target/PDFBOX-5627.pdf")); + page = document.getPage(0); + COSName fontName = page.getResources().getFontNames().iterator().next(); + font = page.getResources().getFont(fontName); + assertTrue(font.isEmbedded()); + document.close(); + } +} + Propchange: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSIncrement.java ------------------------------------------------------------------------------ svn:eol-style = native