利用iText在PDF文档中画一个矩形(Draw a rectangle in a PDF docu

2019-07-03 10:22发布

是否有iText的方式画一个PDF文档中的矩形?

Answer 1:

这里是解决方案。 由于迪伦麦克朗。

PdfWriter writer = ...;
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
cb.setColorStroke(Color.black);
cb.rectangle(x,y,x1,y1);
cb.stroke();
cb.restoreState();


Answer 2:

在.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);


Answer 3:

一个更完整的例子是: http://www.mikesdotnetting.com/Article/88/iTextSharp-Drawing-shapes-and-Graphics



Answer 4:

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的



Answer 5:

   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();
            }
}


文章来源: Draw a rectangle in a PDF document using iText