使用iText在PDF文档中绘制一个矩形

时间:2022-05-23 21:10:23

Is there a way in iText to draw a rectangle in a PDF document?

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

5 个解决方案

#1


11  

Here is the solution. Thanks Dylan McClung.

这是解决方案。谢谢Dylan McClung。

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

#2


3  

In the .NET version I just create a table with a border. I know it isn't Java but maybe the following code will help you.

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

#3


3  

A more complete example is at: http://www.mikesdotnetting.com/Article/88/iTextSharp-Drawing-shapes-and-Graphics

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

#4


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

From API of itext

来自itext的API

#5


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

#1


11  

Here is the solution. Thanks Dylan McClung.

这是解决方案。谢谢Dylan McClung。

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

#2


3  

In the .NET version I just create a table with a border. I know it isn't Java but maybe the following code will help you.

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

#3


3  

A more complete example is at: http://www.mikesdotnetting.com/Article/88/iTextSharp-Drawing-shapes-and-Graphics

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

#4


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

From API of itext

来自itext的API

#5


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