package com.herbert.test;

import com.lowagie.text.*;

import com.lowagie.text.Font;

import com.lowagie.text.pdf.BaseFont;

import com.lowagie.text.pdf.PdfWriter;

import java.awt.*;

import java.io.FileOutputStream;

import java.io.IOException;/**

* Created by Herbert on 2019/1/21.*/

public classPDFUtil {/**

* document对象*/

private static Document document = null;/**

* 创建一个书写器,布局文本位置

* @param leftSize 居左

* @param rightSize 居右

* @param onSize 居上

* @param underSize 居下

* @param path 存储位置

* @throws Exception 初始化PDF错误*/

publicPDFUtil(Integer leftSize , Integer rightSize , Integer onSize , Integer underSize, String path) throws Exception {try{//新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。

document = newDocument(PageSize.A4, leftSize, rightSize, onSize, underSize);//建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。

PdfWriter writer = PdfWriter.getInstance(document, newFileOutputStream(path));//打开文件

document.open();

}catch(Exception e){

e.printStackTrace();

System.out.println("PDF初始化错误");

}

}/**

* 书写每一个段落选择的字体

*

* @param fontType

* 0 //楷体字

* 1 //仿宋体

* 2 //黑体

* 字体需要可在追加

* @return

* @throws IOException

* @throws DocumentException*/

publicBaseFont addFontType(Integer fontType) {

BaseFont baseFont= null;try{switch(fontType){case 0://楷体字

baseFont = BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);break;case 1://仿宋体

baseFont = BaseFont.createFont("c://windows//fonts//SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);break;case 2://黑体

baseFont = BaseFont.createFont("c://windows//fonts//SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);break;

}returnbaseFont;

}catch(Exception e){

System.out.println("选择字体异常");

e.printStackTrace();

}returnbaseFont;

}/**

* 添加段落 - 段落位置( 0 居左 1 居中 2 居右)

* @param fontType 选择字体

* 0 //楷体字

* 1 //仿宋体

* 2 //黑体

* @param fontSize 字体大小

* @param color 字体颜色

* @param alignment 0 居左 1 居中 2 居右

* @param text 文本内容*/

public voidaddParagraph(Integer fontType , Integer fontSize,Color color ,Integer alignment ,String text){try{

BaseFont chinese=addFontType(fontType);

Font font= newFont(chinese, fontSize, Font.COURIER,color);

Paragraph paragraph=newParagraph(text,font);//居中显示

paragraph.setAlignment(alignment);

document.add(paragraph);

}catch(Exception e){

e.printStackTrace();

System.out.println("添加段落异常");

}

}/**

* 添加段落 - 首行缩进

* @param fontType 选择字体

* 0 //楷体字

* 1 //仿宋体

* 2 //黑体

* @param fontSize 字体大小

* @param color 字体颜色

* @param index 首行缩进

* @param text 文本内容*/

public voidaddTextIndent(Integer fontType , Integer fontSize,Color color ,Integer index ,String text){try{

BaseFont chinese=addFontType(fontType);

Font font= newFont(chinese, fontSize, Font.COURIER,color);

Paragraph paragraph=newParagraph(text,font);//设置首行缩进

paragraph.setFirstLineIndent(index);

document.add(paragraph);

}catch(Exception e){

e.printStackTrace();

System.out.println("添加段落异常");

}

}/**

* 添加新的一页*/

public voidaddPage(){try{

document.newPage();

}catch(Exception e){

e.printStackTrace();

System.out.println("添加段落异常");

}

}/**

* 换行

* 传入1是一行,以此递增

* @param lineNum 换的行数*/

public voidnewLine(Integer lineNum) {try{for(int i =0 ; i

document.add(new Paragraph("\n"));

}

}catch(Exception e){

e.printStackTrace();

System.out.println("换行错误");

}

}/**

* 关闭文档*/

public voidclose (){//关闭文档

document.close();

}public static voidmain(String args[]) throws Exception {

PDFUtil pdfUtil= new PDFUtil(80, 80, 120, 120,"E:\\herbert\\test.pdf");

pdfUtil.addParagraph(1,24,new Color(179, 91, 68),0,"我是0个测试案例--居左");

pdfUtil.newLine(10);

pdfUtil.addParagraph(1,24,new Color(75, 108, 179),1,"我是1个测试案例--居中 空行");

pdfUtil.addParagraph(1,24,new Color(140, 109, 179),2,"我是2个测试案例--居右");

pdfUtil.addPage();

pdfUtil.addParagraph(1,24,new Color(73, 179, 130),1,"我是3个测试案例--新增页");

pdfUtil.addParagraph(1,24,new Color(179, 91, 68),0,"我是0个测试案例--居左");

pdfUtil.addTextIndent(1,24,new Color(41, 124, 179),28,"我是3个测试案例--首行缩进");

pdfUtil.close();

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐