• java的PDF纵横向打印


    PDF默认是纵向打印的,通过rotate()来让其改变为横向打印,一般在打印A4 12*21纸以及发票的时候会用横向打印。横向打印时页面会出现行转列以及列转行的情况,因此在设置页面大小的时候一定要宽度设置的宽一些,表格的maxWidth设置在500到600之间就行了,不然会打印的表格就会不是太全或者很少。如果你是纵向打印,那么Rectangle设置页面大小就可以根据实际情况设置了。下面附上代码:

        

    package com.ucfgroup.framework.web.app.company.service.process.pdf.pzPrint;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.math.BigDecimal;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import javax.servlet.http.HttpServletRequest;

    import com.itextpdf.text.Document;
    import com.itextpdf.text.Element;
    import com.itextpdf.text.Font;
    import com.itextpdf.text.Phrase;
    import com.itextpdf.text.Rectangle;
    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.ucfgroup.framework.utils.DateUtil;
    import com.ucfgroup.framework.utils.NumberUtil;
    import com.ucfgroup.framework.utils.Rmb;
    import com.ucfgroup.framework.web.app.company.entity.CAccvouchAndDetail;
    import com.ucfgroup.framework.web.app.company.entity.CUserZt;

    public class PDFPrint {
    Rectangle pageSize = new Rectangle(1000,730);
    Document document = new Document(pageSize);// 建立一个Document对象

    private static Font headfont;// 设置字体大小
    private static Font keyfont;// 设置字体大小
    private static Font textfont;// 设置字体大小
    private static Font textfontHead;// 设置字体大小
    private static int fm = 0;// 凭证号:记-后面的分母
    private static int jcount = 0;// 凭证号:记-后面的分子
    @SuppressWarnings("unused")
    private static int pageNumber = 0;// 当前页
    @SuppressWarnings("unused")
    private static int totalPage = 0;// 总页数
    private static int totalXhCount = 0;// 总共循环次数
    BigDecimal totalJfh = BigDecimal.ZERO;//合计借方和
    BigDecimal totalDfh = BigDecimal.ZERO;//合计贷方和
    BigDecimal morepageTotalJF=BigDecimal.ZERO;//计算凭证分页时候的总和

    static {
    BaseFont bfChinese;
    try {
    bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    headfont = new Font(bfChinese, 19, Font.BOLD);// 设置字体大小
    keyfont = new Font(bfChinese, 15, Font.BOLD);// 设置字体大小
    textfont = new Font(bfChinese, 10, Font.NORMAL);// 设置字体大小
    textfontHead = new Font(bfChinese, 11, Font.NORMAL);// 设置字体大小
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public PDFPrint(File file,int totalXhCountp,int pageNumberp,int totalPagep,int fmp ,int jcountp) {
    totalXhCount = totalXhCountp;
    pageNumber = pageNumberp;
    fm = fmp;
    jcount = jcountp;
    document.setPageSize(pageSize.rotate());// 设置页面大小
    document.setMargins(60, 50, 30, 10);
    try {
    PdfWriter.getInstance(document, new FileOutputStream(file));
    document.open();
    } catch (Exception e) {
    e.printStackTrace();
    }

    }

    int maxWidth = 630;

    public PdfPCell createCell(String value, com.itextpdf.text.Font font, int align, int width) {
    PdfPCell cell = new PdfPCell();
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setHorizontalAlignment(align);
    cell.setPhrase(new Phrase(value, font));
    cell.setPaddingBottom(3);
    cell.setPaddingTop(3);
    // cell.setFixedHeight(35);
    return cell;
    }

    public PdfPCell createCell(String value, com.itextpdf.text.Font font, int width) {
    PdfPCell cell = new PdfPCell();
    cell.setPhrase(new Phrase(value, font));
    cell.setPaddingBottom(11);
    cell.setPaddingTop(11);
    // cell.setFixedHeight(35);
    return cell;
    }

    public PdfPCell createCell(String value, com.itextpdf.text.Font font) {
    PdfPCell cell = new PdfPCell();
    cell.setPhrase(new Phrase(value, font));
    cell.setPaddingBottom(11);
    cell.setPaddingTop(11);
    return cell;
    }

    public PdfPCell createCell(String value, com.itextpdf.text.Font font, int align, int colspan, boolean boderFlag,
    int width) {
    PdfPCell cell = new PdfPCell();
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setHorizontalAlignment(align);
    cell.setColspan(colspan);
    cell.setPhrase(new Phrase(value, font));
    cell.setPadding(3.0f);
    if (!boderFlag) {
    cell.setBorder(0);
    // cell.setPaddingBottom(-50);
    }
    cell.setFixedHeight(30);
    return cell;
    }

    public PdfPCell createCell(String value, com.itextpdf.text.Font font, int align, int colspan, boolean boderFlag) {
    PdfPCell cell = new PdfPCell();
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setHorizontalAlignment(align);
    cell.setColspan(colspan);
    cell.setPhrase(new Phrase(value, font));
    cell.setPadding(3.0f);
    if (!boderFlag) {
    cell.setBorder(0);
    }
    cell.setFixedHeight(30);
    if(value.contains("凭证号:记-")){
    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    // cell.setPaddingLeft(-5);
    }
    return cell;
    }

    public PdfPCell createCell(String value, com.itextpdf.text.Font font, int align, int colspan, boolean boderFlag,
    String flag) {
    PdfPCell cell = new PdfPCell();
    cell.setVerticalAlignment(Element.ALIGN_LEFT);
    cell.setHorizontalAlignment(align);
    cell.setColspan(colspan);
    cell.setPhrase(new Phrase(value, font));
    cell.setPadding(3.0f);
    if (!boderFlag) {
    cell.setBorder(0);
    cell.setPaddingTop(15.0f);
    cell.setPaddingBottom(8.0f);
    }
    cell.setPaddingTop(8);
    cell.setPaddingLeft(-2);
    if(flag.equals("3")){
    cell.setPaddingLeft(-50);
    }
    if(flag.equals("4")){
    cell.setPaddingLeft(-30);
    }
    return cell;
    }

    public PdfPTable createTable(int colNumber) {
    PdfPTable table = new PdfPTable(colNumber);
    try {
    table.setTotalWidth(maxWidth);
    table.setLockedWidth(true);
    table.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.getDefaultCell().setBorder(1);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return table;
    }

    public PdfPTable createTable(float[] widths) {
    PdfPTable table = new PdfPTable(widths);
    try {
    table.setTotalWidth(maxWidth);
    table.setLockedWidth(true);
    table.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.getDefaultCell().setBorder(1);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return table;
    }

    public PdfPTable createBlankTable() {
    PdfPTable table = new PdfPTable(1);
    table.getDefaultCell().setBorder(0);
    table.addCell(createCell("", keyfont));
    table.setSpacingAfter(20.0f);
    table.setSpacingBefore(20.0f);
    return table;
    }

    public void generatePDF(HttpServletRequest request, List<CAccvouchAndDetail> list) throws Exception {
    int count = 0;
    CUserZt zt = (CUserZt) request.getSession().getAttribute("ZTModel");
    BigDecimal total = BigDecimal.ZERO;
    BigDecimal totalJf = BigDecimal.ZERO;
    BigDecimal totalDf = BigDecimal.ZERO;
    int caccno = list.get(0).getCaccno();
    String caccnoStr = "" + caccno;
    if (caccno < 10) {
    caccnoStr = "00" + caccno;
    }
    if (caccno >= 10 && caccno < 100) {
    caccnoStr = "0" + caccno;
    }
    if (fm > 1) {
    caccnoStr += "-" + jcount + "/" + fm;
    }
    PdfPTable table = createTable(4);
    table.setWidths(new float[] { 0.22f, 0.42f, 0.18f, 0.18f });// 每个单元格占多宽

    PdfPCell createCellp = createCell("yyyy", headfont, Element.ALIGN_CENTER, 1, false, 258);
    createCellp.setPaddingLeft(25);

    table.addCell(createCell("", headfont, Element.ALIGN_CENTER, 1, false, 202));
    table.addCell(createCellp);
    table.addCell(createCell("", headfont, Element.ALIGN_CENTER, 1, false, 115));
    table.addCell(createCell("附单据数:" + list.get(0).getcFj(), keyfont, Element.ALIGN_RIGHT, 1, false, 115));

    table.addCell(createCell(zt.getZtname(), textfontHead, Element.ALIGN_LEFT, 1, false));
    table.addCell(createCell("日期:" + DateUtil.dateToString(list.get(0).getCdate(), "yyyy-MM-dd"), textfontHead,
    Element.ALIGN_CENTER, 1, false));
    // table.addCell(createCell(" ", textfontHead, Element.ALIGN_CENTER, 1, false));
    PdfPCell caccnoCell = createCell("凭证号:记-" + caccnoStr, textfontHead, Element.ALIGN_RIGHT, 2, false);
    table.addCell(caccnoCell);

    PdfPCell createDigestCell = createCell("摘要", keyfont, Element.ALIGN_CENTER, 202);
    createDigestCell.setBorderWidthLeft(1.5f);
    createDigestCell.setBorderWidthTop(1.5f);
    createDigestCell.setBorderWidthBottom(1.5f);
    createDigestCell.setFixedHeight(25);

    PdfPCell createSubjectCell = createCell("科目名称", keyfont, Element.ALIGN_CENTER, 258);
    createSubjectCell.setBorderWidthTop(1.5f);
    createSubjectCell.setBorderWidthBottom(1.5f);
    createSubjectCell.setFixedHeight(25);

    PdfPCell createJfCell = createCell("借方", keyfont, Element.ALIGN_CENTER, 115);
    createJfCell.setBorderWidthTop(1.5f);
    createJfCell.setBorderWidthBottom(1.5f);
    createJfCell.setFixedHeight(25);

    PdfPCell createDfCell = createCell("贷方", keyfont, Element.ALIGN_CENTER, 115);
    createDfCell.setBorderWidthTop(1.5f);
    createDfCell.setBorderWidthBottom(1.5f);
    createDfCell.setBorderWidthRight(1.5f);
    createDfCell.setFixedHeight(25);

    table.addCell(createDigestCell);
    table.addCell(createSubjectCell);
    table.addCell(createJfCell);
    table.addCell(createDfCell);

    for (CAccvouchAndDetail cAccvouchAndDetail : list) {
    PdfPCell createDigestTextLeftCell = createCell(cAccvouchAndDetail.getCdigest(), textfont, 202);
    createDigestTextLeftCell.setBorderWidthLeft(1.5f);

    table.addCell(createDigestTextLeftCell);
    table.addCell(createCell(cAccvouchAndDetail.getCcodename(), textfont, 258));

    PdfPCell createCellj = createCell(judgeMoney(cAccvouchAndDetail.getcJf()), textfont, 115);
    createCellj.setVerticalAlignment(Element.ALIGN_MIDDLE);
    createCellj.setHorizontalAlignment(Element.ALIGN_RIGHT);

    PdfPCell createCelld = createCell(judgeMoney(cAccvouchAndDetail.getcDf()), textfont, 115);
    createCelld.setVerticalAlignment(Element.ALIGN_MIDDLE);
    createCelld.setHorizontalAlignment(Element.ALIGN_RIGHT);
    createCelld.setBorderWidthRight(1.5f);

    table.addCell(createCellj);
    table.addCell(createCelld);
    total = total.add(cAccvouchAndDetail.getcJf() == null ? BigDecimal.ZERO : cAccvouchAndDetail.getcJf());
    totalJf = totalJf.add(cAccvouchAndDetail.getcJf() == null ? BigDecimal.ZERO : cAccvouchAndDetail.getcJf());//借方合计 ---
    totalDf = totalDf.add(cAccvouchAndDetail.getcDf() == null ? BigDecimal.ZERO : cAccvouchAndDetail.getcDf());//贷方合计 ---
    count += 1;
    }
    //借方和贷方和的累加 --
    for(;totalXhCount<jcount;){
    totalJfh = totalJfh.add(totalJf);
    totalDfh = totalDfh.add(totalDf);
    break;
    }
    for (int i = count + 1; i < 6; i++) {
    PdfPCell leftBlankCell = createCell(" ", textfont, 202);
    leftBlankCell.setBorderWidthLeft(1.5f);
    table.addCell(leftBlankCell);
    table.addCell(createCell(" ", textfont, 258));
    table.addCell(createCell(" ", textfont, 115));
    PdfPCell rightBlankCell = createCell(" ", textfont, 115);
    rightBlankCell.setBorderWidthRight(1.5f);
    table.addCell(rightBlankCell);
    }

    if (total.compareTo(BigDecimal.ZERO) != 0|| totalJf.compareTo(BigDecimal.ZERO) != 0 || totalDf.compareTo(BigDecimal.ZERO) != 0) {
    if(jcount > 0 && jcount < fm ){//当凭证为多页
    PdfPCell createCelln = createCell("合计:", textfontHead, Element.ALIGN_RIGHT);
    createCelln.setColspan(2);
    createCelln.setPaddingLeft(4);
    createCelln.setPaddingBottom(0);
    createCelln.setPaddingTop(0);
    createCelln.setFixedHeight(17);
    createCelln.setVerticalAlignment(Element.ALIGN_MIDDLE);
    createCelln.setBorderWidthLeft(1.5f);
    createCelln.setBorderWidthTop(1.5f);
    createCelln.setBorderWidthBottom(1.5f);
    table.addCell(createCelln);
    //添加借方金额到表格
    PdfPCell createCellJf = createCell(judgeMoney(totalJf), textfontHead,115);
    createCellJf.setVerticalAlignment(Element.ALIGN_MIDDLE);
    createCellJf.setHorizontalAlignment(Element.ALIGN_RIGHT);
    createCellJf.setBorderWidthTop(1.5f);
    createCellJf.setBorderWidthBottom(1.5f);

    table.addCell(createCellJf);

    //添加贷方金额到表格
    PdfPCell createCellDf = createCell(judgeMoney(totalDf), textfontHead,115);
    createCellDf.setVerticalAlignment(Element.ALIGN_MIDDLE);
    createCellDf.setHorizontalAlignment(Element.ALIGN_RIGHT);
    createCellDf.setBorderWidthRight(1.5f);
    createCellDf.setBorderWidthTop(1.5f);
    createCellDf.setBorderWidthBottom(1.5f);
    table.addCell(createCellDf);
    }
    if(jcount == fm ){//当凭证为最后一页时

    PdfPCell createCell = createCell("合计:" + Rmb.CmycurD(morepageTotalJF.toString()), textfont,
    Element.ALIGN_LEFT);
    createCell.setColspan(2);
    createCell.setPaddingLeft(4);
    createCell.setPaddingBottom(0);
    createCell.setPaddingTop(0);
    createCell.setFixedHeight(17);
    createCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    createCell.setBorderWidthLeft(1.5f);
    createCell.setBorderWidthTop(1.5f);
    createCell.setBorderWidthBottom(1.5f);
    table.addCell(createCell);


    PdfPCell createCellJfs = createCell(judgeMoney(morepageTotalJF), textfont,115);
    createCellJfs.setVerticalAlignment(Element.ALIGN_MIDDLE);
    createCellJfs.setHorizontalAlignment(Element.ALIGN_RIGHT);
    createCellJfs.setBorderWidthTop(1.5f);
    createCellJfs.setBorderWidthBottom(1.5f);

    table.addCell(createCellJfs);

    PdfPCell createCellDfs = createCell(judgeMoney(morepageTotalJF), textfont,115);
    createCellDfs.setVerticalAlignment(Element.ALIGN_MIDDLE);
    createCellDfs.setHorizontalAlignment(Element.ALIGN_RIGHT);
    createCellDfs.setBorderWidthRight(1.5f);
    createCellDfs.setBorderWidthTop(1.5f);
    createCellDfs.setBorderWidthBottom(1.5f);

    table.addCell(createCellDfs);

    morepageTotalJF=BigDecimal.ZERO;//清除上次的凭证分页金额总和
    }
    if (jcount == 0 && fm == 1) {//当凭证只有一页
    PdfPCell createCell = createCell("合计:" + Rmb.CmycurD(total.toString()), textfont, Element.ALIGN_LEFT);
    createCell.setColspan(2);
    createCell.setPaddingLeft(4);
    createCell.setPaddingBottom(0);
    createCell.setPaddingTop(0);
    createCell.setFixedHeight(17);
    createCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    createCell.setBorderWidthLeft(1.5f);
    createCell.setBorderWidthTop(1.5f);
    createCell.setBorderWidthBottom(1.5f);
    table.addCell(createCell);

    PdfPCell createCellJf = createCell(judgeMoney(totalJf), textfont,115);
    createCellJf.setVerticalAlignment(Element.ALIGN_MIDDLE);
    createCellJf.setHorizontalAlignment(Element.ALIGN_RIGHT);
    createCellJf.setBorderWidthTop(1.5f);
    createCellJf.setBorderWidthBottom(1.5f);

    table.addCell(createCellJf);

    PdfPCell createCellDf = createCell(judgeMoney(totalDf), textfont,115);
    createCellDf.setVerticalAlignment(Element.ALIGN_MIDDLE);
    createCellDf.setHorizontalAlignment(Element.ALIGN_RIGHT);
    createCellDf.setBorderWidthTop(1.5f);
    createCellDf.setBorderWidthBottom(1.5f);
    createCellDf.setBorderWidthRight(1.5f);

    table.addCell(createCellDf);
    }
    } else {
    PdfPCell createCell = createCell("合计:", textfont, Element.ALIGN_LEFT);
    createCell.setColspan(2);
    createCell.setPaddingLeft(4);
    createCell.setPaddingBottom(0);
    createCell.setPaddingTop(0);
    createCell.setFixedHeight(17);
    createCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    createCell.setBorderWidthTop(1.5f);
    createCell.setBorderWidthBottom(1.5f);
    createCell.setBorderWidthLeft(1.5f);
    table.addCell(createCell);

    table.addCell(createCell(" ", keyfont, Element.ALIGN_CENTER,115));
    PdfPCell createSumCell = createCell(" ", keyfont, Element.ALIGN_CENTER,115);
    table.addCell(createSumCell);
    createSumCell.setBorderWidthTop(1.5f);
    createSumCell.setBorderWidthBottom(1.5f);
    createSumCell.setBorderWidthRight(1.5f);
    }

    String shrName = zt.getShrName() == null ? "" : zt.getShrName();
    String zdrName = zt.getZdrName() == null ? "" : zt.getZdrName();
    String zgrName = zt.getZgName() == null ? "" : zt.getZgName();

    table.addCell(createCell("xxxx:" + zdrName, textfont, Element.ALIGN_LEFT, 1, false, "1"));
    table.addCell(createCell("xxxx:", textfont, Element.ALIGN_LEFT, 1, false, "2"));
    table.addCell(createCell("xxxx:"+shrName, textfont, Element.ALIGN_LEFT, 1, false, "3"));
    table.addCell(createCell("xxxx:" + zgrName, textfont, Element.ALIGN_LEFT, 1, false, "4"));

    document.add(table);

    // // 创建Chunk对象,设置下划线的厚度为1
    // Chunk strike = new Chunk("");
    // strike.setUnderline(1f, 3f);
    // document.add(strike);
    }

    public void printPdf(HttpServletRequest request, Map<String, List<CAccvouchAndDetail>> map, List<String> aidList,int fmp,int jcountp)
    throws Exception {
    fm = fmp;
    jcount = jcountp;
    int mapSize = aidList.size();
    int mapYs = mapSize % 2;
    if (mapYs > 0) {
    totalPage = mapSize / 2 + 1;
    } else {
    totalPage = mapSize / 2;
    }
    for (String aid : aidList) {
    List<CAccvouchAndDetail> list = map.get(aid);
    Map<Integer, List<CAccvouchAndDetail>> cfListMap = new HashMap<Integer, List<CAccvouchAndDetail>>();
    int size = list.size();
    int cs = size / 5;
    int ys = size % 5;
    if (cs > 1) {
    if (ys > 0) {
    fm = cs + 1;
    } else {
    fm = cs;
    }
    }
    if (cs == 1 && ys > 0) {
    fm = 2;
    }
    if (size > 5) {
    int count = 0;
    int jsq = 0;// 计数器
    for (CAccvouchAndDetail cAccvouchAndDetail : list) {
    morepageTotalJF = morepageTotalJF.add(cAccvouchAndDetail.getcJf());
    List<CAccvouchAndDetail> tempList = cfListMap.get(count);
    if (null == tempList || jsq == 5) {
    jsq = 0;
    tempList = new ArrayList<CAccvouchAndDetail>();
    tempList.add(cAccvouchAndDetail);
    count += 1;
    cfListMap.put(count, tempList);
    jsq += 1;
    } else {
    tempList.add(cAccvouchAndDetail);
    jsq += 1;
    }
    }
    jcount = 0;
    for (int i = 0; i < fm; i++) {
    List<CAccvouchAndDetail> cfList = cfListMap.get(i + 1);
    jcount += 1;
    generatePDF(request, cfList);
    document.newPage();
    totalXhCount += 1;
    }
    } else {
    fm = 1;
    jcount = 0;
    generatePDF(request, list);
    document.newPage();
    totalXhCount += 1;
    }

    }
    document.close();
    }

    public static String leftPad(String str, int i) {
    int addSpaceNo = i - str.length();
    String space = "";
    for (int k = 0; k < addSpaceNo; k++) {
    space = " " + space;
    }
    ;
    String result = space + str;
    return result;
    }

    private String judgeMoney(BigDecimal bd) {
    String temp = "";
    if(bd != null && bd.compareTo(BigDecimal.ZERO) != 0){
    temp = NumberUtil.msStrAddComma(NumberUtil.msBigDecimalFormat(bd));
    }
    return temp;
    }
    }

  • 相关阅读:
    Struts2 xml表单验证
    struts2表单验证-整合国际化
    Strut2-Ajax总结
    java中的线性安全和不安全
    智游推送试用
    推送的重连策略
    Android本地通知的实现方式
    MyBatis框架学习二
    Java基础学习 2 (选择结构,循环结构)
    Java基础学习 1 (变量,数据类型,运算符)
  • 原文地址:https://www.cnblogs.com/yanduanduan/p/6527345.html
Copyright © 2020-2023  润新知