helloaccp.java

来自「主题:用iText输出PDF报表 内容组织: 1. 自我介绍 2. 简要介」· Java 代码 · 共 53 行

JAVA
53
字号
package com.aptech.demo.itext;

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class HelloACCP {
	public static void main(String[] args) throws DocumentException,
			IOException {
		Rectangle rec = new Rectangle(PageSize.A4);
		Document doc = new Document(rec);
		PdfWriter.getInstance(doc, new FileOutputStream("HelloACCP.pdf"));
		doc.open();
		
		BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
				BaseFont.NOT_EMBEDDED);
		Font font = new Font(bf, 12, Font.NORMAL);
		
		Image image = Image.getInstance("mouse.jpg");
		image.setAlignment(Image.ALIGN_CENTER);
		
		
		PdfPTable table = new PdfPTable(2);
		float[] widths = new float[]{60, 30};
		table.setWidthPercentage(50);
		table.setWidths(widths);
		table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
		PdfPCell name = new PdfPCell(new Paragraph("姓名", font));
		PdfPCell age = new PdfPCell(new Paragraph("年龄", font));
		PdfPCell nameValue = new PdfPCell(new Paragraph("XX", font));
		PdfPCell ageValue = new PdfPCell(new Paragraph("18", font));
		table.addCell(name);
		table.addCell(nameValue);
		table.addCell(age);
		table.addCell(ageValue);
		
		doc.add(new Paragraph("HelloACCP,你好!", font));
		doc.add(image);
		doc.add(table);
		doc.close();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?