2013-08-27 64 views
0

我試圖使用iText創建PDF文檔。我跟着THIS不錯的教程,並試圖創建單頁 pdf文檔,其中有一個表。在本教程中,作者使用addMetaData,addTitlePage和addContent等單獨的方法保留表的表創建。我也會分開保存它們,但我是iText的新手,目前我卡住了。目前的代碼是:使用iText在Java中使用獨立方法創建表格

public static void main(String args[]) { 
    try { 
     Document document = new Document(PageSize.A4); 
     PdfWriter.getInstance(document, new FileOutputStream(FILE)); 
     document.open(); 
     addMetaData(document); 
     addTitlePage(document); 
     addContent(document); 
     document.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private static void addTitlePage(Document document) throws DocumentException { 
    Paragraph preface = new Paragraph(); 
    // Add one empty line 
    addEmptyLine(preface, 1); 
    // Header of the document 
    preface.add(new Paragraph("Title here", capFont)); 

    addEmptyLine(preface, 1); 
    // Report generated by: _name, _date 
    preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 
     normFont)); 
    addEmptyLine(preface, 2); 
    preface.add(new Paragraph("This document describes some kind of price list which is unknown to me.", normFont)); 

    document.add(preface); 
} 

private static void addContent(Document document) throws DocumentException { 
    Paragraph content = new Paragraph(); 
    // Add one empty line 
    addEmptyLine(content, 1); 

    // Content of the document 
    content.add(new Paragraph(createTable(subPart), normFont)); // not working line 

    addEmptyLine(content, 5); 
    content.add(new Paragraph("This document is a preliminary version and not subject to the license agreement.", redFont)); 

    document.add(content); 
} 

private static void createTable(Section subPart) throws BadElementException { 
    PdfPTable table = new PdfPTable(3); 
    table.setHorizontalAlignment(Element.ALIGN_CENTER); 
    // Data 
    table.addCell("1"); 
    table.addCell("2"); 
    table.addCell("3"); 
    subPart.add(table); 
} 

任何幫助,將不勝感激。

回答

0

所以,一天文獻閱讀和API主頁後訪問我來到了我的解決方案:

相反的:content.add(new Paragraph(createTable(subPart), normFont));

我現在有:createTable(content);

,當然我改變的類型在段落中創建createTable方法的變量,以使其工作。

-1
package src.AutosysPolicyWriter.Utility; 

import java.util.StringTokenizer; 

import com.lowagie.text.Cell; 
import com.lowagie.text.Document; 
import com.lowagie.text.Element; 
import com.lowagie.text.Font; 
import com.lowagie.text.Phrase; 
import com.lowagie.text.Rectangle; 
import com.lowagie.text.Table; 


/** 
* Modification/itext 1.4 - <Modified : September 26, 2013> 
* @author Oliver Lundag 
* @date 2013-09-06 
* Can handle table creation in the PDF 
*/ 
public class PdfTableUtility { 

    /*** 
    * 
    * @param titleFont - font of the title of the table 
    * @param fontHeader - font of the headers 
    * @param fontData - font of the data 
    * @param thisReport - Document 
    * @param tableTitle - Name of the table 
    * @param headerStrings - headers 
    * @param data - data 
    * 
    * How to use this function; 
    * 
    * Example: 
    * 
    *  // Initialize PdfTableUtility object 
    *  1. PdfTableUtility tableUtility = new PdfTableUtility(); 
    * 
    *  // Declare the value for headers. 
    *  // Note: Number of columns will depends on how many headers has been declared 
    *  2. String[] headers = {"Customer Name","Age","Plan","Amount"}; 
    * 
    *  // Declare the data that will be put inside 
    *  // Note that the arrangement of the strings are the actual display in the pdf 
    *  // ; - separator 
    *  3. String data = "1.Oliver Lundag;26;Plan A;250,000;"+ 
    *       "2.Oliver Lundag;26;Plan A;250,000;"+ 
    *       "3.Oliver Lundag;26;Plan A;250,000"; 
    * 
    *  //call the function with specified arguments 
    *  //arguments will depend on developers perspective 
    *  4. tableUtility.displaytable(FontBold11, FontBold9, FontNormal9, thisReport, "Information", 4, headers, data); 
    */ 
    public void displaytable(Font titleFont, Font fontHeader, Font fontData, Document thisReport,String tableTitle, String[] headerStrings, String data) { 
     try{ 

      //START-(Modification/itext 1.4) SR-CS-13035 - OLUND <Modified : September 26, 2013> - title 
      //1.create table 
      Table title = new Table(1); 
      title.setLastHeaderRow(1); 
      title.setOffset(12f); 
      title.setSpaceInsideCell(1f); 
      title.setBorder(Rectangle.NO_BORDER); 

      //2.create table 
      Cell celltitle = new Cell(new Phrase(tableTitle, titleFont)); 
      celltitle.setLeading(12); 
      celltitle.setBorder(Rectangle.NO_BORDER); 
      celltitle.setHorizontalAlignment(Element.ALIGN_CENTER); 
      title.addCell(celltitle); 

      //3.add the title in document 
      thisReport.add(title); 
      //END -(Modification/itext 1.4) SR-CS-13035 - OLUND <Modified : September 26, 2013> - title 


      //START-(Modification/itext 1.4) SR-CS-13035 - OLUND <Modified : September 26, 2013> - data 
      //1. get the max number of columns 
      int numColumns = headerStrings.length; 

      //2. create a table 
      Table table = new Table(numColumns); 
      table.setOffset(12f); 
      table.setLastHeaderRow(1); 
      table.setSpaceInsideCell(1f); 
      table.setTableFitsPage(true); 
      table.setAutoFillEmptyCells(true); 

      //3.get headers and add it into cells 
      for (String header : headerStrings) { 
       Cell headerCell = new Cell(new Phrase(header, fontHeader)); 
       headerCell.setLeading(12); 
       headerCell.setHorizontalAlignment(Element.ALIGN_CENTER); 
       table.addCell(headerCell); 
      } 

      //4.get data and add it into cells 
      String strToken; 
      if (data.length() > 0) { 
       StringTokenizer stStr = new StringTokenizer(data,";",false); 
       while(stStr.hasMoreTokens()){ 
        strToken = stStr.nextToken().toString(); 
        Cell cell = new Cell(new Phrase(strToken,fontData)); 
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
        cell.setLeading(12); 
        table.addCell(cell); 
       } 
      } 

      //5. add the table in the document 
      thisReport.add(table); 
      //END - (Modification/itext 1.4) SR-CS-13035 - OLUND <Modified : September 26, 2013> - data 

     }catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

}