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("序號\n ", chineseB12)));
                table.addCell(cell);
              
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("項目名稱\n ", chineseB12)));
                table.addCell(cell);
                
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("申報單位\n ", chineseB12)));
                table.addCell(cell);
                
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("組別\n ", chineseB12)));
                table.addCell(cell);
                
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("計划類型\n ", chineseB12)));
                table.addCell(cell);
                
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("資金(萬元)\n ", chineseB12)));
                table.addCell(cell);
                
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("評分\n ", chineseB12)));
                table.addCell(cell);
                
                cell = new Cell();
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.addElement((new Paragraph("專家組意見\n ", 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();  
        }  

         

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM