java: itext 5.5 create pdf
/** * 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(); } */ }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)