Pregunta

pdfbox / java

Im trying to draw a rectangle starting the first horizontal line in the first page and ending draw the second horizontal line in the second page.

But, When I try to draw programmaticaly the second horizontal line in the 2nd page, It draws in the first page.

How to do that ?

BT - Begin Table ET - End Table

in my bean :

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

in my pdf class :

class constructor :

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);
    }
  }
}
¿Fue útil?

Solución

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);
    }
  }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top