pdfbox drawTable

package org.simple2odfbox.demo;


import java.io.IOException;
import java.util.List;


import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.odftoolkit.simple.TextDocument;
import org.odftoolkit.simple.table.Column;
import org.odftoolkit.simple.table.Table;
import org.odftoolkit.simple.utils.ResourceUtilities;


/**
 * @author Daisy
 * @description
 * simple.table --> pdf.box
 * 从odt文件读取table, 转换成pdf的table
 */
public class Simple2odfbox {

private static final String mOdtTestFile = "TestTextTable.odt";
private static TextDocument mOdtDoc;

public static void odf2pdfAsTable() throws Exception {

mOdtDoc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream(mOdtTestFile));
Table table1 = mOdtDoc.getTableList().get(0);
System.out.println("table名:"+table1.getTableName());

//定义一个String[][], 保存cell中的值。
String[][] data = new String[table1.getColumnCount()-1][table1.getRowCount()-1];

List<Column> lCol = table1.getColumnList();
for(int i=0; i<lCol.size(); i++){
Object ob = lCol.get(i);
Column lRow = (Column)lCol.get(i);
for(int j=0; j<lRow.getCellCount()-1; j++){
if(lRow.getCellByIndex(j).getStringValue() == null){
data[i][j] = "";
}else{
System.out.println("Cell["+i+"]["+j+"]"+lRow.getCellByIndex(j).getStringValue());
//data[i][j] = lRow.getCellByIndex(j).getStringValue();
}

}
}

/**
* 以上simple解析完毕,以下代码:转换成PDF
*/
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage( page );
PDPageContentStream contentStream = new PDPageContentStream(doc, page);


String[][] content = {{"","", ""}, 
                     {"sdf","sdf", "sfd"}, 
                     {"","", ""}, 
                     {"","", ""}, 
                     {"","", ""}} ;


drawTable(page, contentStream, 700, 100, content);
contentStream.close();
doc.save("test.pdf" );

}



/**
* @param page
* @param contentStream
* @param y the y-coordinate of the first row
* @param margin the padding on left and right of table
* @param content a 2d array containing the table data
* @throws IOException
*/
public static void drawTable(PDPage page, PDPageContentStream contentStream, 
                           float y, float margin, 
                           String[][] content) throws IOException {
   final int rows = content.length;
   final int cols = content[0].length;
   final float rowHeight = 20f;
   final float tableWidth = page.findMediaBox().getWidth() - margin - margin;
   final float tableHeight = rowHeight * rows;
   final float colWidth = tableWidth/(float)cols;
   final float cellMargin=5f;


   //draw the rows
   float nexty = y ;
   for (int i = 0; i <= rows; i++) {
       contentStream.drawLine(margin, nexty, margin+tableWidth, nexty);
       nexty-= rowHeight;
   }


   //draw the columns
   float nextx = margin;
   for (int i = 0; i <= cols; i++) {
       contentStream.drawLine(nextx, y, nextx, y-tableHeight);
       nextx += colWidth;
   }


   //now add the text        
   contentStream.setFont( PDType1Font.HELVETICA_BOLD , 12 );        


   float textx = margin+cellMargin;
   float texty = y-15;        
   for(int i = 0; i < content.length; i++){
       for(int j = 0 ; j < content[i].length; j++){
           String text = content[i][j];
           contentStream.beginText();
           contentStream.moveTextPositionByAmount(textx,texty);
           contentStream.drawString(text);
           contentStream.endText();
           textx += colWidth;
       }
       texty-=rowHeight;
       textx = margin+cellMargin;
   }
}

/**
* @param args
*/
public static void main(String[] args) throws Exception {

odf2pdfAsTable();

}

}

你可能感兴趣的:(pdfbox drawTable)