Please take a look at the AddLinkImages example. At first, I planned to use WMF files only (as per your request), but I didn't find that many, so I used a PNG, some BMPs and a single WMP.
You want these images to be added just like any other object, but you also want to add an action to them. This can be achieved to wrap the image inside a Chunk
as described in chapter 2 of my book. Once you have a Chunk
, you can define a PdfAction
that will be triggered when the chunk (or in this case, the image wrapped in the chunk) is clicked:
public Chunk createImage(String src, String url) throws BadElementException, IOException {
Image img = Image.getInstance(src);
Chunk chunk = new Chunk(img, 0, 0, true);
chunk.setAction(new PdfAction(url));
return chunk;
}
Note that I set the changeLeading
parameter to true
. If I don't do that, the leading of the paragraph will be the leading of the text and as the images are usually bigger, that would result in overlapping text and images. By setting changeLeading
to true
, the leading will adapt to the height of the images.
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfContentByte cb = writer.getDirectContent();
document.add(new Paragraph("Objects with links"));
Paragraph p = new Paragraph();
p.add(createImage("resources/images/info.png", "http://itextpdf.com/"));
p.add(createImage("resources/images/dog.bmp", "http://pages.itextpdf.com/ebook-stackoverflow-questions.html"));
p.add(createImage("resources/images/fox.bmp", "http://stackoverflow.com/q/29388313/1622493"));
p.add(createImage("resources/images/butterfly.wmf", "http://stackoverflow.com/questions/tagged/itext*"));
document.add(p);
document.close();
}
