*
* @param contents the contents of the page
*/
- public void setContents(PDFStream contents) {
+ public void setContents(PDFReference contents) {
if (contents != null) {
- put("Contents", new PDFReference(contents));
+ put("Contents", contents);
}
}
package org.apache.fop.pdf;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
+import java.util.Arrays;
/**
* Class representing a PDF stream.
return len;
}
+ public int streamHashCode() throws IOException {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ outputRawStreamData(bos);
+ return Arrays.hashCode(bos.toByteArray());
+ }
}
import org.apache.fop.pdf.PDFPage;
import org.apache.fop.pdf.PDFReference;
import org.apache.fop.pdf.PDFResources;
+import org.apache.fop.pdf.PDFStream;
import org.apache.fop.render.extensions.prepress.PageBoundaries;
import org.apache.fop.render.extensions.prepress.PageScale;
import org.apache.fop.render.intermediate.AbstractBinaryWritingIFDocumentHandler;
= new PDFDocumentNavigationHandler(this);
private Map<Integer, PDFArray> pageNumbers = new HashMap<Integer, PDFArray>();
+ private Map<Integer, PDFReference> contents = new HashMap<Integer, PDFReference>();
/**
* Default constructor.
}
try {
this.documentNavigationHandler.commit();
- this.pdfDoc.registerObject(generator.getStream());
- currentPage.setContents(generator.getStream());
+ setUpContents();
PDFAnnotList annots = currentPage.getAnnotations();
if (annots != null) {
this.pdfDoc.addObject(annots);
}
}
+ private void setUpContents() throws IOException {
+ PDFStream stream = generator.getStream();
+ int hash = stream.streamHashCode();
+ if (!contents.containsKey(hash)) {
+ pdfDoc.registerObject(stream);
+ PDFReference ref = new PDFReference(stream);
+ contents.put(hash, ref);
+ }
+ currentPage.setContents(contents.get(hash));
+ }
+
/** {@inheritDoc} */
public void handleExtensionObject(Object extension) throws IFException {
if (extension instanceof XMPMetadata) {
import org.apache.fop.pdf.PDFNumber;
import org.apache.fop.pdf.PDFPage;
import org.apache.fop.pdf.PDFPaintingState;
+import org.apache.fop.pdf.PDFReference;
import org.apache.fop.pdf.PDFResources;
import org.apache.fop.pdf.PDFStream;
PDFFilterList.CONTENT_FILTER, false);
pdfStream.add(getString());
this.pdfDoc.registerObject(pdfStream);
- pdfContext.getCurrentPage().setContents(pdfStream);
+ pdfContext.getCurrentPage().setContents(new PDFReference(pdfStream));
PDFAnnotList annots = pdfContext.getCurrentPage().getAnnotations();
if (annots != null) {
this.pdfDoc.addObject(annots);
PDFPage page = new PDFPage(resources, i, f, f, f, f);
doc.registerObject(page);
doc.registerObject(gen.getStream());
- page.setContents(gen.getStream());
+ page.setContents(new PDFReference(gen.getStream()));
}
gen.flushPDFDoc();
byte[] data = out.toByteArray();
--- /dev/null
+/*
+ * 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.
+ */
+
+/* $Id$ */
+package org.apache.fop.render.pdf;
+
+import java.awt.Dimension;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.transform.stream.StreamResult;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.apps.FopFactory;
+import org.apache.fop.fonts.FontInfo;
+import org.apache.fop.render.intermediate.IFContext;
+import org.apache.fop.render.intermediate.IFException;
+
+public class PDFDocumentHandlerTestCase {
+ @Test
+ public void testPageContentsDeduplicated() throws IFException {
+ FOUserAgent userAgent = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
+ PDFDocumentHandler d = new PDFDocumentHandler(new IFContext(userAgent));
+ d.setFontInfo(new FontInfo());
+ OutputStream writer = new ByteArrayOutputStream();
+ StreamResult result = new StreamResult(writer);
+ d.setResult(result);
+ d.startDocument();
+
+ addPage(d, "a", 0);
+ addPage(d, "b", 1);
+ addPage(d, "a", 2);
+ d.endDocument();
+
+ List<String> contents = new ArrayList<String>();
+ for (String line : writer.toString().split("\n")) {
+ if (line.trim().startsWith("/Contents")) {
+ contents.add(line);
+ }
+ }
+ Assert.assertEquals(contents.size(), 3);
+ Assert.assertEquals(contents.get(0), contents.get(2));
+ Assert.assertFalse(contents.get(0).equals(contents.get(1)));
+ }
+
+ private void addPage(PDFDocumentHandler d, String command, int i) throws IFException {
+ d.startPage(i, "", "", new Dimension());
+ d.getGenerator().add(command);
+ d.endPage();
+ }
+}