//字體
BaseFont baseFont = null;
//路徑
File path = new File("G:\\123.pdf");
//document對象 設置紙張 四周邊距
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
//設置字體
baseFont = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font ht = new Font(baseFont);
baseFont = BaseFont.createFont("G:\\framdit.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font st = new Font(baseFont);
//創建書寫器
PdfWriter pdfw = PdfWriter.getInstance(document,new FileOutputStream(path));
//設置水印
//Watermark watermark =new Watermark(Image.getInstance("G:\\2.jpg"), 0f, 0f);
//document.add(watermark);
//--------------------------頁眉
Phrase phrase = new Phrase(new Chunk("頁眉"));
//phrase.add(new Chunk("頁眉"));
HeaderFooter header = new HeaderFooter(phrase, false);//false不顯示頁碼
//設置居左 0 居中1 居右2
header.setAlignment(0);
//設置是否有邊框
header.setBorder(Rectangle.BOTTOM);
//寫進文檔
document.setHeader(header);
//-----------------------頁腳
HeaderFooter footer = new HeaderFooter(new Phrase("--"), new Phrase("--"));
footer.setAlignment(1);
footer.setBorder(Rectangle.NO_BORDER);//沒有邊框
document.setFooter(footer);
//---------------------加密
pdfw.setEncryption(PdfWriter.STRENGTH128BITS, null, "123", PdfWriter.AllowPrinting);
//文檔屬性 與文檔是否打開無關
document.addTitle("標題");
document.addAuthor("作者");
document.addSubject("主題");
document.addKeywords("關鍵字");
document.addCreator("創建者");
//打開文檔
document.open();
Paragraph paragraph = new Paragraph("123pdf示例",ST);
paragraph.setAlignment(1);
document.add(paragraph);
document.newPage();
Table table = new Table(4);//設置4列
//設置佔頁面寬度比例
table.setWidth(100);
//設置每列的寬度
int [] width = {25, 25, 25, 25};
table.setWidths(width);
Cell cell = new Cell(new Paragraph());
//水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//垂直居中
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setColspan(4);
cell.addElement(new Phrase("示例",ht));
table.addCell(cell);
cell = new Cell(new Paragraph());
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.addElement(new Phrase("名稱",st));
table.addCell(cell);
cell = new Cell(new Paragraph());
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setColspan(3);
cell.addElement(new Phrase("名稱",ST));
table.addCell(cell);
cell = new Cell(new Paragraph());
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.addElement(new Phrase("1",ST));
table.addCell(cell);
cell = new Cell(new Paragraph());
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.addElement(new Phrase("2",ST));
table.addCell(cell);
cell = new Cell(new Paragraph());
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.addElement(new Phrase("3",ST));
table.addCell(cell);
cell = new Cell(new Paragraph());
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.addElement(new Phrase("4",ST));
table.addCell(cell);
document.add(table);
document.newPage();
String str = "在研發科普教具的過程後期,企業與學校教師與學生可以共同研發出新的產品品種。因為老產品是一個平台,在此平台上將開發出很多種,最終需要篩選出機中最具使用價值的新產品或科普教具。這就是課題的主要研發內容。企業研發人員是要把科普教具研發的思路通過科普教具的分析給學生們講解,學生的提問與建議將研發人員啓發,這就是研究開發的意外收穫。只要去做這項工作,研究開發就會有收穫。如果效果特別好,這種形式可以在今後大規模推廣成為常態";
Paragraph para = new Paragraph(str,ST);
document.add(para);
} catch (Exception e) {
e.printStackTrace();
}
document.close();
本文章為轉載內容,我們尊重原作者對文章享有的著作權。如有內容錯誤或侵權問題,歡迎原作者聯繫我們進行內容更正或刪除文章。