0

I haven't been able to find a solution to this iText problem either here in the iText In Action book, and would appreciate suggestions. What I want to do is include technical diagrams, composed of rectangles, lines, etc. in the PDF such that the diagram can float like an image would, but also such that I can associate PDF actions with the graphics objects such as the rectangles. Do I need to extend iText to do this?

For now, I am drawing the graphics with a WMF package and inserting a WMF image, but I cannot associate actions that way or put the graphical objects in layers.

  • Can you elaborate? Which PDF actions are we talking about and what would trigger those actions? Right now, your question sounds like a duplicate of http://stackoverflow.com/questions/27083206/itextshape-clickable-polygon-or-path but I didn't vote to close the question because I'm not sure. If it is a duplicate, you have your answer. If it's not, please explain why not. – Bruno Lowagie Apr 01 '15 at 10:30
  • That looks like a step in the right direction. What I want to do is have UML diagrams in the document. They need to "float" rather than be at absolute page positions. I want to be able to click on a rectangle representing a class, and have an action, such as goto page where the class is defined, or perhaps pop up an annotation with the description. It looks like the example you referenced puts the polygon at a fixed location. – Hops Gegangen Apr 01 '15 at 13:06
  • There is no such thing as floating images in PDF. Images are always added at absolute positions. Are you sure you aren't talking about annotations? If I were you, I'd work with square annotations. – Bruno Lowagie Apr 01 '15 at 13:09
  • Can you show an existing PDF that behaves the way you describe? That would help us to know what you're talking about. If you can't find such a PDF, are you sure that the functionality you're asking for exists? – Bruno Lowagie Apr 01 '15 at 13:14
  • I understand PDF itself is based on absolute locations, iText can place an image at the next vertical position. If I can do the same thing with diagrams based on annotations, that could work. I wrote my own PDF library years ago, but have forgotten some of it. I'm hoping to use iText rather than go back and enhance it. I see the objects I created at the time were annotations: <> – Hops Gegangen Apr 01 '15 at 13:29
  • Maybe this could work: create the diagram image with the WMF Image, which "floats" the way I want. If I know the vertical position of the image, since I know the relative locations of the objects I put in the image, I should be able to overlay some invisible (no border) annotations, right>? – Hops Gegangen Apr 01 '15 at 13:36
  • Well, start by showing me a PDF with a floating WMF image, because I've never seen such a PDF (unless I misunderstand the concept of floating). I can then explain you how to add an image to this WMF. I don't know who downvoted your question, but I guess the reason for that vote was the fact that you use confusing language instead of concrete code. I am now inclined to cast a close vote because your question is unclear. – Bruno Lowagie Apr 01 '15 at 13:39
  • When I do this, iText puts the image at the current location -- I don't tell it where to put it in absolute terms: ImgWMF wmf = new ImgWMF(bytes); document.add(wmf); – Hops Gegangen Apr 01 '15 at 13:47
  • Is that what you mean by floating? Please confirm. – Bruno Lowagie Apr 01 '15 at 13:48
  • OK, give me a moment to prepare an example. – Bruno Lowagie Apr 01 '15 at 13:57

1 Answers1

0

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();
}

enter image description here

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks. I guess I really am not being clear, however I try. I am creating the WMF image to contain numerous rectangles, each of which requires a separate action. But I think you have pointed me in the direction of a solution. I will post it if it works. Basically, I will insert the image as usual, then capture the current vertical position. Since I know where I put the rectangles in the WMF, I should be able to overlay annotations with associated actions. – Hops Gegangen Apr 01 '15 at 14:39
  • If you wrap the `Image` inside a `Chunk`, you can get the position of the image using a generic tag event. You could even draw all the shapes in such an event. – Bruno Lowagie Apr 01 '15 at 14:41