在第二页完成绘制pdfbox矩形
pdfbox/java 我想画一个矩形,从第一页的第一条水平线开始,到第二页的第二条水平线结束 但是,当我试图在第二页中画第二条水平线时,它在第一页中画 怎么做 BT-开始表 ET-结束表 在我的bean中:在第二页完成绘制pdfbox矩形,pdf,pdfbox,Pdf,Pdfbox,pdfbox/java 我想画一个矩形,从第一页的第一条水平线开始,到第二页的第二条水平线结束 但是,当我试图在第二页中画第二条水平线时,它在第一页中画 怎么做 BT-开始表 ET-结束表 在我的bean中: pdf.BT(); pdf.drawText(arraylist with a lof of text lines); pdf.ET(); public generatePDF(Integer pageRotation){ try { pdDocto =
pdf.BT();
pdf.drawText(arraylist with a lof of text lines);
pdf.ET();
public generatePDF(Integer pageRotation){
try {
pdDocto = new PDDocument();
font = PDType1Font.TIMES_ROMAN;
pdPage = new PDPage();
pdPage.setMediaBox(PDPage.PAGE_SIZE_A4);
this.rotation = pageRotation;
pdPage.setRotation(rotation);
pdDocto.addPage(pdPage);
this.getContentStream();
x = new Float(0.0);
y = new Float(0.0);
} catch (IOException e) {
e.printStackTrace();
}
}
public void BT(String alinhamento,Integer Width){
this.BT = true;
this.WidthBT = Width;
}
public void ET(){
this.ET = true;
}
public PDPageContentStream getContentStream() throws IOException {
contentStream = new PDPageContentStream(pdDocto, pdPage,true,true);
if(this.rotation == 90){
contentStream.concatenate2CTM(0, 1, -1, 0, this.pdPage.getMediaBox().getWidth(), 0);
}
contentStream.setFont( font, 11 );
return contentStream;
}
public void drawText(){
if(this.getBT() != null){
if(this.getBT() == true){
contentStream.drawLine(this.getX(), this.getY(), this.getX(), this.getX() + 50);
this.setBT(false);
}else if(this.getET() == true){
contentStream.drawLine(this.getX(), this.getY(), this.getX(), this.getX() + 50);
this.setET(false);
}
}
for (int i = 0; i < numberOfLines; i++) {
if (y <= marginTopBottom) {
contentStream.close(); //fecha o antigo contentStream da primeira página.
pdPage = new PDPage();
pdPage.setMediaBox(PDPage.PAGE_SIZE_A4);
pdPage.setRotation(this.rotation);
pdDocto.addPage(pdPage);
this.setPdPage(pdPage);
this.getContentStream();
y = yOriginal + this.marginTopBottom;
this.setY(y);
this.setPdPage(pdPage);
}
if (i < numberOfLines ) {
contentStream.beginText();
contentStream.setFont(font, fontSize);
y -= this.alturaLinha;
contentStream.moveTextPositionByAmount( x , y - height);
contentStream.drawString(lines.get(i));
contentStream.endText();
this.setY(y);
}
}
}
在我的pdf类中:
pdf.BT();
pdf.drawText(arraylist with a lof of text lines);
pdf.ET();
public generatePDF(Integer pageRotation){
try {
pdDocto = new PDDocument();
font = PDType1Font.TIMES_ROMAN;
pdPage = new PDPage();
pdPage.setMediaBox(PDPage.PAGE_SIZE_A4);
this.rotation = pageRotation;
pdPage.setRotation(rotation);
pdDocto.addPage(pdPage);
this.getContentStream();
x = new Float(0.0);
y = new Float(0.0);
} catch (IOException e) {
e.printStackTrace();
}
}
public void BT(String alinhamento,Integer Width){
this.BT = true;
this.WidthBT = Width;
}
public void ET(){
this.ET = true;
}
public PDPageContentStream getContentStream() throws IOException {
contentStream = new PDPageContentStream(pdDocto, pdPage,true,true);
if(this.rotation == 90){
contentStream.concatenate2CTM(0, 1, -1, 0, this.pdPage.getMediaBox().getWidth(), 0);
}
contentStream.setFont( font, 11 );
return contentStream;
}
public void drawText(){
if(this.getBT() != null){
if(this.getBT() == true){
contentStream.drawLine(this.getX(), this.getY(), this.getX(), this.getX() + 50);
this.setBT(false);
}else if(this.getET() == true){
contentStream.drawLine(this.getX(), this.getY(), this.getX(), this.getX() + 50);
this.setET(false);
}
}
for (int i = 0; i < numberOfLines; i++) {
if (y <= marginTopBottom) {
contentStream.close(); //fecha o antigo contentStream da primeira página.
pdPage = new PDPage();
pdPage.setMediaBox(PDPage.PAGE_SIZE_A4);
pdPage.setRotation(this.rotation);
pdDocto.addPage(pdPage);
this.setPdPage(pdPage);
this.getContentStream();
y = yOriginal + this.marginTopBottom;
this.setY(y);
this.setPdPage(pdPage);
}
if (i < numberOfLines ) {
contentStream.beginText();
contentStream.setFont(font, fontSize);
y -= this.alturaLinha;
contentStream.moveTextPositionByAmount( x , y - height);
contentStream.drawString(lines.get(i));
contentStream.endText();
this.setY(y);
}
}
}
类构造函数:
pdf.BT();
pdf.drawText(arraylist with a lof of text lines);
pdf.ET();
public generatePDF(Integer pageRotation){
try {
pdDocto = new PDDocument();
font = PDType1Font.TIMES_ROMAN;
pdPage = new PDPage();
pdPage.setMediaBox(PDPage.PAGE_SIZE_A4);
this.rotation = pageRotation;
pdPage.setRotation(rotation);
pdDocto.addPage(pdPage);
this.getContentStream();
x = new Float(0.0);
y = new Float(0.0);
} catch (IOException e) {
e.printStackTrace();
}
}
public void BT(String alinhamento,Integer Width){
this.BT = true;
this.WidthBT = Width;
}
public void ET(){
this.ET = true;
}
public PDPageContentStream getContentStream() throws IOException {
contentStream = new PDPageContentStream(pdDocto, pdPage,true,true);
if(this.rotation == 90){
contentStream.concatenate2CTM(0, 1, -1, 0, this.pdPage.getMediaBox().getWidth(), 0);
}
contentStream.setFont( font, 11 );
return contentStream;
}
public void drawText(){
if(this.getBT() != null){
if(this.getBT() == true){
contentStream.drawLine(this.getX(), this.getY(), this.getX(), this.getX() + 50);
this.setBT(false);
}else if(this.getET() == true){
contentStream.drawLine(this.getX(), this.getY(), this.getX(), this.getX() + 50);
this.setET(false);
}
}
for (int i = 0; i < numberOfLines; i++) {
if (y <= marginTopBottom) {
contentStream.close(); //fecha o antigo contentStream da primeira página.
pdPage = new PDPage();
pdPage.setMediaBox(PDPage.PAGE_SIZE_A4);
pdPage.setRotation(this.rotation);
pdDocto.addPage(pdPage);
this.setPdPage(pdPage);
this.getContentStream();
y = yOriginal + this.marginTopBottom;
this.setY(y);
this.setPdPage(pdPage);
}
if (i < numberOfLines ) {
contentStream.beginText();
contentStream.setFont(font, fontSize);
y -= this.alturaLinha;
contentStream.moveTextPositionByAmount( x , y - height);
contentStream.drawString(lines.get(i));
contentStream.endText();
this.setY(y);
}
}
}
public generatePDF(整型页面旋转){
试一试{
pdDocto=新的PDDocument();
font=PDType1Font.TIMES_-ROMAN;
pdPage=新的pdPage();
pdPage.setMediaBox(pdPage.PAGE_大小_A4);
this.rotation=页面旋转;
设置旋转(旋转);
pdDocto.addPage(pdPage);
这是一个.getContentStream();
x=新浮点数(0.0);
y=新浮点数(0.0);
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共void BT(字符串alinhamento,整数宽度){
this.BT=true;
此.WidthBT=宽度;
}
公共空间{
this.ET=true;
}
公共PDPageContentStream getContentStream()引发IOException{
contentStream=新的PDPageContentStream(pdDocto,pdPage,true,true);
如果(this.rotation==90){
concatenate2CTM(0,1,-1,0,this.pdPage.getMediaBox().getWidth(),0);
}
setFont(字体,11);
返回contentStream;
}
public void drawText(){
if(this.getBT()!=null){
if(this.getBT()==true){
contentStream.drawLine(this.getX()、this.getY()、this.getX()、this.getX()+50);
此.setBT(假);
}else if(this.getET()==true){
contentStream.drawLine(this.getX()、this.getY()、this.getX()、this.getX()+50);
此.setET(false);
}
}
对于(int i=0;ipublic generatePDF(整数页面旋转){
试一试{
pdDocto=新的PDDocument();
font=PDType1Font.TIMES_-ROMAN;
pdPage=新的pdPage();
pdPage.setMediaBox(pdPage.PAGE_大小_A4);
this.rotation=页面旋转;
设置旋转(旋转);
pdDocto.addPage(pdPage);
这是一个.getContentStream();
x=新浮点数(0.0);
y=新浮点数(0.0);
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共void BT(字符串alinhamento,整数宽度){
this.BT=true;
此.WidthBT=宽度;
}
公共空间{
this.ET=true;
}
公共PDPageContentStream getContentStream()引发IOException{
contentStream=新的PDPageContentStream(pdDocto,pdPage,true,true);
如果(this.rotation==90){
concatenate2CTM(0,1,-1,0,this.pdPage.getMediaBox().getWidth(),0);
}
setFont(字体,11);
返回contentStream;
}
public void drawText(){
if(this.getBT()!=null){
if(this.getBT()==true){
contentStream.drawLine(this.getX()、this.getY()、this.getX()、this.getX()+50);
此.setBT(假);
}else if(this.getET()==true){
contentStream.drawLine(this.getX()、this.getY()、this.getX()、this.getX()+50);
此.setET(false);
}
}
对于(int i=0;i 如果(y),我假设您在某种程度上仍在处理第一页的内容流,但是您的伪代码使您很难确定任何内容。因此,请将实际代码缩减为一个小的但仍然可以运行的示例来说明问题。如何生成df
方法BT()
和ET()
工作?更重要的是:gettergetBT()
和getET()
何时返回true
或null
?@mkl更新的代码public void BT(String alinhamento,Integer Width){this.BT=true;this.WidthBT=Width;}public void ET(){this.ET=true;}成员ET
和BT
是如何初始化的?已经说过,你的线条绘制命令的参数在我看来很奇怪,contentStream.drawLine(this.getX(),this.getY(),this.getX(),this.getX()+50)
-因为开始和结束X是相同的,这不是水平的,而是垂直的,而结束Y(即this.getX()+50
)看起来更奇怪。没关系。它现在使用我在stack论坛中粘贴的相同代码工作得很好。我在我的bean方法中遇到了一个逻辑错误。无论如何,谢谢:)