OpenPDF document content gets written into header

364 views Asked by At

I'm creating a PDF with OpenPDF 1.3.30, which gets a PdfPTable as header on every page. But the content of the document itself overwrites the the first cell (0,0). Which by the way has a rowspan.

Here is my example code:

import java.io.FileOutputStream;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;

public class TableTest {

  public static void main( final String[] args ) throws Throwable {
    try( final FileOutputStream outputStream = new FileOutputStream( "test.pdf" ) ) {
      final Document pdfDocument = new Document( PageSize.A4 );

      final PdfWriter pdfWriter = PdfWriter.getInstance( pdfDocument,outputStream );
      pdfWriter.setPdfVersion( PdfWriter.VERSION_1_7 );

      final PdfPTable header = new PdfPTable( 5 );
      header.setWidths( new int[] { 30,30,30,30,30 } );
      header.setHeaderRows( 0 );
      header.getDefaultCell().setPadding( 1 );
      header.setSplitLate( false );
      header.setSplitRows( true );

      PdfPCell cell = new PdfPCell( new Phrase( "Gets truncated" ) );
      cell.setColspan( 1 );
      cell.setRowspan( 5 );
      cell.setBorder( Rectangle.BOX );
      cell.setPadding( 1 );
      cell.setVerticalAlignment( Element.ALIGN_MIDDLE );
      cell.setHorizontalAlignment( Element.ALIGN_CENTER );
      header.addCell( cell );

      cell = new PdfPCell( new Phrase( "Text A" ) );
      cell.setColspan( 1 );
      cell.setRowspan( 1 );
      cell.setBorder( Rectangle.BOX );
      cell.setPadding( 1 );
      cell.setHorizontalAlignment( Element.ALIGN_LEFT );
      header.addCell( cell );

      cell = new PdfPCell( new Phrase( "Text B" ) );
      cell.setColspan( 2 );
      cell.setRowspan( 1 );
      cell.setBorder( Rectangle.BOX );
      cell.setPadding( 1 );
      cell.setVerticalAlignment( Element.ALIGN_MIDDLE );
      cell.setHorizontalAlignment( Element.ALIGN_CENTER );
      header.addCell( cell );

      cell = new PdfPCell( new Phrase( "Text C" ) );
      cell.setColspan( 1 );
      cell.setRowspan( 1 );
      cell.setBorder( Rectangle.BOX );
      cell.setPadding( 1 );
      cell.setVerticalAlignment( Element.ALIGN_MIDDLE );
      cell.setHorizontalAlignment( Element.ALIGN_CENTER );
      header.addCell( cell );
      
      cell = new PdfPCell( new Phrase( "" ) );
      cell.setColspan( 4 );
      cell.setRowspan( 1 );
      cell.setBorder( Rectangle.BOTTOM );
      cell.setPadding( 1 );
      cell.setVerticalAlignment( Element.ALIGN_TOP );
      cell.setHorizontalAlignment( Element.ALIGN_CENTER );
      header.addCell( cell );

      Phrase phrase = new Phrase();
      phrase.add( new Phrase( "Text D" ) );
      phrase.add( Chunk.NEWLINE );
      phrase.add( new Phrase( "Text E" ) );
      phrase.add( Chunk.NEWLINE );

      cell = new PdfPCell( phrase );
      cell.setColspan( 3 );
      cell.setRowspan( 1 );
      cell.setBorder( Rectangle.BOX );
      cell.setPadding( 1 );
      cell.setVerticalAlignment( Element.ALIGN_MIDDLE );
      cell.setHorizontalAlignment( Element.ALIGN_CENTER );
      header.addCell( cell );

      cell = new PdfPCell( new Phrase( "Text F" ) );
      cell.setColspan( 1 );
      cell.setRowspan( 1 );
      cell.setBorder( Rectangle.BOX );
      cell.setPadding( 1 );
      cell.setVerticalAlignment( Element.ALIGN_MIDDLE );
      cell.setHorizontalAlignment( Element.ALIGN_CENTER );
      header.addCell( cell );

      cell = new PdfPCell( new Phrase( "" ) );
      cell.setColspan( 4 );
      cell.setRowspan( 1 );
      cell.setBorder( Rectangle.BOX );
      cell.setPadding( 1 );
      cell.setVerticalAlignment( Element.ALIGN_TOP );
      cell.setHorizontalAlignment( Element.ALIGN_CENTER );
      header.addCell( cell );

      cell = new PdfPCell( new Phrase( "Text G" ) );
      cell.setColspan( 1 );
      cell.setRowspan( 1 );
      cell.setBorder( Rectangle.BOX );
      cell.setPadding( 1 );
      cell.setVerticalAlignment( Element.ALIGN_MIDDLE );
      cell.setHorizontalAlignment( Element.ALIGN_LEFT );
      header.addCell( cell );

      cell = new PdfPCell( new Phrase( "Text H" ) );
      cell.setColspan( 2 );
      cell.setRowspan( 1 );
      cell.setBorder( Rectangle.BOX );
      cell.setPadding( 1 );
      cell.setVerticalAlignment( Element.ALIGN_MIDDLE );
      cell.setHorizontalAlignment( Element.ALIGN_CENTER );
      header.addCell( cell );

      phrase = new Phrase();
      phrase.add( new Phrase( "Text I" ) );
      phrase.add( Chunk.NEWLINE );
      phrase.add( new Phrase( "Text J" ) );
      phrase.add( Chunk.NEWLINE );

      cell = new PdfPCell( phrase );
      cell.setColspan( 1 );
      cell.setRowspan( 1 );
      cell.setBorder( Rectangle.BOX );
      cell.setPadding( 1 );
      cell.setVerticalAlignment( Element.ALIGN_MIDDLE );
      cell.setHorizontalAlignment( Element.ALIGN_CENTER );
      header.addCell( cell );

      cell = new PdfPCell( new Phrase( "" ) );
      cell.setColspan( 6 );
      cell.setRowspan( 1 );
      cell.setBorder( Rectangle.BOTTOM );
      cell.setPadding( 1 );
      cell.setVerticalAlignment( Element.ALIGN_TOP );
      cell.setHorizontalAlignment( Element.ALIGN_CENTER );
      header.addCell( cell );

      header.setTotalWidth( pdfWriter.getPageSize().getWidth() - pdfDocument.leftMargin() - pdfDocument.rightMargin() );

      pdfDocument.setMargins( pdfDocument.leftMargin(),pdfDocument.rightMargin(),header.getTotalHeight() + pdfDocument.topMargin(),pdfDocument.bottomMargin() );

      pdfWriter.setPageEvent( new PdfPageEventHelper() {

        @Override
        public void onStartPage( final PdfWriter writer,final Document document ) {
          header.writeSelectedRows( 0,-1,document.left(),document.top() + header.getTotalHeight(),writer.getDirectContent() );
        }
      } );

      pdfDocument.open();

      final Paragraph paragraph = new Paragraph();
      paragraph.add( new Phrase( "Trucates" ) );
      paragraph.add( Chunk.NEWLINE );
      paragraph.add( new Phrase( "Cell(0,0)" ) );

      pdfDocument.add( paragraph );
      pdfDocument.close();
    }
  }
}

The resulting PDF overwrites the text 'Gets replaced' of the header table with text from the document paragraph content 'Replaces\nLogo'.

enter image description here

Any idea what I did wrong?

I even tried to use the HeaderFooter approach to set the header via pdfDocument.setHeader, but that gets somehow weird, because the header doesn't adopt to the size needed by the table.

0

There are 0 answers