问题
I found some tutorials about how to draw forms in iText, but I need to insert it into a cell, and I don´t know how. Thank you for your consideration of this matter.
回答1:
An iText Image extends Rectangle. You could make a new implementation of image.
You can create an Image from a template, and you can create a template using a content byte.
You could therefore create a template, draw a rectangle on it, create the image and then set the image on the desired cell. I have done something similar with a rectangle pattern some time back.
Edit: If you just need to fill the entire cell you can use the setBackground
and setBorder
methods.
回答2:
Since google is not giving any good responses for a complete code example of this and I spent a lot of time trying to figure out how to do this (even if Jes explanation now seems pretty clear...) I figured I'll post an example so further seekers can have a complete code example.
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(450);
PdfTemplate template = cb.createTemplate(30, 30);
template .setLineWidth(0.5f);
template .rectangle(0, 0, 17f, 17f);
template .stroke();
Image img = Image.getInstance(template);
Chunk chunk = new Chunk(img, 1f, 1f);
PdfPCell cell = new PdfPCell();
cell.addElement(chunk);
table.addCell(cell);
回答3:
PdfPCell
has a method to set an image PdfPCell#setImage(Image). Couldn't you use it?
来源:https://stackoverflow.com/questions/4429038/is-there-a-way-to-draw-a-rectangle-into-a-pdfpcell-in-itext-the-java-version