?? createpdf.java
字號:
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.awt.Color;
public class CreatePDF
{
public static void main(String[] args)
{
//設置頁面大小
Rectangle pSize=new Rectangle(PageSize.A4);
//創建一個文檔對象,設置初始化大小
Document document=new Document(pSize);
try
{
PdfWriter.getInstance(document, new FileOutputStream ("8-1.pdf"));
//打開文檔
document.open();
//設置中文字體
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese1 = new Font(bfChinese, 24, Font.NORMAL);
Font FontChinese2 = new Font(bfChinese, 12, Font.NORMAL);
//添加一段文字內容
document.add(new Paragraph("創建PDF文檔\n\r", FontChinese1));
//創建新表格
PdfPTable table = new PdfPTable(3);
// 設置表格大小為可用空白區域的80%
table.setWidthPercentage(80);
//定義一個表格單元
PdfPCell cell = new PdfPCell(new Paragraph("HELLO"));
//定義一個表格單元的跨度
cell.setColspan(3);
//把單元加到表格中
table.addCell(cell);
//把下面這3項順次的加入到表格中,當一行充滿時候自動折行到下一行
table.addCell(new Paragraph("你好!", FontChinese2));
table.addCell(new Paragraph("你好!", FontChinese2));
table.addCell(new Paragraph("你好!", FontChinese2));
//重新定義單元格
cell = new PdfPCell(new Paragraph("你好!", FontChinese2));
//定義單元格的框顏色
cell.setBorderColor(new Color(255, 0, 0));
//把單元格加到表格上,默認為一個單元
table.addCell(cell);
//重新定義單元格
cell = new PdfPCell(new Paragraph("你好!", FontChinese2));
//定義單元格的跨度
cell.setColspan(2);
//定義單元格的背景顏色
cell.setBackgroundColor(new Color(0xC0,0xC0,0xC0));
//增加到表格上
table.addCell(cell);
//將表格內容添加到文檔中
document.add(table);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
//關閉文檔對象
document.close();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -