I
I
iText Software2019-06-03 10:05:32
Java
iText Software, 2019-06-03 10:05:32

How to add a map with an index to a PDF file?

Question. I am using Java and iText to create a PDF. Is it possible to add a map with a pointer so that the user knows where the original point is?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iText_A, 2019-06-03
@iText_A

What do you mean by "a map with a pointer so the user knows where the starting point is"? If the PDF contains a map, you can add an arrow annotation. Do you need exactly this?
I will give two examples.
This option is demonstrated in the AddPointer example:

PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
canvas.setStrokeColor(Color.RED)
        .setLineWidth(3)
        .moveTo(220, 330)
        .lineTo(240, 370)
        .arc(200, 350, 240, 390, 0, (float) 180)
        .lineTo(220, 330)
        .closePathStroke()
        .setFillColor(Color.RED)
        .circle(220, 370, 10)
        .fill();

If the coordinates are known, then a red pointer can be drawn from lines and curves, as in the picture below (red pointer near the Cambridge Innovation Center):
Map with a pointer
This option is demonstrated in the AddPointerAnnotation example:
Rectangle rect = new Rectangle(220, 350, 255, 245);
PdfLineAnnotation lineAnnotation = new PdfLineAnnotation(rect, new float[]{225, 355, 470, 590});
lineAnnotation.setTitle(new PdfString("You are here:"));
lineAnnotation.setContents("Cambridge Innovation Center");
lineAnnotation.setColor(Color.RED);
lineAnnotation.setFlag(PdfAnnotation.PRINT);
 
PdfDictionary borderStyle = new PdfDictionary();
borderStyle.put(PdfName.S, PdfName.S);
borderStyle.put(PdfName.W, new PdfNumber(5));
lineAnnotation.setBorderStyle(borderStyle);
 
PdfArray le = new PdfArray();
le.add(new PdfName("OpenArrow"));
le.add(new PdfName("None"));
lineAnnotation.put(new PdfName("LE"), le);
lineAnnotation.put(new PdfName("IT"), new PdfName("LineArrow"));
 
pdfDoc.getFirstPage().addAnnotation(lineAnnotation);

The result is an annotation (not part of the real content, but an interactive layer overlaid on top of the real content):
Map with annotation
Layer is interactive: when the user clicks on the annotation, additional information appears:
Map with open annotation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question