java 如何在 PDFBox 中绘制实心矩形?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16452543/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:59:19 来源:igfitidea点击:
How to draw a filled rectangle in PDFBox?
提问by Matti Kiviharju
How to draw a filled rectangle using PDFBox?
如何使用 PDFBox 绘制实心矩形?
I just can't find the right function of the PDFBox API documentation.
我只是找不到PDFBox API 文档的正确功能。
I want to draw a filled rectangle under the first row of a table:
我想在表格的第一行下绘制一个实心矩形:
public static void drawTable(PDPage page, PDPageContentStream contentStream,
float y, float margin,
String[][] content) throws IOException {
final int rows = content.length;
final int cols = content[0].length;
final float firstRowHeight = 160f;
final float rowHeight = 20f;
final float colWidth = 15f; //tableWidth/(float)cols;
final float tableWidth = colWidth * cols; //page.findMediaBox().getWidth()-(2*margin);
final float tableHeight = rowHeight * rows + firstRowHeight - rowHeight;
final float cellMargin= 5f;
/*PDRectangle rectangle = new PDRectangle();
rectangle.setLowerLeftX(10);
rectangle.setLowerLeftY(10);
rectangle.setUpperRightX(10);
rectangle.setUpperRightY(10);
page.setMediaBox(rectangle);
page.setCropBox(rectangle);*/
//draw the rows
float nexty = y ;
for (int i = 0; i <= rows; i++) {
contentStream.drawLine(margin,nexty,margin+tableWidth,nexty);
if (i<=0) {
nexty-= firstRowHeight;
} else {
nexty-= rowHeight;
}
}
//draw the columns
float nextx = margin;
for (int i = 0; i <= cols; i++) {
contentStream.drawLine(nextx,y,nextx,y-tableHeight);
nextx += colWidth;
}
//now add the text
contentStream.setFont(PDType1Font.HELVETICA_BOLD,8);
float textx = margin+cellMargin;
float texty = y-15;
//int o = content.length;
for(int i = 0; i < content.length; i++){
for(int j = 0 ; j < content[i].length; j++){
String text = content[i][j];
contentStream.beginText();
if (i<=0) {
contentStream.moveTextPositionByAmount(textx,texty-140);
contentStream.setTextRotation(90*Math.PI*0.25,textx+5,texty-140);
} else {
contentStream.moveTextPositionByAmount(textx,texty);
contentStream.setTextRotation(90*Math.PI*0.25,textx+5,texty-3);
}
contentStream.drawString(text);
contentStream.endText();
textx += colWidth;
}
if (i<=0) {
texty-=firstRowHeight;
} else {
texty-=rowHeight;
}
textx = margin+cellMargin;
}
}
采纳答案by Simon Bengtsson
回答by Matthias Braun
This is how you can draw a rectangle in PDFBox 2:
这是在 PDFBox 2 中绘制矩形的方法:
void drawRect(PDPageContentStream content, Color color, Rectangle rect, boolean fill) {
content.addRect(rect.x, rect.y, rect.width, rect.height);
if (fill) {
content.setNonStrokingColor(color);
content.fill();
} else {
content.setStrokingColor(color);
content.stroke();
}
}
Use it like this:
像这样使用它:
int startX = 20;
int startY = 30;
int width = 40;
int height = 50;
Color color = Color.BLUE;
// Draw a blue filled rectangle
drawRect(content, color, new java.awt.Rectangle(startX, startY, width, height), true);