2013-05-14 33 views
5

Tôi cần tạo một tệp PDF sẽ giữ báo cáo trạng thái thực thi, nơi trạng thái sẽ ở trong cấu trúc bảng. Có thể tạo ra định dạng bảng pdf với api PDFBOX không?Tạo tài liệu PDF mới bằng cách sử dụng PDFBOX API

Dưới đây là một số mẫu mã để tạo một tài liệu PDF mới:

import java.awt.Color; 
import java.io.IOException; 
import org.apache.pdfbox.exceptions.COSVisitorException; 
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.PDFont; 
import org.apache.pdfbox.pdmodel.font.PDType1Font; 



public class HelloWorld 
{ 
    /** 
    * Constructor. 
    */ 
    public HelloWorld() 
    { 
     super(); 
    } 


    public static void main(String[] args) throws IOException, COSVisitorException 
    { 
     PDDocument doc = null; 
    try 
    { 
     doc = new PDDocument(); 

     PDPage page = new PDPage(); 
     doc.addPage(page); 
     PDFont font = PDType1Font.COURIER_BOLD_OBLIQUE; 
     Color color = Color.blue; 


     PDPageContentStream contentStream = new PDPageContentStream(doc, page); 
     contentStream.beginText(); 
     contentStream.setFont(font, 12); 
     contentStream.setNonStrokingColor(color); 
     contentStream.moveTextPositionByAmount(100, 700); 
     contentStream.drawString("message"); 

     contentStream.setNonStrokingColor(Color.GREEN); 
     contentStream.drawString("Hi"); 

     contentStream.endText(); 
     contentStream.close(); 
     doc.save("d:\\abc.pdf"); 
    } 
    finally 
    { 
     if(doc != null) 
     { 
      doc.close(); 
     } 
    } 
    } 
} 

Trả lời

16

Hãy thử điều này:

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.getCropBox().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; 
     } 
    } 

Và chỉ cần gọi phương pháp này trong main chức năng

public static void main(String[] args) throws IOException, COSVisitorException { 
    PDDocument doc = new PDDocument(); 
    PDPage page = new PDPage(); 
    doc.addPage(page); 
    PDPageContentStream contentStream = new PDPageContentStream(doc, page); 

    String[][] content = { 
     {"Name"," Time "}, 
     {"HTC","01:25"}, 
     {"Samsung Tab2","05:30"} 
    } ; 

    drawTable(page, contentStream, 700, 100, content); 

    contentStream.close(); 
    doc.save("h:\\test.pdf"); 
} 
1
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 

import org.apache.pdfbox.pdmodel.*; 
import org.apache.pdfbox.pdmodel.graphics.xobject.*; 
import org.apache.pdfbox.pdmodel.edit.*; 
import org.apache.pdfbox.pdmodel.font.*; 


public class PDFExample { 

    public static void main(String[] args){ 
    // Create a document and add a page to it 
    try { 
    PDDocument document = new PDDocument(); 
    PDPage page = new PDPage(); 
    document.addPage(page); 

    // Image to use 
    PDXObjectImage img = new PDJpeg(document, new FileInputStream(new File("C://213480-EligibilityFormHeader.jpg"))); 

    // Create a new font object selecting one of the PDF base fonts 
    PDFont font = PDType1Font.HELVETICA_BOLD; 

    // Start a new content stream which will "hold" the to be created content 
    PDPageContentStream contentStream = new PDPageContentStream(document, page,true,true); 

    contentStream.setFont(font, 6); 
    contentStream.drawImage(img, 25, 700); 

    drawTable(page, contentStream); 

    // Make sure that the content stream is closed 
    contentStream.close(); 

    // Save the results and ensure that the document is properly closed 
    document.save("C://HelloWorld.pdf"); 
    document.close(); 
    } catch (Exception e) { 
     System.out.println("Exception is: "); 
    } 
    } 

    private static void drawTable(PDPage page, PDPageContentStream contentStream) { 
     try { 
      float y = 650; 
      float margin = 130; 

      String[] content = {"One","Two", "Date", "Score", "Score2", "Score3", "Score4", "Score5", "Score6"}; 

      final int rows = content.length; 
      final int cols = 2; 
      final float rowHeight = 22f; 
      final float tableWidth = 900.0f; 
      final float tableHeight = rowHeight * rows; 
      final float cellMargin=1f; 

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

      float colWidthX [] = {200,70,0}; 

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

      //now add the text 
      float textx = margin+cellMargin; 
      float texty = y-15; 
      //textx = margin+cellMargin; 


       for(int j = 0 ; j < rows; j++) { 
        contentStream.beginText(); 
        contentStream.moveTextPositionByAmount(textx,texty); 

        contentStream.drawString(content[j]); 
        contentStream.endText(); 
        textx += colWidthX[0]+9; 
        contentStream.beginText(); 
        contentStream.moveTextPositionByAmount(textx,texty); 
        if(j==0) 
         contentStream.drawString("1"); 
        if(j==1) 
         contentStream.drawString("12345"); 
        if(j==2) 
         contentStream.drawString("05-December-2003"); 
        if(j==3) 
         contentStream.drawString("15"); 
        if(j==4) 
         contentStream.drawString("1"); 
        if(j==5) 
         contentStream.drawString("1"); 
        if(j==6) 
         contentStream.drawString("1"); 
        if(j==7) 
         contentStream.drawString("1"); 
        if(j==8) 
         contentStream.drawString("1"); 

        contentStream.endText(); 
        textx = margin+cellMargin; //colWidth; 
        texty -= rowHeight; //row height 

       } 
       texty-=rowHeight; 
       textx = margin+cellMargin; 
     } 
     catch (IOException ioe) 
     { 
      //Package.log.error(" drawTable :" + ioe); 
      final String errormsg = "Could not drawTable "; 
      //Package.log.error("In RuleThread drawTable " + errormsg, ioe); 
      throw new RuntimeException(errormsg, ioe); 
     } 
     catch (Exception ex) 
     { 
      //Package.log.error(" drawTable :" + ex); 
      final String errormsg = "Could not drawTable "; 
      //Package.log.error("In RuleThread drawTable " + errormsg, ex); 
      throw new RuntimeException(errormsg, ex); 
     } 
    } 


} 
1

Tôi chỉ quảng cáo ded một wrapper đơn giản cho pdfBOX og github. Nó có một bảng hỗ trợ đơn giản mà bạn có thể sử dụng như một đường cơ sở cut'n paste. Hoặc sử dụng lớp như là nếu bạn muốn. https://github.com/heksemann/HexPDF

+0

Xem nhanh lớp học của bạn 'cursorY = (cursorY mkl