⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdftestutil.java

📁 itext操作创建报表的例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package pdf;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.TreeSet;

import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTable;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class PDFTestUtil  extends  PdfPageEventHelper {
	


    private static PdfTemplate template ; 
    private static BaseFont bfChinese;
    public static BaseFont arial;
    private static Font fontChinese;
    private static Font fontCN;
    private static PdfContentByte cb;
    private static Font normalFont = new Font(arial, 20, Font.BOLD);
    Paragraph paragraph = null;
    private int temp =0;
	/** An Image that goes in the header. */
    public Image headerImage;
    /** The headertable. */

    /** The Graphic state */
    public PdfGState gstate;
    /** A template that will hold the total number of pages. */
    public PdfTemplate tpl;
    /** The font that will be used. */
    public BaseFont helv;
    
    public static Table getEmptyTable()
    {
        Table emptyTable = null;
        try
        {       
            emptyTable = new Table(1);
            emptyTable.setWidth(100);
            emptyTable.setBorder(Rectangle.NO_BORDER);
            
            Cell c = new Cell(new Phrase("\n", normalFont));
            c.setBorder(Rectangle.NO_BORDER);
            emptyTable.addCell(c);
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }       
        return emptyTable;
    }   
    
//  Override iText's class to handle total page number.
	  /**
   * @see com.lowagie.text.pdf.PdfPageEventHelper#onOpenDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
   */
  public void onOpenDocument(PdfWriter writer, Document document) {

          try {
              // initialization of the template
            tpl = writer.getDirectContent().createTemplate(100, 100);
            tpl.setBoundingBox(new Rectangle(-20, -20, 100, 100));
		} catch (Exception e) {
			e.printStackTrace();
		}
  }    
  
  /**
   * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
   */
  public void onEndPage(PdfWriter writer, Document document) {

      // write the headertable    
      try {
    	  
    	  temp = writer.getPageNumber();
	
    	  PdfPTable table = new PdfPTable(4);//fourth columns

          int headerwidths[] =  {40, 30, 10,20}; // percentage
          table.setWidths(headerwidths);
          table.setWidthPercentage(100);
          table.getDefaultCell().setBorder(0);
          table.getDefaultCell().setHorizontalAlignment(0);
          
          //first Row
          PdfPCell newCell = new PdfPCell();
          newCell.addElement(new Paragraph(" "));
          newCell.setBorder(Rectangle.NO_BORDER);
          table.addCell(newCell);
          
          table.getDefaultCell().setHorizontalAlignment(2);
          newCell = new PdfPCell();
  		  newCell.setBorder(Rectangle.NO_BORDER);
  		  Paragraph paragraph = new Paragraph("本页合计",fontCN);
  		  paragraph.setAlignment(Rectangle.ALIGN_CENTER);
  		  newCell.addElement(paragraph);
          table.addCell(newCell);
          
          table.getDefaultCell().setHorizontalAlignment(2);
          newCell = new PdfPCell();
  		  newCell.setBorder(Rectangle.NO_BORDER);
  		  paragraph = new Paragraph("2,771");
  		  paragraph.setAlignment("right");
  		  newCell.addElement(paragraph);
          table.addCell(newCell);
          
          newCell = new PdfPCell();
  		 newCell.setBorder(Rectangle.NO_BORDER);
 		  paragraph = new Paragraph("267,362,55");
  		  paragraph.setAlignment("right");
  		  newCell.addElement(paragraph);
          table.addCell(newCell);
       
          //2nd Row
          table.getDefaultCell().setBorder(1);
          PdfPCell second = new PdfPCell();
          second.setColspan(4);
          second.setBorder(Rectangle.BOTTOM);
          table.addCell(second);
      

      
          //fourth Row
        PdfPCell fourth = new PdfPCell();
  	    fourth.setBorder(Rectangle.NO_BORDER);
  	    fourth.addElement(new Paragraph(" "));
      	table.addCell(fourth);
      	
      	table.getDefaultCell().setHorizontalAlignment(Rectangle.RIGHT);
  		fourth.setBorder(Rectangle.NO_BORDER);
  		paragraph = new Paragraph("订购方签名及戳章:",fontCN);
  		paragraph.setAlignment(Rectangle.ALIGN_RIGHT);
  		fourth.addElement(paragraph);
  		table.addCell(fourth);
  		
          table.getDefaultCell().setBorder(1);
          fourth = new PdfPCell();
          fourth.setBorder(Rectangle.BOTTOM);
          fourth.setColspan(2);
          table.addCell(fourth);
 
      //five Row
        PdfPCell five = new PdfPCell();
        five.addElement(new Paragraph(" "));
        five.setBorder(Rectangle.NO_BORDER);
  	    table.addCell(five);
        five = new PdfPCell();
        five.setColspan(3);
  		five.setBorder(Rectangle.NO_BORDER);
  		paragraph = new Paragraph("日期             :                   年                            月                     日",fontCN);
  		paragraph.setAlignment(Rectangle.ALIGN_RIGHT);
  		five.addElement(paragraph);
  		table.addCell(five);

          document.add(table);
       
	      PdfContentByte cb = writer.getDirectContent();
	      cb.saveState();
	      
	     String text = "**注意:此报表内容含本公司机密资料,严根禁向无关人透露!**  Page " + writer.getPageNumber() + " of ";
	      float textSize = bfChinese.getWidthPoint(text, 12);
	      float textBase = document.bottom() - 20;
	      cb.beginText();
	      cb.setFontAndSize(bfChinese, 12);
	      float adjust = bfChinese.getWidthPoint("0", 12);
	      
	      cb.setTextMatrix(document.right() - textSize - adjust, textBase);
	      cb.showText(text);
	      cb.endText();
	      cb.addTemplate(tpl, document.right() - adjust, textBase);
	      cb.saveState();
	  } catch (Exception e) {

		e.printStackTrace();
	 } 

  }
  
  /**
   * @see com.lowagie.text.pdf.PdfPageEventHelper#onStartPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
   */
  public void onStartPage(PdfWriter writer, Document document) {

	   Paragraph paragraph = new Paragraph("许可经销商品合同",fontCN);
	   paragraph.setAlignment("center");
	   try {
		document.add(paragraph);
		document.add(getEmptyTable());
		document.add(createHeader(fontCN));   
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}


  }
  
  /**
   * @see com.lowagie.text.pdf.PdfPageEventHelper#onCloseDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
   */
  public void onCloseDocument(PdfWriter writer, Document document) {
	  
	 if((temp) == (writer.getPageNumber()-1)){

	 }
     tpl.beginText();
     tpl.setFontAndSize(bfChinese, 12);
     tpl.setTextMatrix(0, 0);
     tpl.showText(Integer.toString(writer.getPageNumber() - 1));
     tpl.endText();
  }




	public static void main(String[] args) throws Exception {
	
		bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		fontCN = new com.lowagie.text.Font(bfChinese, 12,com.lowagie.text.Font.NORMAL);

		

        // step 1
		Document document =  new Document(PageSize.A4, 30, 30, 30, 100);  //定义纸张类型及方向,页边距
	    //step2    
		PdfWriter  writer = PdfWriter.getInstance(document,new FileOutputStream("Contract.pdf"));
        writer.setPageEvent(new PDFTestUtil());	
        // step3
		document.open();
	    //step 4	   

        for(int i =0 ;i<10; i++){
        	document.add(createBody(fontCN));//body
        }
        
        // step 5: we close the document (the outputstream is also closed internally)
        document.close();


	}
	public static PdfPTable createBody(Font fontCN) throws DocumentException{
	    Paragraph paragraph =null;
		PdfPTable table = new PdfPTable(4);//fourth columns
        int headerwidths[] = {20 ,50, 10,20}; // percentage
        table.setWidths(headerwidths);
        table.setWidthPercentage(100);
        table.getDefaultCell().setBorder(0);
        
        table.getDefaultCell().setHorizontalAlignment(0);
      
        PdfPTable headerTable = new PdfPTable(2);
        headerTable.setWidthPercentage(20);
        headerTable.getDefaultCell().setBorder(0);
        headerTable.getDefaultCell().setHorizontalAlignment(1);
        PdfPCell newCell = new PdfPCell();
        //first row
		newCell.setBorder(Rectangle.NO_BORDER);
		newCell.setColspan(2);
		Paragraph prargraph = new Paragraph("310094030");
		prargraph.setAlignment("center");
		newCell.addElement(prargraph);
		headerTable.getDefaultCell().setHorizontalAlignment(1);
		headerTable.addCell(newCell);
		//2nd row
		newCell = new PdfPCell();
		newCell.setBorder(Rectangle.NO_BORDER);
		newCell.setColspan(2);
		prargraph = new Paragraph("第3期",fontCN);
		prargraph.setAlignment("center");
		newCell.addElement(prargraph);
		headerTable.addCell(newCell);
		//three row
		newCell = new PdfPCell();
		newCell.setBorder(Rectangle.NO_BORDER);
		newCell.addElement(new Paragraph("进货价",fontCN));
		headerTable.addCell(newCell);
		newCell = new PdfPCell();
		newCell.setBorder(Rectangle.NO_BORDER);
		newCell.addElement(new Paragraph("零售价",fontCN));
		headerTable.addCell(newCell);
		newCell = new PdfPCell();
		//forth row
		newCell.setBorder(Rectangle.NO_BORDER);
		prargraph = new Paragraph("53.55");
		prargraph.setAlignment("left");
		newCell.addElement(prargraph);
		headerTable.addCell(newCell);
		newCell = new PdfPCell();
		newCell.setBorder(Rectangle.NO_BORDER);
		prargraph = new Paragraph("119.00");
		prargraph.setAlignment("left");
		newCell.addElement(prargraph);
		headerTable.addCell(newCell);
        table.addCell(headerTable);//first column
        
        PdfPTable sizeTable = new PdfPTable(6);
        //first row
        
        PdfPCell sizeCell = new PdfPCell();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -