⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createpdf.java

📁 jsp动态网站开发技术与实践 电子工业出版社 随书附赠源代码
💻 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 + -