提供自己的一些工具类 生成PDF文件所需的jar包
1、创建PDF —— hello word
public class CreatePdfText {
public static void main(String
[] args
) {
System
.out
.println("---start----");
try {
fillTemplate("F:\\test\\fillTemplate.pdf");
} catch (Exception e
) {
e
.printStackTrace();
}
System
.out
.println("---end----");
}
public static void fillTemplate(String outpath
) {
try {
Document document
= new Document();
PdfWriter
.getInstance(document
, new FileOutputStream(outpath
));
document
.open();
document
.add(new Paragraph("Hello World"));
document
.close();
} catch (Exception e
) {
e
.printStackTrace();
}
}
}
2、进阶
创建一个PDF字体样式工具类
package test
;
import java
.io
.IOException
;
import com
.itextpdf
.text
.DocumentException
;
import com
.itextpdf
.text
.Element
;
import com
.itextpdf
.text
.Font
;
import com
.itextpdf
.text
.Paragraph
;
import com
.itextpdf
.text
.pdf
.BaseFont
;
public class PdfFontUtils {
private static BaseFont baseFont
= null
;
static{
try {
baseFont
= BaseFont
.createFont("C:/Windows/fonts/simsun.ttc,1", BaseFont
.IDENTITY_H
, BaseFont
.NOT_EMBEDDED
);
} catch (DocumentException e
) {
e
.printStackTrace();
} catch (IOException e
) {
e
.printStackTrace();
}
}
public static Paragraph
getFont(int type
, String text
){
Font font
= new Font(baseFont
);
if(1 == type
){
font
.setSize(16f);
font
.setStyle(Font
.BOLD
);
} else if(2 == type
){
font
.setSize(16f);
font
.setStyle(Font
.BOLD
);
} else if(3 == type
){
font
.setSize(14f);
font
.setStyle(Font
.BOLD
);
} else if(4 == type
){
font
.setSize(14f);
} else if(5 == type
){
font
.setSize(10.5f);
} else if(6 == type
){
font
.setSize(10.5f);
} else {
font
.setSize(10.5f);
}
Paragraph paragraph
= new Paragraph(text
, font
);
if(1 == type
){
paragraph
.setAlignment(Paragraph
.ALIGN_CENTER
);
paragraph
.setSpacingBefore(10f);
paragraph
.setSpacingAfter(10f);
} else if(2 == type
){
paragraph
.setAlignment(Element
.ALIGN_JUSTIFIED
);
paragraph
.setSpacingBefore(2f);
paragraph
.setSpacingAfter(2f);
} else if(3 == type
){
paragraph
.setSpacingBefore(2f);
paragraph
.setSpacingAfter(1f);
} else if(4 == type
){
paragraph
.setSpacingBefore(2f);
paragraph
.setSpacingAfter(2f);
} else if(5 == type
){
paragraph
.setAlignment(Element
.ALIGN_JUSTIFIED
);
paragraph
.setFirstLineIndent(24);
paragraph
.setSpacingBefore(1f);
paragraph
.setSpacingAfter(1f);
} else if(6 == type
){
paragraph
.setAlignment(Element
.ALIGN_LEFT
);
paragraph
.setSpacingBefore(1f);
paragraph
.setSpacingAfter(1f);
}
return paragraph
;
}
}
创建pdf文件
import java
.io
.File
;
import java
.io
.FileOutputStream
;
import java
.io
.IOException
;
import com
.itextpdf
.text
.Document
;
import com
.itextpdf
.text
.DocumentException
;
import com
.itextpdf
.text
.PageSize
;
import com
.itextpdf
.text
.Paragraph
;
import com
.itextpdf
.text
.Phrase
;
import com
.itextpdf
.text
.Rectangle
;
import com
.itextpdf
.text
.pdf
.PdfPCell
;
import com
.itextpdf
.text
.pdf
.PdfPTable
;
import com
.itextpdf
.text
.pdf
.PdfWriter
;
public class CreatePdfText {
public static void main(String
[] args
) {
System
.out
.println("===========start=============");
try {
Document doc
= createPdf("F:\\test\\test.pdf");
createFile(doc
);
doc
.close();
} catch (Exception e
) {
e
.printStackTrace();
}
System
.out
.println("===========end=============");
}
public static Document
createPdf(String outpath
) throws DocumentException
, IOException
{
Rectangle rect
= new Rectangle(PageSize
.A4
);
File saveDir
= new File(outpath
);
File dir
= saveDir
.getParentFile();
if (!dir
.exists()) {
dir
.mkdirs();
}
Document doc
= new Document(rect
);
PdfWriter writer
= PdfWriter
.getInstance(doc
, new FileOutputStream(outpath
));
writer
.setPdfVersion(PdfWriter
.PDF_VERSION_1_2
);
doc
.addTitle("Title@wpixel");
doc
.addAuthor("Author@wpixel");
doc
.addSubject("Subject@wpixel");
doc
.addKeywords("Keywords@wpixel");
doc
.addCreator("Creator@wpixel");
doc
.setMargins(40, 40, 40, 40);
doc
.open();
return doc
;
}
public static void createFile(Document doc
) throws DocumentException
{
doc
.add(PdfFontUtils
.getFont(1, "合作协议"));
doc
.add(PdfFontUtils
.getFont(6, "甲方:"));
doc
.add(PdfFontUtils
.getFont(6, "乙方:"));
doc
.add(PdfFontUtils
.getFont(6, "时间:"));
doc
.add(PdfFontUtils
.getFont(6, "地点:"));
Paragraph text05
= PdfFontUtils
.getFont(5, "《根据中华人民共和国合同法》的有关规定,经甲、乙双方友好协商,本着长期平等合作.....吧啦吧啦吧啦吧啦吧啦吧啦吧啦吧啦");
doc
.add(text05
);
doc
.add(PdfFontUtils
.getFont(2, "一、合作方式及条件"));
doc
.add(PdfFontUtils
.getFont(5, "1.双方根据国家法律规定建立合作关系,双方严格遵守和执行国家各项方针政策和有关法律、法规和条例规定。 "));
doc
.add(PdfFontUtils
.getFont(5, "2.双方严格按照《中华人民共和国招标投标法》及相关规定实施合作。 "));
doc
.add(PdfFontUtils
.getFont(5, "3.双方本着密切配合、分工协作、保证质量、按期完成的原则,共同做好工作。 "));
doc
.add(PdfFontUtils
.getFont(2, "二、权利义务"));
doc
.add(PdfFontUtils
.getFont(5, "1.双方根据国家法律规定建立合作关系,双方严格遵守和执行国家各项方针政策和有关法律、法规和条例规定。 "));
doc
.add(PdfFontUtils
.getFont(5, "2.双方严格按照《中华人民共和国招标投标法》及相关规定实施合作。 "));
doc
.add(PdfFontUtils
.getFont(5, "3.双方本着密切配合、分工协作、保证质量、按期完成的原则,共同做好工作。 "));
doc
.add(PdfFontUtils
.getFont(2, "三、其他"));
doc
.add(PdfFontUtils
.getFont(5, "1.双方根据国家法律规定建立合作关系,双方严格遵守和执行国家各项方针政策和有关法律、法规和条例规定。 "));
doc
.add(PdfFontUtils
.getFont(5, "2.双方严格按照《中华人民共和国招标投标法》及相关规定实施合作。 "));
doc
.add(PdfFontUtils
.getFont(5, "3.自定义 "));
PdfPTable table
= new PdfPTable(2);
table
.getDefaultCell().setBorder(PdfPCell
.NO_BORDER
);
PdfPCell cell
;
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "甲方:(盖章)")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "乙方:(盖章)")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "法定代表人或负责人签章")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "法定代表人或负责人签章")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "地址:")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "地址:")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "开户银行:")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "开户银行:")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "邮编:")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "邮编:")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "授权代理人:")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "项目经理:")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "电话:")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
cell
.setBorder(Rectangle
.NO_BORDER
);
cell
= new PdfPCell(new Phrase(PdfFontUtils
.getFont(5, "电话:")));
cell
.setColspan(1);
cell
.setBorder(0);
table
.addCell(cell
);
doc
.add(table
);
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-51406.html