zoukankan      html  css  js  c++  java
  • iText实现导出pdf文件java代码实现例子

    ///////////////////////////////////主类//////////////////////////////////////////

    package com.iText;

    import java.io.FileOutputStream;

    import com.itextpdf.text.Document;
    import com.itextpdf.text.Element;
    import com.itextpdf.text.Font;
    import com.itextpdf.text.PageSize;
    import com.itextpdf.text.Paragraph;
    import com.itextpdf.text.pdf.BaseFont;
    import com.itextpdf.text.pdf.PdfPTable;
    import com.itextpdf.text.pdf.PdfPTableEvent;
    import com.itextpdf.text.pdf.PdfWriter;

    public class test2 {
    public static void main(String[] args) {

    Document document = new Document(PageSize.A4);
    try {
    BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    Font fontChinese = new Font(baseFontChinese , 16 , Font.BOLD);
    Paragraph title = new Paragraph("包间设备使用情况统计报表",fontChinese);
    Paragraph title2 = new Paragraph(" ");
    //设置标题格式对齐方式
    title.setAlignment(Element.ALIGN_CENTER);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("F:/a.pdf"));
    document.open();
    BaseFont _baseFont = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

    PdfPTable table = new PdfPTable(7);
    table.setHeaderRows(1);
    PdfPTableEvent event = new AlternatingBackground();
    table.setTableEvent(event);

    table.getDefaultCell().setMinimumHeight(30);
    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
    table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
    /*int[] a = {18,18,58,68,18,18,18};
    _tabGoods.setWidths(a);*/
    table.setWidthPercentage(100);
    // 添加标题行
    //_tabGoods.setHeaderRows(2);
    table.addCell(new Paragraph("序号", new Font(_baseFont)));
    table.addCell(new Paragraph("商品名称", new Font(_baseFont)));
    table.addCell(new Paragraph("自定义码", new Font(_baseFont)));
    table.addCell(new Paragraph("规格", new Font(_baseFont)));
    table.addCell(new Paragraph("数量", new Font(_baseFont)));
    table.addCell(new Paragraph("单价", new Font(_baseFont)));
    table.addCell(new Paragraph("小计", new Font(_baseFont)));
    String[] _outTrades = {"序号1","商品名称2","自定义码355555","规格4","数量5","单价6","小计7"};
    // 将商品销售详细信息加入表格
    for(int i = 0; i < 30;) {
    table.addCell(String.valueOf((++i)));
    table.addCell(new Paragraph(_outTrades[1]+i, new Font(_baseFont)));
    table.addCell(new Paragraph(_outTrades[2]+i, new Font(_baseFont)));
    table.addCell(new Paragraph(_outTrades[3]+i, new Font(_baseFont)));
    table.addCell(new Paragraph(_outTrades[4]+i, new Font(_baseFont)));
    table.addCell(new Paragraph(_outTrades[5]+i, new Font(_baseFont)));
    table.addCell(new Paragraph(_outTrades[6]+i, new Font(_baseFont)));
    }
    document.add(title);
    document.add(title2);
    document.add(table);
    document.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    //////////////////////////////////////////////////////////////////

    ///////////////////////////相间背景色的事件监听//////////////////////////

    package com.iText;

    import com.itextpdf.text.BaseColor;
    import com.itextpdf.text.Rectangle;
    import com.itextpdf.text.pdf.PdfContentByte;
    import com.itextpdf.text.pdf.PdfPTable;
    import com.itextpdf.text.pdf.PdfPTableEvent;

    public class AlternatingBackground implements PdfPTableEvent {

    public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {

    int columns;
    Rectangle rect;

    //合适的颜色:(235,235,235)
    int footer = widths.length - table.getFooterRows();
    int header = table.getHeaderRows() - table.getFooterRows();
    for (int row = header; row < footer; row += 2) {
    columns = widths[row].length - 1;
    rect = new Rectangle(widths[row][0], heights[row], widths[row][columns], heights[row + 1]);
    rect.setBackgroundColor(new BaseColor(225,225,225));
    rect.setBorder(Rectangle.NO_BORDER);
    canvases[PdfPTable.BASECANVAS].rectangle(rect);
    }
    }

    }

  • 相关阅读:
    Linux Shell tr 命令详解
    Shell统计每个单词出现的个数
    启明4-29团队进度博客
    启明4-28团队进度博客
    启明4-27团队进度博客
    启明4-26团队进度博客
    启明4-25团队进度博客
    启明4-24团队博客
    每日总结8
    启明4-23团队进度博客
  • 原文地址:https://www.cnblogs.com/wbjgogogo/p/6483191.html
Copyright © 2011-2022 走看看