Java实现PDF的生成

来源:互联网 发布:已翻拍的网络耽美小说 编辑:程序博客网 时间:2024/05/22 00:15

  • 基本步骤

1、新建document对象

2、建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中

3、打开文档

4、向文档中添加内容

5、关闭文档

http://blog.csdn.net/yi2419808933/article/details/52469241

http://blog.csdn.net/justinytsoft/article/details/53320225?locationNum=4&fps=1

http://www.cnblogs.com/dengjiali/articles/2521301.html

http://blog.csdn.net/jixiangrurui/article/details/41042925

  • java生成pdf方案总结

1. Jasper Report生成pdf:设计思路是先生成模板,然后得到数据,最后将两者整合得到结果。但是Jasper Report的问题在于,其生成模板的方式过于复杂,即使有IDE的帮助,我们还是需要对其中的众多规则有所了解才行,否则就会给调试带来极大的麻烦。

2. openoffice生成pdf:openoffice是开源软件且能在windows和linux平台下运行。

3. itext + flying saucer生成pdf:itext和flying saucer都是免费开源的,且与平台无关,结合css和velocity技术,可以很好的实现。

http://blog.csdn.net/lewee0215/article/details/44238889?locationNum=16

  • PDF操作中的一些类

https://my.oschina.net/smellok/blog/73727

  • Java程序实现Word文档转为pdf以及出现的问题解决

http://blog.csdn.net/u013238430/article/details/52943075

http://blog.csdn.net/ilovejavas/article/details/17501899

  • 添加水印文字

http://blog.sina.com.cn/s/blog_d5af62a70102vhrv.html

http://www.cnblogs.com/tankqiu/p/4412898.html

/**
     * 加水印(字符串)
     * @param inputFile 需要加水印的PDF路径
     * @param outputFile 输出生成PDF的路径
     * @param waterMarkName 水印字符
     */
    public static void stringWaterMark(String inputFile, String waterMarkName) {

    try {

    String[] spe = PDFStyle.separatePath(inputFile);
String outputFile = spe[0] + "_WM." + spe[1];
   
PdfReader reader = new PdfReader(inputFile);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));

int total = reader.getNumberOfPages() + 1;

PdfContentByte under;

//给每一页加水印
for (int i = 1; i < total; i++) {

Rectangle rectangle = stamper.getReader().getPageSizeWithRotation(i);
float x = rectangle.getWidth()/2;
float y = rectangle.getHeight()/2-5;

under = stamper.getUnderContent(i);
under.beginText();

under.setFontAndSize(PDFStyle.getBaseFont(), 35);
under.setTextMatrix(200, 120);

under.setColorFill(new Color(238, 209, 212));
under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, x, y, 50);
// 添加水印文字
under.endText();
under.stroke();
}
stamper.close();

} catch (Exception e) {
e.printStackTrace();
}

}

/**
* 分割路径
* @param path
* @return 返回分割后的路径
*/
public static String[] separatePath(String path){

if(StringUtils.isBlank(path)){
return null;
}

String[] sep = path.split("\\.");
return new String[]{sep[0],sep[1]};
}

  • 合并PDF

http://blog.csdn.net/qq_32566703/article/details/70112058

http://blog.csdn.net/MissEel/article/details/75220571

//合并PDF
public static void pdfMerge(Document document , PdfWriter pdfWriter){

try {

PdfReader pdfReader;
//附加信息PDF
pdfReader = new PdfReader("E:\\附加信息.pdf");
PdfContentByte pdfContentByte = pdfWriter.getDirectContent();
int totalPages = pdfReader.getNumberOfPages();
int pageOfCurrentReaderPDF = 0;

while (pageOfCurrentReaderPDF < totalPages) {

//创建新的一页
document.newPage();
pageOfCurrentReaderPDF++;
PdfImportedPage page = pdfWriter.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
pdfContentByte.addTemplate(page, 0, 0);
}
} catch (IOException e) {

e.printStackTrace();
}
}
原创粉丝点击