7

iText 有没有办法在 PDF 文档中绘制一个矩形?

4

5 回答 5

13

这是解决方案。谢谢迪伦麦克朗。

PdfWriter writer = ...;
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
cb.setColorStroke(Color.black);
cb.rectangle(x,y,x1,y1);
cb.stroke();
cb.restoreState();
于 2009-08-25T17:04:34.607 回答
3

在 .NET 版本中,我只是创建了一个带边框的表格。我知道它不是 Java,但也许下面的代码会对你有所帮助。

iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.LETTER, 20, 20, 20, 20);
PdfPTable table;
PdfPCell cell;

// single element w/ border
table = new PdfPTable(1);
cell = new PdfPCell(new Phrase("BOLD WORDS", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11, Font.BOLD)));
cell.BorderWidth = 2;
cell.Padding = 5;
cell.PaddingTop = 3;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
table.SetWidthPercentage(new float[1] { 598f }, PageSize.LETTER);
table.HorizontalAlignment = Element.ALIGN_CENTER;
document.Add(table);
于 2009-08-21T13:01:06.963 回答
3

更完整的示例位于: http: //www.mikesdotnetting.com/Article/88/iTextSharp-Drawing-shapes-and-Graphics

于 2011-12-11T06:39:22.303 回答
1
public static void drawRectangle(PdfContentByte content, float width, float height) {
    content.saveState();
    PdfGState state = new PdfGState();
    state.setFillOpacity(0.6f);
    content.setGState(state);
    content.setRGBColorFill(0xFF, 0xFF, 0xFF);
    content.setLineWidth(3);
    content.rectangle(0, 0, width, height);
    content.fillStroke();
    content.restoreState();
}

来自itext的API

于 2012-08-01T18:35:56.093 回答
0
private static void rect(PdfWriter writer) {

   PdfContentByte cb = writer.getDirectContent();
   try {
       cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false), 24);
       cb.rectangle(140f,140f,280f,420f);
       cb.stroke();
   } catch (DocumentException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
   } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
   }
}
于 2016-02-01T12:58:00.410 回答