e.printStackTrace();
}
}]]></source>
- <p>
- Check the iText tutorial and documentation for setting access flags, password,
- encryption strength and other parameters.
- </p>
- </section>
- <section id="pdf-watermark">
- <title>Watermarks</title>
- <p>
- In addition to the <a href="#pdf-postprocess">PDF Post-processing</a> options, consider the following workarounds:
- </p>
- <ul>
- <li>
- Use a background image for the body region.
- </li>
- <li>
- (submitted by Trevor Campbell) Place an image in a
- region that overlaps the flowing text. For example, make
- region-before large enough to contain your image. Then include a
- block (if necessary, use an absolutely positioned block-container)
- containing the watermark image in the static-content for the
- region-before. Note that the image will be drawn on top of the
- normal content.
- </li>
- </ul>
+ <p>
+ Check the iText tutorial and documentation for setting access flags, password,
+ encryption strength and other parameters.
+ </p>
+ </section>
+ <section id="pdf-watermark">
+ <title>Watermarks</title>
+ <p>
+ In addition to the <a href="#pdf-postprocess">PDF Post-processing</a> options, consider the following workarounds:
+ </p>
+ <ul>
+ <li>
+ Use a background image for the body region.
+ </li>
+ <li>
+ (submitted by Trevor Campbell) Place an image in a
+ region that overlaps the flowing text. For example, make
+ region-before large enough to contain your image. Then include a
+ block (if necessary, use an absolutely positioned block-container)
+ containing the watermark image in the static-content for the
+ region-before. Note that the image will be drawn on top of the
+ normal content.
+ </li>
+ </ul>
+ </section>
+ <section id="pdf-extensions">
+ <title>Extensions</title>
+ <p>The PDF Renderer supports some PDF specific extensions which can be embedded
+ into the input FO document. To use the extensions the appropriate namespace must
+ be declared in the fo:root element like this:</p>
+ <source><![CDATA[
+<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
+ xmlns:pdf="http://xmlgraphics.apache.org/fop/extensions/pdf">
+ ]]></source>
+ <section id="pdf-embedded-file">
+ <title>Embedded Files</title>
+ <p>
+ It is possible to attach/embed arbitrary files into a PDF file. You can give a name and
+ a description of the file. Example:
+ </p>
+ <source><![CDATA[
+ <fo:declarations>
+ <pdf:embedded-file filename="image.jpg" src="url(file:///C:/Temp/myimage.jpg)" description="My image"/>
+ <pdf:embedded-file src="url(file:///C:/Temp/MyTextDoc.odt)"/>
+ </fo:declarations>
+ ]]></source>
+ <p>
+ <code>pdf:embedded-file</code> must be a child of <code>fo:declarations</code>.
+ The "src" property is used to reference the file that is to be embedded. This property
+ uses the "uri-specification" datatype from the XSL-FO specification.
+ The "filename" property is optional. If it is missing the filename is automatically set
+ from the URI/IRI of the "src" property. An optional description can also be added to
+ further describe the file attachment.
+ </p>
+ <p>
+ It is also possible to reference an embedded file from an <code>fo:basic-link</code>.
+ Use the special "embedded-file:" URI scheme with the filename as single argument after
+ the URI scheme. Example:
+ </p>
+ <source><![CDATA[
+<fo:basic-link external-destination="url(embedded-file:image.jpg)">Attached Image</fo:basic-link>
+]]></source>
+ <p>
+ Note: Not all PDF Viewers (including some Acrobat Versions) will open the embedded file
+ when clicking on the link. In that case, the user will have to open he attachment via
+ the separate list of file attachments.
+ </p>
+ </section>
+ </section>
</section>
-</section>
<section id="ps">
<title>PostScript</title>
<p>
org.apache.fop.render.ps.extensions.PSExtensionElementMapping
org.apache.fop.render.afp.extensions.AFPElementMapping
org.apache.fop.render.pcl.extensions.PCLElementMapping
+org.apache.fop.render.pdf.extensions.PDFElementMapping
--- /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.pdf;
+
+import java.util.Date;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * This class represents an embedded file stream.
+ */
+public class PDFEmbeddedFile extends PDFStream {
+
+ protected static Log log = LogFactory.getLog(PDFEmbeddedFile.class.getName());
+
+ /**
+ * Creates a new embedded file stream.
+ */
+ public PDFEmbeddedFile() {
+ super();
+ put("Type", new PDFName("EmbeddedFile"));
+ PDFDictionary params = new PDFDictionary();
+ params.put("CreationDate", params.formatDateTime(new Date()));
+ put("Params", params);
+ }
+
+}
return link;
}
+ private static final String EMBEDDED_FILE = "embedded-file:";
+
/**
* Create/find and return the appropriate external PDFAction according to the target
*
public PDFAction getExternalAction(String target, boolean newWindow) {
int index;
String targetLo = target.toLowerCase();
- // HTTP URL?
- if (targetLo.startsWith("http://")) {
+ if (target.startsWith(EMBEDDED_FILE)) {
+ // File Attachments (Embedded Files)
+ String filename = target.substring(EMBEDDED_FILE.length());
+ return getLaunchActionForEmbeddedFile(filename);
+ } else if (targetLo.startsWith("http://")) {
+ // HTTP URL?
return new PDFUri(target);
- // Non PDF files. Try to /Launch them.
} else if (targetLo.startsWith("file://")) {
+ // Non PDF files. Try to /Launch them.
target = target.substring("file://".length());
return getLaunchAction(target);
- // Bare PDF file name?
} else if (targetLo.endsWith(".pdf")) {
+ // Bare PDF file name?
return getGoToPDFAction(target, null, -1, newWindow);
- // PDF file + page?
} else if ((index = targetLo.indexOf(".pdf#page=")) > 0) {
+ // PDF file + page?
String filename = target.substring(0, index + 4);
int page = Integer.parseInt(target.substring(index + 10));
return getGoToPDFAction(filename, null, page, newWindow);
- // PDF file + destination?
} else if ((index = targetLo.indexOf(".pdf#dest=")) > 0) {
+ // PDF file + destination?
String filename = target.substring(0, index + 4);
String dest = target.substring(index + 10);
return getGoToPDFAction(filename, dest, -1, newWindow);
- // None of the above? Default to URI:
} else {
+ // None of the above? Default to URI:
return new PDFUri(target);
}
}
+ private PDFAction getLaunchActionForEmbeddedFile(String filename) {
+ PDFNames names = getDocument().getRoot().getNames();
+ if (names == null) {
+ throw new IllegalStateException(
+ "No Names dictionary present."
+ + " Cannot create Launch Action for embedded file: " + filename);
+ }
+ PDFNameTreeNode embeddedFiles = names.getEmbeddedFiles();
+ if (embeddedFiles == null) {
+ throw new IllegalStateException(
+ "No /EmbeddedFiles name tree present."
+ + " Cannot create Launch Action for embedded file: " + filename);
+ }
+ PDFArray files = embeddedFiles.getNames();
+ PDFReference embeddedFileRef = null;
+ int i = 0;
+ while (i < files.length()) {
+ String name = (String)files.get(i);
+ i++;
+ PDFReference ref = (PDFReference)files.get(i);
+ if (name.equals(filename)) {
+ embeddedFileRef = ref;
+ break;
+ }
+ i++;
+ }
+ if (embeddedFileRef == null) {
+ throw new IllegalStateException(
+ "No embedded file with name " + filename + " present.");
+ }
+ PDFLaunch launch = new PDFLaunch(embeddedFileRef);
+ return launch;
+ }
+
/**
* Create or find a PDF GoTo with the given page reference string and Y offset,
* and return its PDF object reference
package org.apache.fop.pdf;
+
/**
* class representing a /FileSpec object.
*
*/
-public class PDFFileSpec extends PDFObject {
-
- /**
- * the filename
- */
- protected String filename;
+public class PDFFileSpec extends PDFDictionary {
/**
* create a /FileSpec object.
/* generic creation of object */
super();
+ put("Type", new PDFName("Filespec"));
+ put("F", filename);
+ }
- this.filename = filename;
+ private String getFilename() {
+ return (String)get("F");
}
/**
- * {@inheritDoc}
+ * Associates an dictionary with pointers to embedded file streams with this file spec.
+ * @param embeddedFile the dictionary with pointers to embedded file streams
*/
- public String toPDFString() {
- return getObjectID()
- + "<<\n/Type /FileSpec\n"
- + "/F (" + this.filename + ")\n"
- + ">>\nendobj\n";
+ public void setEmbeddedFile(PDFDictionary embeddedFileDict) {
+ put("EF", embeddedFileDict);
}
- /*
- * example
- * 29 0 obj
- * <<
- * /Type /FileSpec
- * /F (table1.pdf)
- * >>
- * endobj
+ /**
+ * Sets a description for the file spec.
+ * @param description the description
+ * @since PDF 1.6
*/
+ public void setDescription(String description) {
+ put("Desc", description);
+ }
/** {@inheritDoc} */
protected boolean contentEquals(PDFObject obj) {
PDFFileSpec spec = (PDFFileSpec)obj;
- if (!spec.filename.equals(filename)) {
+ if (!spec.getFilename().equals(getFilename())) {
return false;
}
* @param pageReference the pageReference represented by this object
*/
public PDFGoTo(String pageReference) {
- /* generic creation of object */
super();
-
- this.pageReference = pageReference;
+ setPageReference(pageReference);
}
/**
*/
public PDFGoTo(String pageReference, Point2D position) {
/* generic creation of object */
- super();
-
- this.pageReference = pageReference;
+ this(pageReference);
setPosition(position);
}
*/
public class PDFLaunch extends PDFAction {
- private PDFFileSpec externalFileSpec;
+ private PDFReference externalFileSpec;
+ /**
+ * Creates a new /Launch action.
+ * @param fileSpec the file specification to launch
+ */
public PDFLaunch(PDFFileSpec fileSpec) {
+ this(fileSpec.makeReference());
+ }
+
+ /**
+ * Creates a new /Launch action.
+ * @param fileSpec a reference to the file specification
+ */
+ public PDFLaunch(PDFReference fileSpec) {
+ PDFObject fs = fileSpec.getObject();
+ if (fs != null) {
+ assert fs instanceof PDFFileSpec;
+ }
this.externalFileSpec = fileSpec;
}
+ /** {@inheritDoc} */
public String getAction() {
return this.referencePDF();
}
+ /** {@inheritDoc} */
public String toPDFString() {
StringBuffer sb = new StringBuffer(64);
sb.append(getObjectID());
sb.append("<<\n/S /Launch\n/F ");
- sb.append(externalFileSpec.referencePDF());
+ sb.append(externalFileSpec.toString());
sb.append(" \n>>\nendobj\n");
return sb.toString();
PDFLaunch launch = (PDFLaunch) obj;
- if (!launch.externalFileSpec.referencePDF().equals(externalFileSpec.referencePDF())) {
+ if (!launch.externalFileSpec.toString().equals(externalFileSpec.toString())) {
return false;
}
*/
public class PDFNames extends PDFDictionary {
+ private static final String DESTS = "Dests";
+ private static final String EMBEDDED_FILES = "EmbeddedFiles";
+
/**
* Create the Names object
*/
* @return the Dests object, or null if it's not used
*/
public PDFDests getDests() {
- return (PDFDests)get("Dests");
+ return (PDFDests)get(DESTS);
}
/**
* @param dests the Dests object
*/
public void setDests(PDFDests dests) {
- put("Dests", dests);
+ put(DESTS, dests);
+ }
+
+ /**
+ * Returns the EmbeddedFiles object
+ * @return the EmbeddedFiles object, or null if it's not used
+ */
+ public PDFNameTreeNode getEmbeddedFiles() {
+ return (PDFNameTreeNode)get(EMBEDDED_FILES);
+ }
+
+ /**
+ * Set the EmbeddedFiles object
+ * @param dests the EmbeddedFiles object
+ */
+ public void setEmbeddedFiles(PDFNameTreeNode dests) {
+ put(EMBEDDED_FILES, dests);
}
}
}
}
+ /** Checks if embedded files are allowed. */
+ public void verifyEmbeddedFilesAllowed() {
+ final String err = "{0} does not allow embedded files.";
+ if (isPDFAActive()) {
+ throw new PDFConformanceException(format(err, getPDFAMode()));
+ }
+ if (isPDFXActive()) {
+ //Implicit since file specs are forbidden
+ throw new PDFConformanceException(format(err, getPDFXMode()));
+ }
+ }
+
}
import org.apache.fop.render.intermediate.IFDocumentNavigationHandler;
import org.apache.fop.render.intermediate.IFException;
import org.apache.fop.render.intermediate.IFPainter;
+import org.apache.fop.render.pdf.extensions.PDFEmbeddedFileExtensionAttachment;
import org.apache.fop.util.XMLUtil;
/**
} else if (extension instanceof Metadata) {
XMPMetadata wrapper = new XMPMetadata(((Metadata)extension));
pdfUtil.renderXMPMetadata(wrapper);
+ } else if (extension instanceof PDFEmbeddedFileExtensionAttachment) {
+ PDFEmbeddedFileExtensionAttachment embeddedFile
+ = (PDFEmbeddedFileExtensionAttachment)extension;
+ try {
+ pdfUtil.addEmbeddedFile(embeddedFile);
+ } catch (IOException ioe) {
+ throw new IFException("Error adding embedded file: " + embeddedFile.getSrc(), ioe);
+ }
} else {
log.debug("Don't know how to handle extension object. Ignoring: "
+ extension + " (" + extension.getClass().getName() + ")");
} else if (action instanceof URIAction) {
URIAction u = (URIAction)action;
assert u.isComplete();
+ String uri = u.getURI();
PDFFactory factory = getPDFDoc().getFactory();
- pdfAction = factory.getExternalAction(u.getURI(), u.isNewWindow());
+ pdfAction = factory.getExternalAction(uri, u.isNewWindow());
if (!pdfAction.hasObjectNumber()) {
- //Some PDF actions a pooled
+ //Some PDF actions are pooled
getPDFDoc().registerObject(pdfAction);
}
this.completeActions.put(action.getID(), pdfAction);
import org.apache.fop.render.Graphics2DAdapter;
import org.apache.fop.render.RendererContext;
import org.apache.fop.render.pdf.PDFLogicalStructureHandler.MarkedContentInfo;
+import org.apache.fop.render.pdf.extensions.PDFEmbeddedFileExtensionAttachment;
import org.apache.fop.traits.RuleStyle;
import org.apache.fop.util.AbstractPaintingState;
import org.apache.fop.util.CharUtilities;
ExtensionAttachment attachment = ((OffDocumentExtensionAttachment)odi).getAttachment();
if (XMPMetadata.CATEGORY.equals(attachment.getCategory())) {
pdfUtil.renderXMPMetadata((XMPMetadata)attachment);
+ } else if (PDFEmbeddedFileExtensionAttachment.CATEGORY.equals(
+ attachment.getCategory())) {
+ try {
+ pdfUtil.addEmbeddedFile((PDFEmbeddedFileExtensionAttachment)attachment);
+ } catch (IOException ioe) {
+ throw new RuntimeException("Error embedding file", ioe);
+ }
}
}
}
package org.apache.fop.render.pdf;
import java.awt.color.ICC_Profile;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.xmlgraphics.image.loader.util.ImageUtil;
import org.apache.xmlgraphics.xmp.Metadata;
import org.apache.xmlgraphics.xmp.schemas.XMPBasicAdapter;
import org.apache.xmlgraphics.xmp.schemas.XMPBasicSchema;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.fo.extensions.xmp.XMPMetadata;
import org.apache.fop.pdf.PDFAMode;
+import org.apache.fop.pdf.PDFArray;
import org.apache.fop.pdf.PDFConformanceException;
import org.apache.fop.pdf.PDFDictionary;
import org.apache.fop.pdf.PDFDocument;
+import org.apache.fop.pdf.PDFEmbeddedFile;
import org.apache.fop.pdf.PDFEncryptionManager;
import org.apache.fop.pdf.PDFEncryptionParams;
+import org.apache.fop.pdf.PDFFileSpec;
import org.apache.fop.pdf.PDFICCBasedColorSpace;
import org.apache.fop.pdf.PDFICCStream;
import org.apache.fop.pdf.PDFInfo;
import org.apache.fop.pdf.PDFMetadata;
+import org.apache.fop.pdf.PDFNameTreeNode;
+import org.apache.fop.pdf.PDFNames;
import org.apache.fop.pdf.PDFNumsArray;
import org.apache.fop.pdf.PDFOutputIntent;
import org.apache.fop.pdf.PDFPageLabels;
+import org.apache.fop.pdf.PDFReference;
import org.apache.fop.pdf.PDFXMode;
+import org.apache.fop.render.pdf.extensions.PDFEmbeddedFileExtensionAttachment;
import org.apache.fop.util.ColorProfileUtil;
/**
nums.put(pageIndex, dict);
}
+ /**
+ * Adds an embedded file to the PDF file.
+ * @param embeddedFile the object representing the embedded file to be added
+ * @throws IOException if an I/O error occurs
+ */
+ public void addEmbeddedFile(PDFEmbeddedFileExtensionAttachment embeddedFile)
+ throws IOException {
+ this.pdfDoc.getProfile().verifyEmbeddedFilesAllowed();
+ PDFNames names = this.pdfDoc.getRoot().getNames();
+ if (names == null) {
+ //Add Names if not already present
+ names = this.pdfDoc.getFactory().makeNames();
+ this.pdfDoc.getRoot().setNames(names);
+ }
+
+ //Create embedded file
+ PDFEmbeddedFile file = new PDFEmbeddedFile();
+ this.pdfDoc.registerObject(file);
+ Source src = getUserAgent().resolveURI(embeddedFile.getSrc());
+ InputStream in = ImageUtil.getInputStream(src);
+ if (in == null) {
+ throw new FileNotFoundException(embeddedFile.getSrc());
+ }
+ try {
+ OutputStream out = file.getBufferOutputStream();
+ IOUtils.copyLarge(in, out);
+ } finally {
+ IOUtils.closeQuietly(in);
+ }
+ PDFDictionary dict = new PDFDictionary();
+ dict.put("F", file);
+ PDFFileSpec fileSpec = new PDFFileSpec(embeddedFile.getFilename());
+ fileSpec.setEmbeddedFile(dict);
+ if (embeddedFile.getDesc() != null) {
+ fileSpec.setDescription(embeddedFile.getDesc());
+ }
+ this.pdfDoc.registerObject(fileSpec);
+
+ //Make sure there is an EmbeddedFiles in the Names dictionary
+ PDFNameTreeNode embeddedFiles = names.getEmbeddedFiles();
+ if (embeddedFiles == null) {
+ embeddedFiles = new PDFNameTreeNode();
+ //this.pdfDoc.registerObject(embeddedFiles);
+ names.setEmbeddedFiles(embeddedFiles);
+ }
+
+ //Add to EmbeddedFiles in the Names dictionary
+ PDFArray nameArray = embeddedFiles.getNames();
+ if (nameArray == null) {
+ nameArray = new PDFArray();
+ embeddedFiles.setNames(nameArray);
+ }
+ nameArray.add(embeddedFile.getFilename());
+ nameArray.add(new PDFReference(fileSpec));
+ }
+
}
--- /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.extensions;
+
+// FOP
+import org.apache.fop.fo.FONode;
+import org.apache.fop.fo.extensions.ExtensionAttachment;
+
+/**
+ * Base class for the PDF-specific extension elements.
+ */
+public abstract class AbstractPDFExtensionElement extends FONode {
+
+ /**Extension attachment. */
+ protected PDFExtensionAttachment attachment;
+
+ /**
+ * Default constructor
+ *
+ * @param parent parent of this node
+ * @see org.apache.fop.fo.FONode#FONode(FONode)
+ */
+ public AbstractPDFExtensionElement(FONode parent) {
+ super(parent);
+ }
+
+ /** {@inheritDoc} */
+ public String getNamespaceURI() {
+ return PDFElementMapping.NAMESPACE;
+ }
+
+ /** {@inheritDoc} */
+ public String getNormalNamespacePrefix() {
+ return "pdf";
+ }
+
+ /**
+ * Returns the extension attachment.
+ * @return the extension attachment if one is created by the extension element, null otherwise.
+ * @see org.apache.fop.fo.FONode#getExtensionAttachment()
+ */
+ public ExtensionAttachment getExtensionAttachment() {
+ if (attachment == null) {
+ this.attachment = (PDFExtensionAttachment)instantiateExtensionAttachment();
+ }
+ return this.attachment;
+ }
+
+ /**
+ * Instantiates extension attachment object.
+ * @return extension attachment
+ */
+ protected abstract ExtensionAttachment instantiateExtensionAttachment();
+
+}
+
--- /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.extensions;
+
+import org.apache.fop.fo.ElementMapping;
+import org.apache.fop.fo.FONode;
+
+/**
+ * This class provides the element mapping for the PDF-specific extensions.
+ */
+public class PDFElementMapping extends ElementMapping {
+
+ /** Namespace for the extension */
+ public static final String NAMESPACE = "http://xmlgraphics.apache.org/fop/extensions/pdf";
+
+ /** Main constructor */
+ public PDFElementMapping() {
+ this.namespaceURI = NAMESPACE;
+ }
+
+ /** {@inheritDoc} */
+ protected void initialize() {
+ if (foObjs == null) {
+ foObjs = new java.util.HashMap();
+ foObjs.put(PDFEmbeddedFileElement.ELEMENT, new PDFEmbeddedFileMaker());
+ }
+ }
+
+ static class PDFEmbeddedFileMaker extends ElementMapping.Maker {
+ public FONode make(FONode parent) {
+ return new PDFEmbeddedFileElement(parent);
+ }
+ }
+}
--- /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.extensions;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
+
+import org.apache.fop.apps.FOPException;
+import org.apache.fop.datatypes.URISpecification;
+import org.apache.fop.fo.Constants;
+import org.apache.fop.fo.FONode;
+import org.apache.fop.fo.PropertyList;
+import org.apache.fop.fo.extensions.ExtensionAttachment;
+
+/**
+ * Extension element for pdf:embedded-file.
+ */
+public class PDFEmbeddedFileElement extends AbstractPDFExtensionElement {
+
+ protected static final String ELEMENT = "embedded-file";
+
+ /**
+ * Main constructor
+ * @param parent parent FO node
+ */
+ protected PDFEmbeddedFileElement(FONode parent) {
+ super(parent);
+ }
+
+ /** {@inheritDoc} */
+ protected void startOfNode() throws FOPException {
+ super.startOfNode();
+ if (parent.getNameId() != Constants.FO_DECLARATIONS) {
+ invalidChildError(getLocator(), parent.getName(), getNamespaceURI(), getName(),
+ "rule.childOfDeclarations");
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void processNode(String elementName, Locator locator,
+ Attributes attlist, PropertyList propertyList)
+ throws FOPException {
+ PDFEmbeddedFileExtensionAttachment embeddedFile
+ = (PDFEmbeddedFileExtensionAttachment)getExtensionAttachment();
+ String desc = attlist.getValue("description");
+ if (desc != null && desc.length() > 0) {
+ embeddedFile.setDesc(desc);
+ }
+ String src = attlist.getValue("src");
+ src = URISpecification.getURL(src);
+ if (src != null && src.length() > 0) {
+ embeddedFile.setSrc(src);
+ } else {
+ missingPropertyError("src");
+ }
+ String filename = attlist.getValue("filename");
+ if (filename == null || filename.length() == 0) {
+ try {
+ URI uri = new URI(src);
+ String path = uri.getPath();
+ int idx = path.lastIndexOf('/');
+ if (idx > 0) {
+ filename = path.substring(idx + 1);
+ } else {
+ filename = path;
+ }
+ embeddedFile.setFilename(filename);
+ } catch (URISyntaxException e) {
+ //Filename could not be deduced from URI
+ missingPropertyError("name");
+ }
+ }
+ embeddedFile.setFilename(filename);
+ }
+
+ /** {@inheritDoc} */
+ public String getLocalName() {
+ return ELEMENT;
+ }
+
+ /** {@inheritDoc} */
+ protected ExtensionAttachment instantiateExtensionAttachment() {
+ return new PDFEmbeddedFileExtensionAttachment();
+ }
+}
--- /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.extensions;
+
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+/**
+ * This is the pass-through value object for the PDF extension.
+ */
+public class PDFEmbeddedFileExtensionAttachment extends PDFExtensionAttachment {
+
+ /** element name */
+ protected static final String ELEMENT = "embedded-file";
+
+ /** name of file to be embedded */
+ private static final String ATT_NAME = "filename";
+
+ /** source of file to be embedded (URI) */
+ private static final String ATT_SRC = "src";
+
+ /** a description of the file to be embedded */
+ private static final String ATT_DESC = "desc";
+
+ /** filename attribute */
+ private String filename = null;
+
+ /** description attribute (optional) */
+ private String desc = null;
+
+ /** source name attribute */
+ private String src = null;
+
+ /**
+ * No-argument contructor.
+ */
+ public PDFEmbeddedFileExtensionAttachment() {
+ super();
+ }
+
+ /**
+ * Default constructor.
+ * @param filename the name of the file
+ * @param src the location of the file
+ * @param desc the description of the file
+ */
+ public PDFEmbeddedFileExtensionAttachment(String filename, String src, String desc) {
+ super();
+ this.filename = filename;
+ this.src = src;
+ this.desc = desc;
+ }
+
+ /**
+ * Returns the file name.
+ * @return the file name
+ */
+ public String getFilename() {
+ return filename;
+ }
+
+ /**
+ * Sets the file name.
+ * @param name The file name to set.
+ */
+ public void setFilename(String name) {
+ this.filename = name;
+ }
+
+ /**
+ * Returns the file description.
+ * @return the description
+ */
+ public String getDesc() {
+ return desc;
+ }
+
+ /**
+ * Sets the description of the file.
+ * @param desc the description to set
+ */
+ public void setDesc(String desc) {
+ this.desc = desc;
+ }
+
+ /**
+ * Returns the source URI of the file.
+ * @return the source URI
+ */
+ public String getSrc() {
+ return src;
+ }
+
+ /**
+ * Sets the source URI of the file.
+ * @param src the source URI
+ */
+ public void setSrc(String src) {
+ this.src = src;
+ }
+
+ /** {@inheritDoc} */
+ public String getCategory() {
+ return CATEGORY;
+ }
+
+ /** {@inheritDoc} */
+ public String toString() {
+ return "PDFEmbeddedFile(name=" + getFilename() + ", " + getSrc() + ")";
+ }
+
+ /**
+ * @return the element name
+ * @see org.apache.fop.render.ps.extensions.PDFExtensionAttachment#getElement()
+ */
+ protected String getElement() {
+ return ELEMENT;
+ }
+
+ /** {@inheritDoc} */
+ public void toSAX(ContentHandler handler) throws SAXException {
+ AttributesImpl atts = new AttributesImpl();
+ if (filename != null && filename.length() > 0) {
+ atts.addAttribute(null, ATT_NAME, ATT_NAME, "CDATA", filename);
+ }
+ if (src != null && src.length() > 0) {
+ atts.addAttribute(null, ATT_SRC, ATT_SRC, "CDATA", src);
+ }
+ if (desc != null && desc.length() > 0) {
+ atts.addAttribute(null, ATT_DESC, ATT_DESC, "CDATA", desc);
+ }
+ String element = getElement();
+ handler.startElement(CATEGORY, element, element, atts);
+ handler.endElement(CATEGORY, element, element);
+ }
+
+}
--- /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.extensions;
+
+import org.apache.xmlgraphics.util.XMLizable;
+
+import org.apache.fop.fo.extensions.ExtensionAttachment;
+
+/**
+ * This is the pass-through value object for the PDF extension.
+ */
+public abstract class PDFExtensionAttachment implements ExtensionAttachment, XMLizable {
+
+ /** The category URI for this extension attachment. */
+ public static final String CATEGORY = "apache:fop:extensions:pdf";
+
+ /**
+ * Default constructor.
+ */
+ public PDFExtensionAttachment() {
+ //nop
+ }
+
+ /**
+ * @return the category URI
+ * @see org.apache.fop.fo.extensions.ExtensionAttachment#getCategory()
+ */
+ public String getCategory() {
+ return CATEGORY;
+ }
+
+ /** @return type name */
+ public String getType() {
+ String className = getClass().getName();
+ return className.substring(className.lastIndexOf('.') + 3);
+ }
+
+ /**
+ * @return a string representation of this object
+ * @see java.lang.Object#toString()
+ */
+ public String toString() {
+ return getType();
+ }
+
+ /** @return element */
+ protected abstract String getElement();
+}
--- /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.extensions;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.apache.fop.util.ContentHandlerFactory;
+import org.apache.fop.util.ContentHandlerFactory.ObjectBuiltListener;
+
+/**
+ * ContentHandler (parser) for restoring PDF extension objects from XML.
+ */
+public class PDFExtensionHandler extends DefaultHandler
+ implements ContentHandlerFactory.ObjectSource {
+
+ /** Logger instance */
+ protected static Log log = LogFactory.getLog(PDFExtensionHandler.class);
+
+ private Attributes lastAttributes;
+
+ private PDFExtensionAttachment returnedObject;
+ private ObjectBuiltListener listener;
+
+ /** {@inheritDoc} */
+ public void startElement(String uri, String localName, String qName, Attributes attributes)
+ throws SAXException {
+ boolean handled = false;
+ if (PDFExtensionAttachment.CATEGORY.equals(uri)) {
+ lastAttributes = attributes;
+ handled = false;
+ if (localName.equals(PDFEmbeddedFileExtensionAttachment.ELEMENT)) {
+ //handled in endElement
+ handled = true;
+ }
+ }
+ if (!handled) {
+ if (PDFExtensionAttachment.CATEGORY.equals(uri)) {
+ throw new SAXException("Unhandled element " + localName
+ + " in namespace: " + uri);
+ } else {
+ log.warn("Unhandled element " + localName
+ + " in namespace: " + uri);
+ }
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void endElement(String uri, String localName, String qName) throws SAXException {
+ if (PDFExtensionAttachment.CATEGORY.equals(uri)) {
+ if (PDFEmbeddedFileExtensionAttachment.ELEMENT.equals(localName)) {
+ String name = lastAttributes.getValue("name");
+ String src = lastAttributes.getValue("src");
+ String desc = lastAttributes.getValue("description");
+ this.returnedObject = new PDFEmbeddedFileExtensionAttachment(name, src, desc);
+ }
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void endDocument() throws SAXException {
+ if (listener != null) {
+ listener.notifyObjectBuilt(getObject());
+ }
+ }
+
+ /** {@inheritDoc} */
+ public Object getObject() {
+ return returnedObject;
+ }
+
+ /** {@inheritDoc} */
+ public void setObjectBuiltListener(ObjectBuiltListener listener) {
+ this.listener = listener;
+ }
+}
documents. Example: the fix of marks layering will be such a case when it's done.
-->
<release version="FOP Trunk" date="TBD">
- </release>
+ <action context="Renderers" dev="JM" type="add" fixes-bug="44460" due-to="Andrejus Chaliapinas">
+ Added support for PDF File Attachments (Embedded Files).
+ </action>
+ </release>
<release version="1.0" date="21 July 2010">
<action context="Renderers" dev="JM" type="fix">
AFP Output: Fixed positioning of Java2D-based images (when GOCA is enabled).