java: itext 5.5 create pdf

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
 * encoding: utf-8
 * 版权所有 2024 ©涂聚文有限公司 言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
 * 许可信息查看: https://github.com/itext/itext-publications-examples-java/blob/develop/src/main/java/com/itextpdf/samples/sandbox/fonts/FreeSansBold.java
 * 描述:https://mvnrepository.com/artifact/com.itextpdf/kernel
 * # Author    : geovindu,Geovin Du 涂聚文.
 * # IDE       : IntelliJ IDEA 2023.1 Java 17
 * # Datetime  : 2024 - 2024/11/20 - 20:13
 * # User      : geovindu
 * # Product   : IntelliJ IDEA
 * # Project   : EssentialAlgorithms
 * # File      : SimpleItext.java
 * # explain   : 学习  类
 **/
 
package BLL;
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.awt.geom.Point2D;
//import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.Element.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.font.*;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.pdf.canvas.draw.*;
import com.itextpdf.text.pdf.PdfEncodings;
//import static com.itextpdf.kernel.pdf.PdfName.BaseFont;
import com.itextpdf.text.io.*;
//import com.itextpdf.io.font.PdfEncodings.*;
//import com.itextpdf.layout.Document;
//import com.itextpdf.layout.element.Paragraph;
//import com.itextpdf.layout.element.Text;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.fonts.*;
 
import java.util.List;
import java.util.ArrayList;
 
import Model.Orders;
 
 
 
 
 
 
public class SimpleItext {
 
    private static String USER_PASSWORD = "geovindu";
    private static String OWNER_PASSWORD = "geovindu";
 
    public static final String DEST = "./target/sandbox/fonts/chinese.pdf";
 
    public static final String FONT = "./src/main/resources/font/NotoSansCJKsc-Regular.otf";
 
    /*
     * "These are the protagonists in 'Hero', a movie by Zhang Yimou:
     * 無名 (Nameless), 殘劍 (Broken Sword), 飛雪 (Flying Snow), 如月 (Moon), 秦王 (the King), and 長空 (Sky)."
     */
    public static final String TEXT = "These are the protagonists in 'Hero', a movie by Zhang Yimou:\n"
            + "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), "
            + "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), "
            + "\u79e6\u738b (the King), and \u9577\u7a7a (Sky).";
 
    // "十锊埋伏"
    public static final String CHINESE = "\u5341\u950a\u57cb\u4f0f";
 
    // "誰も知らない"
    public static final String JAPANESE = "\u8ab0\u3082\u77e5\u3089\u306a\u3044";
 
    // "빈집"
    public static final String KOREAN = "\ube48\uc9d1";
 
    public void Createpdf()
   {
      Document document = new Document();
      try
      {
          PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("itextExample.pdf"));
          //设置打开文档必码
         // writer.setEncryption(USER_PASSWORD.getBytes(),
                  //OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING,
                  //PdfWriter.ENCRYPTION_AES_128);
          String fontFile2 = "font\\MHeiHK-Light.TTF";
          //BaseFont FSChinese = BaseFont.createFont(fontFile2,BaseFont.is,BaseFont.NOT_EMBEDDED);//这是设置仿宋基本字体
 
 
          String path = "C:/WINDOWS/Fonts/simhei.ttf";//windows里的字体资源路径
          BaseFont bf = BaseFont.createFont(fontFile2, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
          Font font = new Font(bf, 10f, Font.NORMAL, BaseColor.BLACK);
 
          //PdfFont f2 = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H");
         // PdfFont pfont = PdfFontFactory.createFont(fontFile2, PdfEncodings.IDENTITY_H);//PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED
         // PdfFont pfont2= PdfFontFactory.createRegisteredFont(fontFile2, PdfEncodings.IDENTITY_H);
 
          //Font text=new Font(font,16,Font.NORMAL);//这里设置字体大小和粗细
 
          //PdfFont f = PdfFontFactory.createFont(fontFile2, PdfEncodings.IDENTITY_H);
          //Paragraph p = new Paragraph("H\u2082SO\u2074").setFont(f).setFontSize(10);
 
          document.open();
          //document.setFont(pfont);
          document.open();
          document.add(new Paragraph("Some content here",font));
 
          //Set attributes here
          document.addAuthor("Geovin Du 涂聚文");
          document.addCreationDate();
          document.addCreator("dusystem.com");
          document.addTitle("Set Attribute Example");
          document.addSubject("An example to show how attributes can be added to pdf files.");
          PdfPTable table = new PdfPTable(3); // 3 columns.
          table.setWidthPercentage(100); //Width 100%
          table.setSpacingBefore(10f); //Space before table
          table.setSpacingAfter(10f); //Space after table
 
 
          //Set Column widths
          float[] columnWidths = {1f, 1f, 1f};
          table.setWidths(columnWidths);
 
          PdfPCell cell1 = new PdfPCell(new Paragraph("深圳市",font));
          cell1.setBorderColor(BaseColor.BLUE);
          cell1.setPaddingLeft(10);
          cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
          cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
 
          PdfPCell cell2 = new PdfPCell(new Paragraph("南山區",font));
          cell2.setBorderColor(BaseColor.GREEN);
          cell2.setPaddingLeft(10);
          cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
          cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
 
          PdfPCell cell3 = new PdfPCell(new Paragraph("科技園",font));
          cell3.setBorderColor(BaseColor.RED);
          cell3.setPaddingLeft(10);
          cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
          cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
 
          //To avoid having the cell border and the content overlap, if you are having thick cell borders
          //cell1.setUserBorderPadding(true);
          //cell2.setUserBorderPadding(true);
          //cell3.setUserBorderPadding(true);
          List<Orders> orders = new ArrayList<>();
          Orders order = new Orders();
          order.setProductId(1); ;
          order.setProduct("中國電器");
          order.setQty(50);
          order.setPrice(52.0);
          orders.add(order);
 
          order = new Orders();
          order.setProductId(2);
          order.setProduct ("日本電器");
          order.setQty(150);
          order.setPrice(252.0);
          orders.add(order);
 
 
          table.addCell(cell1);
          table.addCell(cell2);
          table.addCell(cell3);
 
          document.add(table);
 
          table = new PdfPTable(new float[] { 2, 1, 2 });
          table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
          table.addCell(new Paragraph("產品ID",font));
          table.addCell(new Paragraph("名稱",font));
          table.addCell(new Paragraph("書量",font));
          table.setHeaderRows(1);
          PdfPCell[] cells = table.getRow(0).getCells();
          for (int j=0;j<cells.length;j++){
              cells[j].setBackgroundColor(BaseColor.GRAY);
          }
          for (Orders s:orders){
              table.addCell(String.valueOf(s.getProductId()));
              table.addCell(new Paragraph(s.getProduct(),font));
              table.addCell(String.valueOf(s.getQty()));
        }
 
          document.add(table);
 
          //Add Image
          Image image1 = Image.getInstance("geovindu.jpg");
          //Fixed Positioning
          image1.setAbsolutePosition(205f, 150f);
          //Scale to new height and new width of image
          image1.scaleAbsolute(100, 100);
          //Add to document
          document.add(image1);
 
 
          document.close();
          writer.close();
 
      } catch (Exception e)
      {
          e.printStackTrace();
      }
 
  }
  /*
    public void manipulatePdf(String dest) throws Exception
    {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
 
        // Overwrite some default document font-related properties. From now on they will be used for all the elements
        // added to the document unless they are overwritten inside these elements
        //doc.setFont(font);
 
        doc.add(new Paragraph(TEXT));
        doc.add(new Paragraph(CHINESE));
        doc.add(new Paragraph(JAPANESE));
        doc.add(new Paragraph(KOREAN));
 
        doc.close();
    }
*/
 
}

  

 

posted @   ®Geovin Du Dream Park™  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示