如何使用itext创建没有边距的pdf?

时间:2015-08-19 18:29:05

标签: java itext

我正在尝试使用itext创建一个pdf,我希望它有一个沿着页面底部运行的页脚,下面没有边距或空白区域。我创建了一个文档,并将边距设置为0,但无论边缘边缘还有什么边缘。任何帮助都会很棒!这就是我所拥有的:

公共类PageTemplate {

public static void main( String[] args )
{

    Rectangle fullPage  = new Rectangle( 619, 792 );
    fullPage.setBorder( Rectangle.NO_BORDER ); // left, right, top, bottom border
    fullPage.setBorderWidth( 0 ); // a width of 5 user units
    fullPage.setUseVariableBorders( true ); // the full width will be visible

    Document document   = new Document( fullPage, 0, 0, 0, 0 );



    try
    {
        PdfWriter writer = PdfWriter.getInstance( document, new FileOutputStream( "results/template2.pdf" ) );

        document.open();

        PdfContentByte cb = writer.getDirectContent();                      
        cb.setColorFill( new Color( 52, 152, 219 ) );

        BaseFont bfBold   = BaseFont.createFont( BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED );
        Font footerFont   = new Font( bfBold, 6 );
        footerFont.setColor( new Color( 255, 255, 255 ) );

        PdfPTable footer  = new PdfPTable( 1 );
        PdfPCell footerCell = new PdfPCell();
        footer.setTotalWidth( 612 );
        footer.getDefaultCell().setBorder( PdfPCell.NO_BORDER );
        footer.getDefaultCell().setHorizontalAlignment( Element.ALIGN_LEFT );
        footer.getDefaultCell().setVerticalAlignment( Element.ALIGN_MIDDLE );
        footer.getDefaultCell().setBackgroundColor( new Color( 52, 152, 219 ) );
        footer.getDefaultCell().setPaddingLeft( 30 );
        footer.addCell( new Phrase( new Chunk( "We need to decide what to put here", footerFont ) ) );
        footer.getRow( 0 ).setMaxHeights( 18 );
        footer.completeRow();

        System.out.println( "Spacing BEFORE table: " + footer.spacingBefore() );
        System.out.println( "Spacing AFTER table: " + footer.spacingAfter() );            
        System.out.println( "Footer table has " + footer.size() + " rows" );

        footer.writeSelectedRows( 0, -1, document.left(), document.bottom() + 18, cb );


        document.newPage();

    }


    catch ( DocumentException de )
    {
        System.err.println( de.getMessage() );
    }

    catch ( IOException ioe )
    {
        System.err.println( ioe.getMessage() );
    }

    document.close();
}

}

0 个答案:

没有答案