1、展示效果
2、相关功能实现
JAVA开发 使用Apache PDFBox库生成PDF文件,绘制表格
3、实现代码
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TableInPDFBox {
private static final int WARPNUM = 11; //每行显示的字符数量,一个中文占一个字符,其他字符占0.5个
//判断输入的字符是不是中文
public static boolean isChineseChar(char ch) {
String regex = "[\u4E00-\u9FA5]";
String charToCheck = String.valueOf(ch);
return charToCheck.matches(regex);
}
//每行显示的字符数量,获取每个字符串所需要的行数
public static int GetWarpRowByString(String str){
float sum = 0 ; //sum 用来计算中文字符
int row = 0,index=0;
char[] tempArr = str.toCharArray();
for(int i = 0;i<tempArr.length;i++){
if(isChineseChar(tempArr[i])){
sum +=1;
}else{
sum +=0.5f;
}
if(sum == WARPNUM){
row++;
sum=0;
}else if(sum>WARPNUM){
i--;
row++;
sum = 0;
}else{
}
}
row = row +1;
return row;
}
//根据每行的内容,获取最大需要换行的行数(只比较爱好、优势两列)
public static int[] ConfirmRowLength(String[][] stu,int index){
int[] MaxlengthArr = new int[stu.length];
MaxlengthArr[0] = 1;
for(int i = 1;i<stu.length;i++){
int max = GetWarpRowByString(stu[i][0]);
for(int j=index;j<stu[i].length;j++){
int strRow = GetWarpRowByString(stu[i][j]);
if(strRow>max){
max = strRow;
}
}
MaxlengthArr[i] = max;
}
return MaxlengthArr;
}
//将字符串按照最大每行显示数进行分割,保存到字符串数组中
public static String[] GetWarpStr(int rowLength,String str){
float sum = 0.0f;
String[] returnStr=new String[rowLength];
int returnIndex = 0;
String temp="";
char[] strArr = str.toCharArray();
for(int i= 0;i<strArr.length;i++){
if(isChineseChar(strArr[i])){
sum = sum + 1.0f;
}else{
sum+=0.5f;
}
temp = temp + strArr[i];
if(sum == WARPNUM){
returnStr[returnIndex++]= temp;
temp = "";
sum = 0.0f;
}else if(sum >= WARPNUM){
returnStr[returnIndex++]= temp.substring(0,temp.length()-1);
i--;
temp = "";
sum = 0.0f;
}else{
}
}
if(!temp.isEmpty() && returnIndex<rowLength){
returnStr[returnIndex++] = temp;
}
return returnStr;
}
public static void main(String[] args) {
PDDocument document = null;
String[][] stuData = new String[][]{{"姓名","工号","部门","爱好","优势"}
,{"张三丰","WD0001","武当派","阅读让我们获得知识和灵感,运动可以让我们保持身体健康。阅读让我们获得知识和灵感,运动可以让我们保持身体健康。阅读让我们获得知识和灵感,运动可以让我们保持身体健康。阅读让我们获得知识和灵感,运动可以让我们保持身体健康。","武当派和三丰派的祖师,会少林武功和九阳真经,武功特点是阴阳互融、刚柔并济,达到了武学的巅峰。真是泰裤辣。武功特点是阴阳互融、刚柔并济,达到了武学的巅峰。真是泰裤辣"}
,{"郭 襄","EM0001","峨嵋派","书法有助于修养身心,陶冶情操,既练习了字,也可以修炼沉稳的性格.书法有助于修养身心,陶冶情操,既练习了字,也可以修炼沉稳的性格。","会峨眉九阳功,擅长黄药师最精妙的剑法之一的落英剑法。"}};
try {
int[] lengthArr = ConfirmRowLength(stuData,3);
float[] rowIndxArr = new float[stuData.length+1];
float[] colIndxArr = new float[stuData[0].length+1];
document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss");
String dateFormat = sdf.format(date);
// 表格参数
int rows = 3;
int cols = 5;
float cellWidth1 = 60 , cellWidth2 = 150;
float cellHeight = 30;
float xStart = 30,xEnd = xStart + 3 * cellWidth1+2*cellWidth2; //横向开始线的x点,横向结束线的x点
float yStart = 700,yEnd = 0; //纵向开始线的y点,纵向结束线的y点
float sumRows = cellHeight;
// 绘制表格x轴线
for (int i = 0; i <= rows; i++) {
float rowY =0;
if(i == 0){
rowY = yStart - i * cellHeight;
}else{
sumRows = sumRows +lengthArr[i-1]*10+5*2;
rowY = yStart - sumRows;
yEnd = rowY;
}
rowIndxArr[i] = rowY;
contentStream.moveTo(xStart, rowY);
contentStream.lineTo(xEnd, rowY);
contentStream.stroke();
}
//绘制表格y轴线
for (int j = 0; j <= cols; j++) {
float colX;
if(j<=3){
colX= xStart + j * cellWidth1;
}else{
colX = xStart +3*cellWidth1 +(j-3)*cellWidth2;
}
colIndxArr[j] = colX;
contentStream.moveTo(colX, yStart);
contentStream.lineTo(colX, yEnd);
contentStream.stroke();
}
// 填写表格内容
File fontFile = new File("C:\\Windows\\Fonts\\SIMLI.TTF"); // 字体文件路径
PDType0Font font = PDType0Font.load(document, fontFile);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
String text = stuData[i][j];
String[] rowText = GetWarpStr(lengthArr[i],text);
for(int k = 0;k<rowText.length && rowText[k]!=null;k++){
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(colIndxArr[j]+5, rowIndxArr[i]-10-10*k);
contentStream.showText(rowText[k]);
contentStream.endText();
}
}
}
contentStream.close(); // 关闭内容流
// 保存PDF
document.save("D:\\JAVAFILE\\attachments\\pdfs\\" + dateFormat + " table.pdf");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (document != null) {
try {
document.close(); // 关闭文档
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
标签:JAVA,String,表格,int,sum,float,new,PDF,contentStream
From: https://blog.csdn.net/a_waste_only/article/details/139609582