[Java] java生成pdf →→→→→进入此内容的聊天室

来自 , 2020-12-21, 写在 Java, 查看 198 次.
URL http://www.code666.cn/view/5383c731
  1. package com.hundsun.gildata.irp.core.action;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import com.hundsun.gildata.irp.common.util.ItextUtils;
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.PageSize;
  9. import com.itextpdf.text.pdf.PdfWriter;
  10.  
  11. public class MakePdfTest {
  12.        
  13.         private String att;
  14.  
  15.         public void makdPdf() throws IOException {
  16. //              final int headFontSize = 18;
  17. //              final int sectionFontSize = 12;
  18.                 final int margin = 10;
  19.                 String filePath = "d:/aaa/";
  20.                 String fileName = "aa.pdf";
  21.                
  22.                 att = filePath + fileName;
  23.                
  24.                 File file = new File(filePath);
  25.                 if (!file.exists()) {
  26.                         file.mkdirs();
  27.                 }
  28.                 Document document = null;
  29.                 FileOutputStream fos = null;
  30.                 PdfWriter pdf = null;
  31.                 try {
  32.                         document = new Document(PageSize.A4, margin, margin, margin, margin);
  33.                         fos = new FileOutputStream(filePath + fileName);
  34.                         pdf = PdfWriter.getInstance(document, fos);
  35.                         document.open();
  36.  
  37.                         String summary = "  this is a pdf made bycode\n  另起一行".replaceAll("\n", "<br/>").replaceAll(" ", "&nbsp;&nbsp;");
  38.                         document.add(ItextUtils.processViewpoint(summary));
  39.                        
  40.                         att = filePath + fileName;
  41.                        
  42.  
  43.                 } catch (Exception e) {
  44.                         return;
  45.                 } finally {
  46.                         if (document != null) {
  47.                                 document.close();
  48.                         }
  49.                         if (pdf != null) {
  50.                                 pdf.close();
  51.                         }
  52.                         if (fos != null) {
  53.                                 fos.close();
  54.                         }
  55.                        
  56.                 }
  57.         }
  58.  
  59.         public static void main(String[] args) {
  60.                
  61.                 MakePdfTest testMake = new MakePdfTest();
  62.                 try {
  63.                         testMake.makdPdf();
  64.                         System.out.println(testMake.att);
  65.                 } catch (IOException e) {
  66.                         e.printStackTrace();
  67.                 }
  68.  
  69.         }
  70.  
  71. }
  72.  
  73. //java/1341

回复 "java生成pdf "

这儿你可以回复上面这条便签

captcha