Java 使用 iText 在 PDF 文档中绘制矩形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1311732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Draw a rectangle in a PDF document using iText
提问by Milhous
Is there a way in iText to draw a rectangle in a PDF document?
iText 有没有办法在 PDF 文档中绘制矩形?
采纳答案by Milhous
Here is the solution. Thanks Dylan McClung.
这是解决方案。谢谢迪伦麦克朗。
PdfWriter writer = ...;
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
cb.setColorStroke(Color.black);
cb.rectangle(x,y,x1,y1);
cb.stroke();
cb.restoreState();
回答by Mayo
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);
回答by Jeff S
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
回答by sreeprasad
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
回答by abh22ishek
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();
}
}