当前位置: 首页 > news >正文

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;
}
}

相关文章:

  • 微信三方登录相关(Swift)
  • 大数加法模板
  • System类
  • PyOdps 0.4版本发布,从一个故事说起
  • 电话面试总结
  • link visited hover actived顺序
  • Log4Net 在ASP.NET WebForm 和 MVC的全局配置
  • 也谈谈Unity的transform使用
  • 多个文本框录入,使用回车键替找Tab键
  • debian8 更新系统内核
  • 查询SQL存储过程创建时间
  • 利用反射——查看类的成员
  • 【码云周刊第 12 期】走过微软20年,埋头并发编程15年,如何减少代码的认知负荷?...
  • 直方图、基数、选择性、群集因子
  • Codeforces 771C:Bear and Tree Jumps
  • Android 初级面试者拾遗(前台界面篇)之 Activity 和 Fragment
  • Android系统模拟器绘制实现概述
  • const let
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • ES6语法详解(一)
  • java中的hashCode
  • maven工程打包jar以及java jar命令的classpath使用
  • MD5加密原理解析及OC版原理实现
  • mysql_config not found
  • React-redux的原理以及使用
  • Redis字符串类型内部编码剖析
  • spring + angular 实现导出excel
  • Spring框架之我见(三)——IOC、AOP
  • Terraform入门 - 1. 安装Terraform
  • UEditor初始化失败(实例已存在,但视图未渲染出来,单页化)
  • vue-router 实现分析
  • 从输入URL到页面加载发生了什么
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 前端设计模式
  • 如何用vue打造一个移动端音乐播放器
  • 实现菜单下拉伸展折叠效果demo
  • nb
  • FaaS 的简单实践
  • HanLP分词命名实体提取详解
  • 树莓派用上kodexplorer也能玩成私有网盘
  • ​LeetCode解法汇总2182. 构造限制重复的字符串
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • #数学建模# 线性规划问题的Matlab求解
  • #微信小程序:微信小程序常见的配置传旨
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (C#)一个最简单的链表类
  • (附源码)python房屋租赁管理系统 毕业设计 745613
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (原創) 如何解决make kernel时『clock skew detected』的warning? (OS) (Linux)
  • (转)拼包函数及网络封包的异常处理(含代码)
  • (转)详解PHP处理密码的几种方式
  • .apk 成为历史!
  • .bashrc在哪里,alias妙用
  • .net Signalr 使用笔记