WEB工程生成PDF並匯出到本地
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Blob;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.powersi.Test;
import com.powersi.hygeia.framework.ParameterMapping;
import com.powersi.hygeia.framework.util.DBHelper;
import com.powersi.hygeia.framework.util.UtilFunc;
public class Test {
Document document = null;// 建立一個Document物件
private static Font headFont ;
private static Font keyFont ;
private static Font nbspFont;
private static Font textfont_H ;
private static Font textfont_B ;
private static Font textfont_13;
private static Font textfont_12;
private static Font textfont_11;
private static Font textfont_10;
private static Font textfont_9;
private static Font textfont_8;
private static Font textfont_7;
private static Font textfont_6;
int maxWidth = 520;
static{
BaseFont bfChinese_H;
try {
/**
* 新建一個字型,iText的方法 STSongStd-Light 是字型,在iTextAsian.jar 中以property為字尾
* UniGB-UCS2-H 是編碼,在iTextAsian.jar 中以cmap為字尾 H 代表文字版式是 橫版, 相應的 V 代表豎版
*/
bfChinese_H = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
headFont = new Font(bfChinese_H, 14, Font.NORMAL);
keyFont = new Font(bfChinese_H, 26, Font.BOLD);
nbspFont = new Font(bfChinese_H, 13, Font.NORMAL);
textfont_H = new Font(bfChinese_H, 10, Font.NORMAL);
textfont_B = new Font(bfChinese_H, 12, Font.NORMAL);
textfont_13 = new Font(bfChinese_H, 13, Font.NORMAL);
textfont_12 = new Font(bfChinese_H, 12, Font.NORMAL);
textfont_11 = new Font(bfChinese_H, 11, Font.NORMAL);
textfont_10 = new Font(bfChinese_H, 10, Font.NORMAL);
textfont_9 = new Font(bfChinese_H, 9, Font.NORMAL);
textfont_8 = new Font(bfChinese_H, 8, Font.NORMAL);
textfont_7 = new Font(bfChinese_H, 7, Font.NORMAL);
textfont_6 = new Font(bfChinese_H, 6, Font.NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 設定頁面屬性
* @param file
*/
public Test(File file) {
//自定義紙張
Rectangle rectPageSize = new Rectangle(350, 620);
// 定義A4頁面大小
//Rectangle rectPageSize = new Rectangle(PageSize.A4);
rectPageSize = rectPageSize.rotate();// 加上這句可以實現頁面的橫置
document = new Document(rectPageSize,10, 150, 10, 40);
try {
PdfWriter.getInstance(document,new FileOutputStream(file));
document.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 建表格(以列的數量建)
* @param colNumber
* @return
*/
public PdfPTable createTable(int colNumber){
PdfPTable table = new PdfPTable(colNumber);
try{
//table.setTotalWidth(maxWidth);
//table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
table.setSpacingBefore(10);
table.setWidthPercentage(100);
}catch(Exception e){
e.printStackTrace();
}
return table;
}
/**
* 建表格(以列的寬度比建)
* @param widths
* @return
*/
public PdfPTable createTable(float[] widths){
PdfPTable table = new PdfPTable(widths);
try{
//table.setTotalWidth(maxWidth);
//table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
table.setSpacingBefore(10);
table.setWidthPercentage(100);
}catch(Exception e){
e.printStackTrace();
}
return table;
}
/**
* 表格中單元格
* @param value
* @param font
* @param align
* @return
*/
public PdfPCell createCell(String value,Font font,int align){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value,font));
return cell;
}
/**
* 表格中單元格
* @param value
* @param font
* @param align
* @param colspan
* @param rowspan
* @return
*/
public PdfPCell createCell(String value,Font font,int align_v,int align_h,int colspan,int rowspan){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(align_v);
cell.setHorizontalAlignment(align_h);
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setPhrase(new Phrase(value,font));
return cell;
}
/**
* 建立無邊框表格
* @param value
* @param font
* @param align
* @param colspan
* @param rowspan
* @return
*/
public PdfPCell createCellNotBorder(String value,Font font,int align_v,int align_h,int colspan,int rowspan){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(align_v);
cell.setHorizontalAlignment(align_h);
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setPhrase(new Phrase(value,font));
cell.setBorderWidth(0f);
return cell;
}
/**
* 建短語
* @param value
* @param font
* @return
*/
public Phrase createPhrase(String value,Font font){
Phrase phrase = new Phrase();
phrase.add(value);
phrase.setFont(font);
return phrase;
}
/**
* 建段落
* @param value
* @param font
* @param align
* @return
*/
public Paragraph createParagraph(String value,Font font,int align){
Paragraph paragraph = new Paragraph();
paragraph.add(new Phrase(value,font));
paragraph.setAlignment(align);
return paragraph;
}
public void generatePDF(String tjid) throws Exception{
document.add(createParagraph("機構名稱",headFont,Element.ALIGN_CENTER));
document.add(createParagraph(" ",nbspFont,Element.ALIGN_CENTER));
document.add(createParagraph(" ",nbspFont,Element.ALIGN_CENTER));
document.add(createParagraph("文件名稱",keyFont,Element.ALIGN_CENTER));
document.add(createParagraph(" ",nbspFont,Element.ALIGN_CENTER));
document.add(createParagraph(" ",nbspFont,Element.ALIGN_CENTER));
//表格資訊
float[] widths1 = {20f,15f,10f,10f,20f,8f,9f,8f}; //整個表格的列寬,列數。
PdfPTable table1 = createTable(widths1);
// 內容 字型垂直對齊 橫向對齊 佔列 佔行
table1.addCell(createCell("體檢號碼:", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("姓名", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("婚姻", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("性別", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("年齡", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("證件號碼", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,3,1));
table1.addCell(createCell("聯絡電話", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,3,1));
table1.addCell(createCell("體檢型別", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,3,1));
table1.addCell(createCell("體檢日期", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,3,1));
table1.addCell(createCell("單位名稱", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,7,1));
document.add(table1);
document.close();
}
public static void main(String[] args) throws Exception {
File file = new File("D:\\test.pdf");
file.createNewFile();
String tjid="";
new Test(file).generatePDF(tjid);
}
}