在第二页完成绘制pdfbox矩形

时间:2014-01-27 11:46:36

标签: pdf pdfbox

pdfbox / java

我试图绘制一个矩形,从第一页开始第一条水平线,然后在第二页画出第二条水平线。

但是,当我尝试在第二页中绘制programmaticaly第二条水平线时,它会在第一页中绘制。

怎么做?

BT - 开始表 ET - 结束表

在我的bean中

pdf.BT();
    pdf.drawText(arraylist with a lof of text lines);
pdf.ET();

在我的pdf课程中:

类构造函数:

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

1 个答案:

答案 0 :(得分:0)

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