Reputation: 31
With a for-loop I iterate over a set of coordinates stored in matrix X, and draw circles at indicated positions (itextpdf used):
...
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(fileName));
Document document = new Document(pdfDoc, new PageSize(one));
PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
for (int i = 0; i < total; i++) {
canvas.circle(X[d * i + axisX], X[d * i + axisY], 1.0);
canvas.fillStroke();
// -- numbers (i+1) should be drawn beside the circles
}
How could I use the above for-loop and write the numbers beside the circles, as indicated above?
Upvotes: 0
Views: 294
Reputation: 77606
There are many different ways to add text at absolute positions with iText 7.
Since you already have a PdfCanvas
object, you can use the very low-level approach that consists of writing PDF syntax line by line:
canvas.beginText();
canvas.moveText(x, y);
canvas.showText("1");
canvas.endText();
This is explained in chapter 3 of the iText 7 jump-start tutorial.
Writing low-level PDF syntax is error-prone when you don't know the PDF reference by heart. There is also an easier way:
document.showTextAligned(new Paragraph("1"), x, y, TextAlignment.CENTER);
Now you don't have to worry about constructing a text object using beginText()
and endText()
; iText takes care of all the low-level syntax.
This is explained in chapter 2 of the building blocks tutorial.
Upvotes: 1