XilongPei / Openparts

MIT License
3 stars 1 forks source link

Getting started with iText 7 Java #68

Open XilongPei opened 6 years ago

XilongPei commented 6 years ago

https://developers.itextpdf.com/itext7/download-and-install-information/Java

iText 7 Community - our open-source software library under AGPL license.

The iText AGPL license is for developers who wish to share their entire application source code with the open-source community as free software under the AGPL “copyleft” terms. You can read more on the AGPL license's terms and conditions here. The software is available on Maven and NuGet. The source code is available on GitHub. There is also an open source version available of pdfInvoice, pdfHTML, pdfDebug and pdfSweep. iText 7 Core - our closed-source software library.

Download instructions are identical for both, and fully explained below.

XilongPei commented 6 years ago

iText:一个易于使用的PDF函数库 http://hao.jobbole.com/itext/

https://mvnrepository.com/artifact/com.itextpdf

XilongPei commented 6 years ago

iText是一个非常著名的能够快速产生PDF文件的Java类库。支持文本,表格,图形的操作,可以方便的跟 Servlet 进行结合。在企业的信息系统中,报表处理一直占比较重要的作用,iText组件通过在服务器端使用Jsp 或JavaBean生成PDF报表,客户端采用超级连接显示或下载得到生成的报表,这样就很好的解决了B/S系统的报表处理问题。 iText是著名的开放源码的站点sourceforge的一个项目,它是一个用于生成PDF文档的一个java开源库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。如果PDF是标记的且包含一个结构树,就可以借助于iText将PDF文档转换成XML文档(这往往取决于PDF文档是如何创建的)。另外还可以从页面中提取纯文本。iText还可以用来标识现有的PDF文档,以及对它们进行加密等。 iText7是在AGPL协议下的(就是凡是用到他的代码的项目需要开源,除非购买上商业版,不过我们在国内就不要管这么多啦),itext5、7字体设置默认不支持中文,需要下载远东字体包iTextAsian.jar,否则不能 往PDF文档中输出中文字体以及读取中文文档会出错。 http://blog.csdn.net/ljheee/article/details/52761768 下面介绍每一个jar文件:

· kernel和 io: 包含低层次常用的基础的函数

· layout:包含高层次的函数

· forms:有关AcorForms操作需要的函数库

· pdfa:有关PDF/A(电子文档标准)的相关操作

· pdftest: test例子中所引用的库

    除了这些常用的架包意外,还有一些其他可能的包:

· barcodes:当你想要创建bar code(条代码?)时使用

· hyph:当你想要文字有连字符时使用

· font-asian:当你想要用CJK字符时(Chinese / Japanese / Korean)

· sign:当你想要使用电子签名是使用

XilongPei commented 6 years ago

iText 7.1 - Migration guide for Java https://maven.com.itextpdf.com/blog/itext-71-migration-guide-java https://developers.itextpdf.com/examples/itext-action-second-edition/chapter-1

XilongPei commented 6 years ago

http://quicker.iteye.com/blog/548805 https://stackoverflow.com/questions/44235815/bytearrayoutputstream-size-is-zero-in-pdfwriter 使用内存来加工pdf,需要评估内存使用情况。 `

protected void doPost(HttpServletRequest request,  
        HttpServletResponse response) throws ServletException, IOException {  
    Document doc = new Document();  
    ByteArrayOutputStream ba = new ByteArrayOutputStream();  
    try {  
        PdfWriter writer = PdfWriter.getInstance(doc, ba);  
        doc.open();  
        doc.add(new Paragraph("Hello World"));  

    } catch (DocumentException e) {  
        e.printStackTrace();  
    }  
    doc.close();  

    response.setContentType("application/pdf");  
    response.setContentLength(ba.size());  
    ServletOutputStream out = response.getOutputStream();  
    ba.writeTo(out);  
    out.flush();  
}

 Document document = new Document();
  try
  {
    ByteArrayOutputStream dsf = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(document, dsf);
    document.open();
    document.add(new Paragraph("Some content here"));

    // Set attributes here
    document.addAuthor("Lokesh Gupta");
    document.addCreationDate();
    document.addCreator("HowToDoInJava.com");
    document.addTitle("Set Attribute Example");
    document.addSubject("An example to show how attributes can be added to pdf files.");
    if (frontPageMap != null && !frontPageMap.isEmpty()) {

        PdfPTable table = new PdfPTable(frontPageMap.size()); // creating
                                                                // columns.
        table.setWidthPercentage(100); // Width 100%
        table.setSpacingBefore(10f); // Space before table
        table.setSpacingAfter(10f); // Space after table

        for (Map.Entry<String, Object> entry : frontPageMap.entrySet()) {
            PdfPCell tempCell = new PdfPCell(new Paragraph(entry.getKey()));
            tempCell.setBorderColor(BaseColor.BLUE);
            tempCell.setPaddingLeft(10);
            tempCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            tempCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(tempCell);
        }
        document.add(table);

    }
    System.out.println("Size Of Byte Array is "+dsf.size());
    ByteArrayInputStream bInput = new ByteArrayInputStream(dsf.toByteArray());
    file = new DefaultStreamedContent(bInput, "pdf", fileName);
    document.close();
    writer.close();

`