现在我使用pdfCanvas绘制矩形,代码如下:
PdfPage页面= pdf.getFirstPage();
PdfCanvas canvas = new PdfCanvas(page, true);
float x = 35;
float y = 480;
canvas.rectangle(x, y, 30, 30).stroke();
实际上,我想将矩形添加到表格中
Table table = new Table(2);
table.setWidth(261.5f);
iText 5,可以将画布设置为图像对象并添加到表格中。但是在iText 7中,该功能不起作用。如何在iText7中将图片绘制到表格上?
最佳答案
您可以从独立的PdfCanvas
创建XObject
,将其转换为XObject
,然后将Image
添加到Image
:
Rectangle boundingBox = new Rectangle(20,470,50,50);
PdfFormXObject xObject = new PdfFormXObject(boundingBox);
xObject.makeIndirect(pdfDoc);//Make sure the XObject gets added to the document
PdfCanvas canvas = new PdfCanvas(xObject, pdfDoc);//Create a canvas from the XObject
canvas.setStrokeColor(Color.BLUE).setLineWidth(3f).rectangle(35, 480, 30, 30).stroke();
Image rect = new Image(xObject);
table.addCell(rect);