我是靠谱客的博主 负责歌曲,最近开发中收集的这篇文章主要介绍使用itext生成带表格pdf文件(包含生成条形码、设置单元格高度、向表格中插入图片、设置输出文件的页面大小),觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
package com.shipfile.pojo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class GetExportPdf {
//public static void main(String[] args) throws DocumentException, FileNotFoundException
{
public String getpdffile(HttpServletRequest request, String path, String job_num, List<ShipPojo> list, String picPath, String packing_date) {
float rate = 2.83333f;
//10厘米X15里面的纸
int width = Math.round(100 * rate);
int height = Math.round(150 * rate);
//设置纸张大小,设置横向纵向只用将宽高互换即可
Document doc = new Document(new Rectangle(height,width),1,1,1,1);
String exportfilepath = path +"/" +list.get(0).getMcat_num()+"-"+System.currentTimeMillis()+".pdf";
//File file = new File("C:\Users\503125053\Desktop\victor\测试1.pdf");
//System.out.println("导出文件的路径为:"+exportfilepath);
File file = new File(exportfilepath);
if (file.exists()) {
file.delete();
}
PdfWriter writer = null;
try {
FileOutputStream foStream = new FileOutputStream(file);
// 把word与输出流绑定
writer = PdfWriter.getInstance(doc, foStream);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 把word与输出流绑定
//RtfWriter2.getInstance(doc, foStream);
// 开始写入word,必须调用这个方法才能进行word的写入
doc.open();
// 加载额外字体的第一种方法:iText本身支持的字体少,这个时候可以使用系统自带的字体
// FontFactory.register("C:\WINDOWS\Fonts\simsun.ttc");
// 这是另一种加载字体的方法,把字体文件放到webapp下,防止某些电脑并没有第一种方法中的字体.
String realPath = request.getSession().getServletContext().getRealPath("/") + "font/";
FontFactory.register(realPath + "SIMSUN.TTC");
FontFactory.register(realPath + "STFANGSO.TTF");
FontFactory.register(realPath + "STKAITI.TTF");
// 对字体文件右键查看详细信息的标题,可以获取getFont方法中第一个参数的字符串.
// 第二个参数不用管是编码集, 第三个参数是字体大小
Font titlefont = FontFactory.getFont("simsun", BaseFont.WINANSI, 11);
// 设置字体大小
titlefont.setSize(11);
//
设置字体格式,只能设置一种格式
titlefont.setStyle(Font.BOLD);
Font titlefont1 = FontFactory.getFont("simsun", BaseFont.WINANSI,16);
// 设置字体大小
titlefont1.setSize(16);
//titlefont1.setSize(Font.BOLD);
// 设置字体的重载方法,可以传递多种样式,比如加粗倾斜
//titlefont.setStyle("bold, italic");
//添加gelogo
Image logo_img = null;
//String imgUrl = "C:\Users\503125053\Desktop\victor/GE_Logo.png";
try {
// 这个方法用于获取图片,可以传递图片的路径,或者可以传递图片的byte[]内容
logo_img = Image.getInstance(picPath);
// 设置图片大小
logo_img.scaleAbsolute(120, 30);
} catch (Exception e) {
e.printStackTrace();
}
//根据list长度进行循环
for (int f = 0; f < list.size(); f++) {
// 申明一个5列的表格
PdfPTable table = new PdfPTable(5);
// 设置表格宽度
//table.setBorderWidth(1);
//防止表格与文本之间有空行
//table.setOffset(1);
// 设置表格每行的单元格的宽度,数组的长度表明了每行的单元格宽度比例
int[] widths = {20, 13, 10, 13, 20};
// 设置每行的单元格
try {
table.setWidths(widths);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 设置表格所占页面的百分比
table.setWidthPercentage(100);
//table.setWidth(100);
// 锁住宽度
//table.setLockedWidth(true);
// 设置表格上面空白宽度
table.setSpacingBefore(10f);
// 设置表格下面空白宽度
table.setSpacingAfter(10f);
// 设置表格默认为无边框
table.getDefaultCell().setBorder(0);
// 设置表格的对齐方式
//table.setAlignment(ALIGN_CENTER);
// table.setAlignment(Table.ALIGN_CENTER);
//列数
for (int i = 0; i < 5; i++) {
//行数
for (int j = 0; j < 3; j++) {
// 申明一个单元格,Phrase表示单元格内容,参数1是单元格的文本内容,参数2是字体
//Cell cell = new Cell(new Phrase("content" + i, titlefont));
PdfPCell cell = new PdfPCell();
//设置单元格样式
//cell.setBackgroundColor(new BaseColor(0xdd7e6b));
cell.setBorder(0);
cell.setBorderWidthTop(0.1f);
cell.setBorderWidthBottom(0.1f);
cell.setBorderWidthLeft(0.1f);
cell.setBorderWidthRight(0.1f);
/*cell.setBorderColorBottom();
cell.setBorderColorLeft(new BaseColor(0x674ea7));
cell.setBorderColorRight(new BaseColor(0x674ea7));
cell.setBorderColorTop(new BaseColor(0x674ea7));*/
cell.setUseAscender(true);//设置可以居中
// 水平对齐方式
//cell.setHorizontalAlignment(Element.ALIGN_CENTER);
// 垂直对齐方式
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//合并单元格
if(i == 0 && j == 0) {
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(1);
// 当前单元格占1行,纵向跨度
cell.setColspan(5);
// 当前单元格占5列,纵向跨度
cell.setFixedHeight(50);
cell.addElement(logo_img);
}else if (i == 0&& j==1) {
//隐藏下边框
cell.disableBorderSide(2);
cell.setFixedHeight(25);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setRowspan(1);
cell.setColspan(3);
cell.addElement(new Phrase("Job#",titlefont));
}else if (i == 0 && j==2) {
//隐藏下边框
cell.disableBorderSide(2);
cell.setFixedHeight(25);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setRowspan(1);
cell.setColspan(2);
cell.addElement(new Phrase("MCAT#",titlefont));
}else if (i == 1 && j==0) {
System.out.println("进入");
//隐藏上边框
cell.disableBorderSide(1);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
cell.setRowspan(1);
cell.setColspan(3);
cell.setFixedHeight(120);
//生成条形码的操作
PdfContentByte cd = writer.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setCodeType(code128.CODE128);
code128.setCode(job_num);
code128.setBarHeight(20);
code128.setFont(null);
Image image128 = code128.createImageWithBarcode(cd, null, null);
image128.scaleAbsolute(180, 30);
image128.setAlignment(1);
cell.addElement(image128);
//cell.addElement(jobcode_img);
//换行用n
Paragraph paragraph = new Paragraph(job_num,titlefont1);
paragraph.setAlignment(1);//设置文字水平居中
cell.addElement(paragraph);
}else if (i == 1 && j==1) {
//隐藏上边框
cell.disableBorderSide(1);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
//cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setRowspan(1);
cell.setColspan(2);
//设置表格高度
cell.setFixedHeight(120);
PdfContentByte cd = writer.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setCodeType(code128.CODE128);
code128.setCode(list.get(f).getMcat_num());
code128.setBarHeight(20);
code128.setFont(null);
Image image128 = code128.createImageWithBarcode(cd, null, null);
image128.scaleAbsolute(120, 30);
image128.setAlignment(1);
cell.addElement(image128);
//cell.addElement(mcatcode_img);
//换行用n
Paragraph paragraph = new Paragraph(list.get(f).getMcat_num(),titlefont1);
paragraph.setAlignment(1);
cell.addElement(paragraph);
}/*else if (i == 2 && j==1) {
cell.setHorizontalAlignment(Cell.ALIGN_LEFT);
cell.setRowspan(1);
cell.setColspan(3);
cell.add(img);
}else if (i == 3 && j==0) {
cell.setHorizontalAlignment(Cell.ALIGN_LEFT);
cell.setRowspan(1);
cell.setColspan(2);
cell.add(img);
}*/else if (i == 1 && j==2) {
//隐藏下边框
cell.disableBorderSide(2);
//设置表格高度
cell.setFixedHeight(25);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.addElement(new Phrase("Size(cm)",titlefont));
}else if (i == 2 && j==0) {
//隐藏下边框
cell.disableBorderSide(2);
cell.setFixedHeight(25);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.addElement(new Phrase("Weight(Kg)",titlefont));
}else if (i == 2 && j==1) {
//隐藏下边框
cell.disableBorderSide(2);
cell.setFixedHeight(25);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.addElement(new Phrase("Number",titlefont));
}else if (i == 2 && j==2) {
//隐藏下边框
cell.disableBorderSide(2);
cell.setFixedHeight(25);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.addElement(new Phrase("CNT#",titlefont));
}else if (i == 3 && j==0) {
//隐藏下边框
cell.disableBorderSide(2);
cell.setFixedHeight(25);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.addElement(new Phrase("Packing date",titlefont));
}else if (i == 3 && j==1) {
//隐藏上边框
cell.disableBorderSide(1);
//设置表格高度
cell.setFixedHeight(60);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
Paragraph paragraph = new Paragraph(list.get(f).getSize(),titlefont1);
paragraph.setAlignment(1);
cell.addElement(paragraph);
}else if (i == 3 && j==2) {
//隐藏上边框
cell.disableBorderSide(1);
cell.setFixedHeight(60);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
Paragraph paragraph = new Paragraph(list.get(f).getWeight(),titlefont1);
paragraph.setAlignment(1);
cell.addElement(paragraph);
}else if (i == 4 && j==0) {
//隐藏上边框
cell.disableBorderSide(1);
cell.setFixedHeight(60);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
Paragraph paragraph = new Paragraph(list.get(f).getNumber().toString(),titlefont1);
paragraph.setAlignment(1);
cell.addElement(paragraph);
}else if (i == 4 && j==1) {
//隐藏上边框
cell.disableBorderSide(1);
cell.setFixedHeight(60);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
Paragraph paragraph = new Paragraph(list.get(f).getCnt(),titlefont1);
paragraph.setAlignment(1);
cell.addElement(paragraph);
}else if (i == 4 && j==2) {
//隐藏上边框
cell.disableBorderSide(1);
cell.setFixedHeight(60);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
Paragraph paragraph = new Paragraph(packing_date,titlefont1);
paragraph.setAlignment(1);
cell.addElement(paragraph);
}
// 将单元格插入到表格
table.addCell(cell);;
}
}
// 将表格添加到word
try {
doc.add(table);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 关闭word文档对象,调用这个方法之后就无法添加内容到这个doc对象了.
doc.close();
System.out.println("执行完成!");
return exportfilepath;
}
//隐藏上边框
//cell.disableBorderSide(1);
//隐藏下边框
// cell.disableBorderSide(2);
//隐藏上、下边框
//cell.disableBorderSide(3);
//隐藏左边框
//cell.disableBorderSide(4);
//隐藏左、上边框
//cell.disableBorderSide(5);
//隐藏左、下边框
//cell.disableBorderSide(6);
//隐藏左、上、下边框
//cell.disableBorderSide(7);
//隐藏右边框
//cell.disableBorderSide(8);
//隐藏右、上边框
// cell.disableBorderSide(9);
//隐藏右、下边框
//cell.disableBorderSide(10);
//隐藏右、上、下边框
//cell.disableBorderSide(11);
//隐藏左、右边框
//cell.disableBorderSide(12);//左右没了
//隐藏上、左、右边框
//cell.disableBorderSide(13);//只剩下
//隐藏下、左、右边框
//cell.disableBorderSide(14);//只剩上
//隐藏全部
//cell.disableBorderSide(15);//全没了
}
最后
以上就是负责歌曲为你收集整理的使用itext生成带表格pdf文件(包含生成条形码、设置单元格高度、向表格中插入图片、设置输出文件的页面大小)的全部内容,希望文章能够帮你解决使用itext生成带表格pdf文件(包含生成条形码、设置单元格高度、向表格中插入图片、设置输出文件的页面大小)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复