java生成pdf(完整版)
package websm.cn;
import com.itextpdf.text.*;
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 java.io.*;
import java.util.Map;
import java.util.Set;
public class creatPdf {
private static Font titleFont;
private static Font textFont;
private static int maxWidth = 480;
static {
try {
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
titleFont = new Font(bfChinese, 16, Font.NORMAL);
textFont = new Font(bfChinese, 16, Font.NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
}
public void generatePdf() {
File file = new File(File.separator);
try {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
file.createNewFile();
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
document.addTitle("标题");
document.addAuthor("作者");
document.addSubject("主题");
document.addKeywords("关键字");
document.addCreator("创建者");
createPdfFile(document);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void createPdfFile(Document document) throws DocumentException, IOException {
Paragraph paragraph = titleText("标题");
PdfPTable table = CreateTable(new float[]{100, 140, 100, 140});
setTableCellInfo(table);
InputStream is = this.getClass().getClassLoader().getResourceAsStream("");
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[8192];
int read;
while ((read = is.read(buf, 0, buf.length)) >= 0) {
out.write(buf, 0, read);
}
Image image = Image.getInstance(out.toByteArray());
image.setAbsolutePosition(400, 260);
image.scaleAbsolute(100, 100);
Paragraph p = createParagraphNoInde();
Phrase ph = new Paragraph("说明", textFont);
Phrase ph1 = new Paragraph("重要说明............................", textFont);
p.add(ph);
p.add(ph1);
document.add(paragraph);
document.add(table);
document.add(image);
document.add(p);
}
private void setTableCellInfo(PdfPTable table) {
table.addCell(createCell("三千业务", textFont, Element.ALIGN_CENTER, 4));
table.addCell(createCell("姓名", textFont));
table.addCell(createCell("性别", textFont));
table.addCell(createCell("年龄", textFont));
}
private Paragraph titleText(String text) {
Paragraph paragraph = new Paragraph(text, titleFont);
paragraph.setAlignment(1);
paragraph.setIndentationLeft(12);
paragraph.setIndentationRight(12);
paragraph.setFirstLineIndent(24);
paragraph.setLeading(20F);
paragraph.setSpacingBefore(5F);
paragraph.setSpacingAfter(10F);
return paragraph;
}
public static PdfPTable CreateTable(float[] widths) {
PdfPTable pdfPTable = new PdfPTable(widths);
try {
pdfPTable.setTotalWidth(maxWidth);
pdfPTable.setLockedWidth(true);
pdfPTable.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfPTable.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return pdfPTable;
}
public static PdfPCell createCell(String value, Font font, int align, int colspan) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setMinimumHeight(25);
cell.setPhrase(new Phrase(value, font));
return cell;
}
public static PdfPCell createCell(Paragraph value, int align, int colspan) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setMinimumHeight(25);
cell.setPhrase(value);
return cell;
}
public static PdfPCell createCell(String value, Font font) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setMinimumHeight(25);
cell.setPhrase(new Phrase(value, font));
return cell;
}
public static Paragraph createParagraphNoInde() {
Paragraph paragraph = new Paragraph();
paragraph.setAlignment(0);
paragraph.setIndentationLeft(12);
paragraph.setIndentationRight(12);
paragraph.setFirstLineIndent(0);
paragraph.setLeading(20F);
return paragraph;
}
}