You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by ma...@zf.com on 2013/08/21 16:05:54 UTC
Changing a link to an attached file
Hi,
Maybe somebody is able to help me with this small issue.
I have to embed all external file which are referenced by the pdf
into the pdf. (this was really easy with pdfbox)
Now, I have to change the external link to the attached file. So I get stuck,
because I didn't find any hint how to do this.
-----------------------------8<----------------------------------------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
import org.apache.pdfbox.pdmodel.interactive.action.type.PDAction;
import org.apache.pdfbox.pdmodel.interactive.action.type.PDActionGoTo;
import org.apache.pdfbox.pdmodel.interactive.action.type.PDActionURI;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDNamedDestination;
public class FileEmbed {
private String pathToFile;
private final PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
public FileEmbed(String pathToFile) {
super();
this.pathToFile = pathToFile;
}
public String getPathToFile() {
return pathToFile;
}
public void setPathToFile(String pathToFile) {
this.pathToFile = pathToFile;
}
public void embedFiles() throws Exception {
PDDocument doc = null;
try {
doc = PDDocument.load(pathToFile);
List allPages = doc.getDocumentCatalog().getAllPages();
for (int i = 0; i < allPages.size(); i++) {
PDPage page = (PDPage) allPages.get(i);
List annotations = page.getAnnotations();
for (int j = 0; j < annotations.size(); j++) {
PDAnnotation annot = (PDAnnotation) annotations.get(j);
if (annot instanceof PDAnnotationLink) {
PDAnnotationLink link = (PDAnnotationLink) annot;
PDAction action = link.getAction();
if (action instanceof PDActionURI) {
PDActionURI uri = (PDActionURI) action;
String oldURI = uri.getURI();
System.out.println(oldURI);
addToAttachment(oldURI, doc);
String newURI = "computer1.wmf";
System.out.println("Page " + (i + 1)
+ ": Replacing " + oldURI + " with "
+ newURI);
uri.setURI(newURI);
PDDocumentNameDictionary names = doc
.getDocumentCatalog().getNames();
PDActionGoTo newAct = new PDActionGoTo();
PDNamedDestination pd = new PDNamedDestination();
// pd.setNamedDestination("text.txt");
newAct.setDestination(pd);
link.setDestination(pd);
// link.setAction(newAct);
}
}
}
doc.save("new" + pathToFile);
}
} finally {
if (doc != null) {
doc.close();
}
}
}
private void addToAttachment(String oldURI, PDDocument doc)
throws IOException {
PDComplexFileSpecification fs = new PDComplexFileSpecification();
String[] filename = oldURI.split("/");
System.out.println(filename[filename.length - 1]);
fs.setFile(filename[filename.length - 1]);
InputStream is = new FileInputStream(new File(oldURI.replace(
"file:///", "")));
PDEmbeddedFile ef = new PDEmbeddedFile(doc, is);
ef.setSubtype("test/plain");
ef.setSize((int) new File(oldURI.replace("file:///", "")).length());
ef.setCreationDate(new GregorianCalendar());
fs.setEmbeddedFile(ef);
// now add the entry to the embedded file tree and set in the document.
Map efMap = new HashMap();
efMap.put("My first attachment", fs);
efTree.setNames(efMap);
// attachments are stored as part of the "names" dictionary in the
// document catalog
PDDocumentNameDictionary names = new PDDocumentNameDictionary(
doc.getDocumentCatalog());
names.setEmbeddedFiles(efTree);
doc.getDocumentCatalog().setNames(names);
}
}
----------------------------->8----------------------------------------------------------
I don't know how to set a Nameddestination on an embedded file.
Kind regards
Markus Sticker
Forschung und Entwicklung ZF Konzern/Research and Development ZF Group
Infrastruktur/Infrastructure (DTEP4)
ZF Friedrichshafen AG
88038 Friedrichshafen, Deutschland/Germany
Telefon/Phone +49 7541 77-7644, Telefax/Fax +49 7541 77-907644
Markus.Sticker.Epos@zf.com<ma...@zf.com>
Vorsitzender des Aufsichtsrats/Chairman of the Supervisory Board: Prof. Dr. Giorgio Behr
Vorstand/Board of Management: Dr. Stefan Sommer (Vorsitzender/CEO), Dr. Konstantin Sauer, Jürgen Holeksa, Michael Hankel, Wilhelm Rehm, Rolf Lutz
Sitz/Headquarters: Friedrichshafen
Handelsregistereintrag Amtsgericht Ulm HRB 630206/Trade register of the municipal court of Ulm HRB 630206
AW: Changing a link to an attached file
Posted by ma...@zf.com.
Hi,
sure. I have taken this to build my first tests.
But as I mentioned, my question is about
changing the external link to a link referring to an attached file
not embedding files, this works very well with pdfbox.
There is no link to the attached file within this sample.
BR
Markus Sticker
-----Ursprüngliche Nachricht-----
Von: Andreas Lehmkuehler [mailto:andreas@lehmi.de]
Gesendet: Mittwoch, 21. August 2013 19:14
An: users@pdfbox.apache.org
Betreff: Re: Changing a link to an attached file
Hi,
Am 21.08.2013 16:05, schrieb markus.sticker.epos@zf.com:
> Hi,
>
> Maybe somebody is able to help me with this small issue.
Did you see the example [1] on how to add embedded files?
> I have to embed all external file which are referenced by the pdf into
> the pdf. (this was really easy with pdfbox)
>
> Now, I have to change the external link to the attached file. So I get
> stuck, because I didn't find any hint how to do this.
>
> -----------------------------8<---------------------------------------
> -------------------
> SNIP
BR
Andreas Lehmkühler
[1]
http://svn.apache.org/repos/asf/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java
Re: Changing a link to an attached file
Posted by Andreas Lehmkuehler <an...@lehmi.de>.
Hi,
Am 21.08.2013 16:05, schrieb markus.sticker.epos@zf.com:
> Hi,
>
> Maybe somebody is able to help me with this small issue.
Did you see the example [1] on how to add embedded files?
> I have to embed all external file which are referenced by the pdf
> into the pdf. (this was really easy with pdfbox)
>
> Now, I have to change the external link to the attached file. So I get stuck,
> because I didn’t find any hint how to do this.
>
> -----------------------------8<----------------------------------------------------------
> SNIP
BR
Andreas Lehmkühler
[1]
http://svn.apache.org/repos/asf/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java