JAVA 產生一個表格的 PDF檔案

來源:互聯網
上載者:User

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.io.*;
import java.util.*;
import java.awt.Color;

/**
 * <p>Title: 產生PDF檔案</p>
 * <p>Description: 本執行個體通過使用iText包產生一個表格的PDF檔案</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Filename: myPDF.java</p>
 * @author 杜江
 * @version 1.0
 */
public class myPDF{
/**
 *<br>方法說明:寫PDF檔案
 *<br>輸入參數:
 *<br>傳回型別:
 */
  public void write(){
   try{
     Document document=new Document(PageSize.A4, 50, 50, 100, 50);
     Rectangle pageRect=document.getPageSize();
     PdfWriter.getInstance(document, new FileOutputStream("tables.pdf"));
     //建立漢字字型
     BaseFont bfSong = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
     Font fontSong = new Font(bfSong, 10, Font.NORMAL);
     // 增加一個浮水印
     try {
         Watermark watermark = new Watermark(Image.getInstance("test.jpg"), pageRect.left()+50,pageRect.top()-85);
         watermark.scalePercent(50);
         document.add(watermark);
     }catch(Exception e) {
    System.err.println("請查看檔案“test.jpg”是否在正確的位置?");
     }
    
      // 為頁增加頁頭資訊
     HeaderFooter header = new HeaderFooter(new Phrase("Java執行個體一百例",fontSong), false);
     header.setBorder(2);
     header.setAlignment(Element.ALIGN_RIGHT);
     document.setHeader(header);
    
   // 為頁增加頁尾資訊
     HeaderFooter footer = new HeaderFooter(new Phrase("第 ",fontSong),new Phrase(" 頁",fontSong));
     footer.setAlignment(Element.ALIGN_CENTER);
     footer.setBorder(1);
     document.setFooter(footer);

      // 開啟文檔
     document.open();
     //構造表格
     Table table = new Table(4);
     table.setDefaultVerticalAlignment(Element.ALIGN_MIDDLE);
     table.setBorder(Rectangle.NO_BORDER);
     int hws[] = {10, 20, 10, 20,};
     table.setWidths(hws);
     table.setWidth(100);
     //表頭資訊
     Cell cellmain = new Cell(new Phrase("使用者資訊",new Font(bfSong, 10, Font.BOLD,new Color(0,0,255))));
     cellmain.setHorizontalAlignment(Element.ALIGN_CENTER);
     cellmain.setColspan(4);
     cellmain.setBorder(Rectangle.NO_BORDER);
     cellmain.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
     table.addCell(cellmain);
      //分表頭資訊
     Cell cellleft= new Cell(new Phrase("收貨人資訊",new Font(bfSong, 10, Font.ITALIC,new Color(0,0,255))));
     cellleft.setColspan(2);
     cellleft.setHorizontalAlignment(Element.ALIGN_CENTER);
     table.addCell(cellleft);
     Cell cellright= new Cell(new Phrase("訂貨人資訊",new Font(bfSong, 10, Font.ITALIC,new Color(0,0,255))));
     cellright.setColspan(2);
     cellright.setHorizontalAlignment(Element.ALIGN_CENTER);
     table.addCell(cellright);
    
     //收貨和訂貨人資訊,表體內容
     table.addCell(new Phrase("姓名",fontSong));
     table.addCell(new Phrase("張三",fontSong));
     table.addCell(new Phrase("姓名",fontSong));
     table.addCell(new Phrase("李四",fontSong));

     table.addCell(new Phrase("電話",fontSong));
     table.addCell(new Phrase("23456789",fontSong));
     table.addCell(new Phrase("電話",fontSong));
     table.addCell(new Phrase("9876543",fontSong));

     table.addCell(new Phrase("郵編",fontSong));
     table.addCell(new Phrase("100002",fontSong));
     table.addCell(new Phrase("郵編",fontSong));
     table.addCell(new Phrase("200001",fontSong));

     table.addCell(new Phrase("地址",fontSong));
     table.addCell(new Phrase("北京西城區XX路XX號",fontSong));
     table.addCell(new Phrase("地址",fontSong));
     table.addCell(new Phrase("上海陸家嘴區XX路XX號",fontSong));

     table.addCell(new Phrase("電子郵件",fontSong));
     table.addCell(new Phrase("zh_san@hotmail.com",fontSong));
     table.addCell(new Phrase("電子郵件",fontSong));
     table.addCell(new Phrase("li_si@hotmail.com",fontSong));
     //將表格添加到文本中
     document.add(table);
     //關閉文本,釋放資源
     document.close();
    
   }catch(Exception e){
     System.out.println(e);  
   }
  }
/**
 *<br>方法說明:主方法
 *<br>輸入參數:
 *<br>傳回型別:
 */
  public static void main(String[] arg){
    myPDF p = new myPDF();
    p.write();
  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.