itext A4纸张横向创建PDF

   
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;

public  class  Test{

public static void main(String[] as) throws Exception{
        Test.testRotate();
}

/**

*itext A4纸张横向创建PDF,需要加itext jar报

*http://www.cnblogs.com/qgc88/

*/
 public static  void testRotate(){  
            Document document = new Document();
        //横向
            Rectangle pageSize = new Rectangle(PageSize.A4.height(), PageSize.A4.width());
            pageSize.rotate();
            document.setPageSize(pageSize);
            
             // 页码
            Page page = new Page();
            page.setPageFormat("— %d —");
            page.setRelativeY(-10);
            page.setRelativeX(-35);
            PdfPageEventHelper eventPage[] = new PdfPageEventHelper[1];
            eventPage[0] = page;
            
            try {  
            String destPath="E:\test.pdf";
                PdfWriter writer = PdfWriter.getInstance(document,
                        new FileOutputStream(destPath));

                if (eventPage != null) {
                    for (PdfPageEventHelper event : eventPage) {
                        writer.setPageEvent(event);
                    }
                }
                document.open();  
                
                 BaseFont fontChinese = null;
                try {
                    fontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                            BaseFont.NOT_EMBEDDED);// 设置中文字体(支持中文显示)
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String titlec="意见汇总";
                  Font chinese12 = new Font(fontChinese, 12, Font.NORMAL);
                  Font chineseB12 = new Font(fontChinese, 12, Font.BOLD);
                  
                  Font chinese = new Font(fontChinese, 18, Font.BOLD);
                   Paragraph title = new Paragraph(titlec, chinese);
                   title.setAlignment(Element.ALIGN_CENTER);
                   
                document.add(title);
                
              
                Table table = new Table(8);   //8是总的列数,也可以同时指定行数和列数new Table(3,4)
                
                table.setTableFitsPage(true);//防止跨页有段开的边框(填充整页)
                
                table.setWidths(new int[]{5,20,10,10,10,10,5,30}); //每列比例
                table.setWidth(100);//表格站table 100%
                table.setBorder(0);
              
                Cell cell = new Cell();
                cell.setHeader(true);
               // cell.setColspan(8);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("序号 ", chineseB12)));
                table.addCell(cell);
              
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("项目名称 ", chineseB12)));
                table.addCell(cell);
                
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("申报单位 ", chineseB12)));
                table.addCell(cell);
                
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("组别 ", chineseB12)));
                table.addCell(cell);
                
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("计划类型 ", chineseB12)));
                table.addCell(cell);
                
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("资金(万元) ", chineseB12)));
                table.addCell(cell);
                
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("评分 ", chineseB12)));
                table.addCell(cell);
                
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("专家组意见 ", chineseB12)));
                table.addCell(cell);
            
                table.endHeaders();
             
             
                for (int i = 0; i < 20; i++) {
                Cell   cell1 = new Cell(String.valueOf(i));
                cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                //max lines ,设置成1防止换行,配合cell宽度,可以实现自动截取
              //  cell1.setMaxLines(1);
                table.addCell(cell1);
                
                Cell   cell2 = new Cell();
                cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell2.addElement(new Paragraph("项目测目测目测目测目测试1", chinese12));
                table.addCell(cell2);
                
                Cell   cell3 = new Cell();
                cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell3.addElement(new Paragraph("项目目测目测测试1", chinese12));
                table.addCell(cell3);
                
                Cell   cell4 = new Cell();
                cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell4.addElement(new Paragraph("http://www.cnblogs.com/qgc88/", chinese12));
                table.addCell(cell4);
                
                Cell   cell5 = new Cell();
                cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell5.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell5.addElement(new Paragraph("科技人才需求", chinese12));
                table.addCell(cell5);
                
                Cell   cell6 = new Cell();
                cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell6.addElement(new Paragraph("22", chinese12));
                table.addCell(cell6);
                
                Cell   cell7 = new Cell();
                cell7.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell7.addElement(new Paragraph("100.00", chinese12));
                table.addCell(cell7);
                
                Cell   cell8 = new Cell();
                cell8.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell8.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell8.addElement(new Paragraph("主审家综合意", chinese12));
                table.addCell(cell8);
                          
                }
             
                
                document.add(table);
             
            } catch (DocumentException de) {  
                System.err.println(de.getMessage());  
            } catch (IOException ioe) {  
                System.err.println(ioe.getMessage());  
            }  
            document.close();  
        }  

         

原文地址:https://www.cnblogs.com/qgc88/p/4063309.html