DANCE WITH JAVA

          開發出高質量的系統

          常用鏈接

          統計

          積分與排名

          好友之家

          最新評論

          使用IText打印PDF

          IText是開源的,對與打印PDF做的還不錯,下邊一個例子關于IText的,列出了大部份常用的功能。
          import java.io.FileOutputStream;

          import com.lowagie.text.Cell;
          import com.lowagie.text.Document;
          import com.lowagie.text.Font;
          import com.lowagie.text.PageSize;
          import com.lowagie.text.Paragraph;
          import com.lowagie.text.Table;
          import com.lowagie.text.pdf.BaseFont;
          import com.lowagie.text.pdf.PdfWriter;


          public class Example {
              
          private static Font FONT_11;
              
          private static BaseFont msgothic;
              
          public static void main(String[] args) throws Exception{
                  
          try{
                      
          //定義字體
                      msgothic = BaseFont.createFont("D:\\workspace\\UseIText\\MSMINCHO.TTC,0",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
                      FONT_11
          =new Font(msgothic);
                      FONT_11.setSize(
          11);
                  }
          catch(Exception e){
                      e.printStackTrace();
                  }

                  
          //為了測試方便,加入自動關閉打開acrord32
                  Runtime.getRuntime().exec("tskill acrord32").waitFor();
                  Thread.sleep(
          1000);
                  Document document
          =new Document(PageSize.A4.rotate());
                  
          //寫文件
                  PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("d:/temp.pdf"));
                  document.open();
                  makeDocument(document);
                  document.close();
                  
          //為了測試方便,加入自動關閉打開acrord32
                  Runtime.getRuntime().exec("\"c:\\Program Files\\Adobe\\Acrobat 7.0\\Reader\\acrord32.exe\" d:/temp.pdf").waitFor();
              }

              
          public static void makeDocument(Document document)throws Exception{
                  
          //table大部份操做類似與html,下邊是一些常用的參數
                  
          //3是總的列數,也可以同時指定行數和列數new Table(3,4)
                  Table table=new Table(3);
                  
          //table的寬度
                  table.setWidth(98);
                  
          //類似html的cellSpaceing
                  table.setSpacing(2);
                  
          //每一列的寬度,是比例不是固定寬度
                  table.setWidths(new int[]{10,30,60});
                  
          //對齊方式
                  table.setAlignment("CENTER");
                  
          //table是否有邊框
                  table.setBorder(0);
                  
          //cell默認是否有邊框
                  table.setDefaultCellBorder(0);
                  
          //自動填充空白
                  table.setAutoFillEmptyCells(true);
                  
          int n=10;
                  
          for(int i=0;i<n;i++){
                      makeContent(table);
                  }

                  
          //新的一頁要加上這句
                  document.newPage();
                  
          for(int i=0;i<n;i++){
                      makeContent(table);
                  }

                  document.add(table);
              }

              
          public static void  makeContent(Table table)throws Exception {
                  
          int len = table.columns();
                  
          for(int i=0;i<len;i++){
                      String testStr 
          = "test"+i+"xxxxxxxxxxxxxxxxxxxxxx";
                      Cell cell
          =new Cell(testStr);
                      
          //max lines ,設置成1防止換行,配合cell寬度,可以實現自動截取
                      cell.setMaxLines(1);
                      table.addCell(cell);
                  }

              }

              
          //構造一個自定義的cell
              public static Cell makeCell(int colspan,String align,int maxLines,Font font){
                  Cell cell 
          = null;
                  Paragraph paragraph 
          =null;
                  
          try{
                      
          //使用自定義字體
                      paragraph=new Paragraph("testxxx",font);
                      cell
          =new Cell(paragraph);
                      
          //設置colspan,同樣的方法可以設置rowspan
                      if(colspan >1){
                          cell.setColspan(colspan);
                      }

                      
          //設置對齊
                      if((align != null&& (!align.equals(""))){
                          cell.setHorizontalAlignment(align);
                      }

                      
          //設置maxlines
                      cell.setMaxLines(maxLines);
                  }
          catch(Exception e){
                      e.printStackTrace();
                  }

                  
          return cell;
              }

          }

          posted on 2007-01-29 11:55 dreamstone 閱讀(11969) 評論(14)  編輯  收藏 所屬分類: 利器其它開源框架

          評論

          # re: 使用IText打印PDF 2007-01-29 16:42 寸土



          不錯!!!  回復  更多評論   

          # re: 使用IText打印PDF 2007-01-30 08:40 robin0925

          不知道,對於中文的打印,有沒有解決換行的問題,即:如果換行的時候,行首是標點的情況的處理?  回復  更多評論   

          # re: 使用IText打印PDF[未登錄] 2007-01-30 21:25 dreamstone

          標點的問題我當時也查了一下,沒有找到IText提供的解決方案,有個臨時辦法是取一下列寬,然后算一下,如果位置剛好是標點就在標點前的一個字加一個空格,擠下一個字來,但這樣的問題造成要多次算。也在找解決方案  回復  更多評論   

          # re: 使用IText打印PDF 2007-03-02 09:28 dreamstone

          補充幾個問題的答案:
          如何取得一個空的Cell
          Cell cell = Cell.getDummyCell();空格的情況會被忽略
            回復  更多評論   

          # re: 使用IText打印PDF 2007-03-02 09:29 dreamstone

          如何調整行高
          調整字體大小就可以
            回復  更多評論   

          # re: 使用IText打印PDF 2007-03-02 09:30 dreamstone

          如何調整空行的行高
          也是用字體,但是要用\n來構造空行
          Paragraph paragraph=new Paragraph("\n",FONT_44);
          Cell cell=new Cell(paragraph);
          cell.setColspan(len);
          cell.setMaxLines(1);
          table.addCell(cell);   回復  更多評論   

          # re: 使用IText打印PDF 2007-03-02 09:30 dreamstone

          如何打印一條線:
          用DummyCell來構造空行,同時設置邊框顏色
          Cell cell = Cell.getDummyCell();
          cell.setColspan(len);
          cell.setMaxLines(1);
          cell.setBorderColorBottom(Color.black);
          cell.setBorderWidthBottom(1);
          table.addCell(cell);   回復  更多評論   

          # re: 使用IText打印PDF 2007-03-02 09:30 dreamstone

          強行調整行高,縮小行間距
          Paragraph paragraph=new Paragraph(content,font);
          Cell cell=new Cell(paragraph);
          cell.setColspan(colspan);
          cell.setHorizontalAlignment(align);
          //主要是這個
          cell.setLeading(font.size());
          cell.setMaxLines(1);
          table.addCell(cell);  回復  更多評論   

          # re: 使用IText打印PDF 2008-05-21 17:42 流浪的水仙

          請問有辦法把某一列的寬度固定么?  回復  更多評論   

          # re: 使用IText打印PDF[未登錄] 2008-08-07 15:52 新人

          新手問個問題:
          這代碼是不是只能生成pdf文件,再手動打印,能不能再程序中實現打印,
          即自動打印生成的pdf文件。  回復  更多評論   

          # re: 使用IText打印PDF 2008-10-06 16:09 sdsd

          ok,thank you!  回復  更多評論   

          # re: 使用IText打印PDF[未登錄] 2012-11-06 16:11 玲玲

          標點符號在首行的問題有相關的解決方案嗎  回復  更多評論   

          # re: 使用IText打印PDF[未登錄] 2013-05-06 19:18 lg

          @dreamstone
          趕緊尋找pdf中中文標點符號顯示在首字母處的問題。  回復  更多評論   

          # re: 使用IText打印PDF[未登錄] 2014-03-05 17:16 x

          請問如何在已有的pdf中添加表格呢?  回復  更多評論   

          主站蜘蛛池模板: 紫金县| 甘肃省| 宝应县| 鄯善县| 连平县| 临沭县| 华阴市| 肃北| 大安市| 盱眙县| 晋江市| 新密市| 阜宁县| 四平市| 平山县| 刚察县| 巫山县| 日照市| 临猗县| 安西县| 噶尔县| 长葛市| 顺平县| 松潘县| 静安区| 绥阳县| 大丰市| 本溪| 卢氏县| 泽普县| 射阳县| 凤庆县| 白山市| 滨州市| 华蓥市| 盖州市| 淮安市| 拜泉县| 万安县| 嘉定区| 文水县|