`
fengpeng
  • 浏览: 100400 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

我和iText的第一次亲密接触

    博客分类:
  • Java
阅读更多
关键字: 企业应用   java    

要生成pdf文件,在网上查了下资料,首选iText,跟着大家走,我也iText一把。
1:把字型编程A4横向

代码
  1. Document document = new Document(PageSize.A4.rotate());   
<script>render_code();</script>
2:在PDF文件中加入表格
代码
  1.   float[] widths = {0.05f,0.29f, 0.05f, 0.05f, 0.14f, 0.03f, 0.04f,0.04f, 0.05f, 0.05f,0.08f,0.08f, 0.05f};   
  2. //new 一个13列的table   
  3.   PdfPTable table = new PdfPTable(13);   
  4. //设置table每一列的宽度,widths里写的是百分比,他们加和需要是1   
  5.   table.setWidths(widths);   
  6.   //设置表格在页面上的宽度,设成100表示可以表格填满页面,但是要去掉页面margin   
  7. table.setWidthPercentage(100);   
  8. //设置表格上端的空白距离,类似css中的margin-top:xxpx;这样在给表格加上标题后,标题就不会跟表格重叠在一起了。   
  9. table.setSpacingBefore(3f);   
<script>render_code();</script>
3:向表格里填数据, 例子
代码
  1. for(int i = 0; i<26; i++)   
  2. {   
  3.   table.table.addCell(i+"");   
  4. }   
<script>render_code();</script>
这样就会往表格里填上2行数据,这个api比较简单,不用向jxl/poi 那里那样还有明确写出到底要往那个cell填
4:标题和表格组合
代码
  1. document.add(new Paragraph(titleWorkhour, FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(000))));   
  2. //由于设置了table.setSpacingBefore(3f);所以table跟标题不会重合。   
  3. document.add(table);   
<script>render_code();</script>
5:分页
代码
  1. document.newPage();   
<script>render_code();</script>
6:合并PDF文件,由于往一个document里加内容只能是顺序往下加,而我的summary页要最后才能算出来,但是summary页又要放在第一页
所以我不得不先把body生成一个pdf文件,然后summary在生成一个文件,然后把两个文件合并成同一个文件。
代码
  1. private void concatenateSummary(String[] args, String finalFile)   
  2.     {   
  3.         try {   
  4.             int pageOffset = 0;   
  5.             ArrayList master = new ArrayList();   
  6.             int f = 0;   
  7.             String outFile = finalFile;   
  8.             Document document = null;   
  9.             PdfCopy  writer = null;   
  10.             while (f < args.length) {   
  11.                 // we create a reader for a certain document   
  12.                 PdfReader reader = new PdfReader(args[f]);   
  13.                 reader.consolidateNamedDestinations();   
  14.                 // we retrieve the total number of pages   
  15.                 int n = reader.getNumberOfPages();   
  16.                 List bookmarks = SimpleBookmark.getBookmark(reader);   
  17.                 if (bookmarks != null) {   
  18.                     if (pageOffset != 0)   
  19.                         SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);   
  20.                     master.addAll(bookmarks);   
  21.                 }   
  22.                 pageOffset += n;   
  23.                    
  24.                 if (f == 0) {   
  25.                     // step 1: creation of a document-object   
  26.                     document = new Document(reader.getPageSizeWithRotation(1));   
  27.                     // step 2: we create a writer that listens to the document   
  28.                     writer = new PdfCopy(document, new FileOutputStream(outFile));   
  29.                     // step 3: we open the document   
  30.                     document.open();   
  31.                 }   
  32.                 // step 4: we add content   
  33.                 PdfImportedPage page;   
  34.                 for (int i = 0; i < n; ) {   
  35.                     ++i;   
  36.                     page = writer.getImportedPage(reader, i);   
  37.                     writer.addPage(page);   
  38.                 }   
  39.                 PRAcroForm form = reader.getAcroForm();   
  40.                 if (form != null)   
  41.                     writer.copyAcroForm(reader);   
  42.                 f++;   
  43.             }   
  44.             if (!master.isEmpty())   
  45.                 writer.setOutlines(master);   
  46.             // step 5: we close the document   
  47.             document.close();   
  48.         }   
  49.         catch(Exception e) {   
  50.             e.printStackTrace();   
  51.         }   
  52.     }   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics