小工具知识网

当前位置: 首页 > 行业知识 > java生成pdf

行业知识

java生成pdf

2026-12-05 18:34:34 来源:互联网转载

1.pdf预览

2.maven

                            com.itextpdf            itextpdf            5.5.9                            com.itextpdf            itext-asian            5.2.0        

3.代码

package mis.shared.file;import com.bestvike.linq.Linq;import com.itextpdf.text.*;import com.itextpdf.text.pdf.BaseFont;import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter;import com.itextpdf.text.pdf.draw.LineSeparator;import java.io.FileOutputStream;/** * pdf工具类 */public class PdfUtil {    public static void main(String[] args) throws Exception {        Demo();    }    public static void Demo() throws Exception {        Document document = new Document(PageSize.A4, 50, 50, 30, 30); // 指定页面大小为A4,且自定义页边距(marginLeft、marginRight、marginTop、marginBottom)        PdfWriter.getInstance(document, new FileOutputStream("E:\develop\tmp\test" + System.currentTimeMillis() + ".pdf"));        document.open();        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);        Font keyfont = new Font(bfChinese, 10, Font.BOLD);        Font textfont = new Font(bfChinese, 10, Font.NORMAL);        Paragraph wrap = new Paragraph("\r\n", textfont);        // 段落        Paragraph paragraph = new Paragraph("知之愈明,则行之愈笃;行之愈笃,则知之益明。——朱熹(宋)", textfont);        // 直线        Paragraph line = new Paragraph();        line.add(new Chunk(new LineSeparator()));        // 超链接        Anchor link = new Anchor("点击跳转到百度", textfont);        link.setReference("www.baidu.com");        // 添加图片        Image image = Image.getInstance("E:\develop\tmp\install.png");        image.setAlignment(Image.ALIGN_CENTER);        image.scalePercent(40); //依照比例缩放        // 表格        PdfPTable table = createTable(new float[]{80, 80, 80, 80, 80, 80});        table.addCell(createCell("早上9:00", keyfont, Element.ALIGN_CENTER));        table.addCell(createCell("中午11:00", keyfont, Element.ALIGN_CENTER));        table.addCell(createCell("中午13:00", keyfont, Element.ALIGN_CENTER));        table.addCell(createCell("下午15:00", keyfont, Element.ALIGN_CENTER));        table.addCell(createCell("下午17:00", keyfont, Element.ALIGN_CENTER));        table.addCell(createCell("晚上19:00", keyfont, Element.ALIGN_CENTER));        for (int i = 0; i  

上一篇:黄金市场揭秘,最新9999黄金价格走势深度解析

下一篇:阿里云网站服务器搭建