pom引入itext的时候如果提示资源下载失败,尝试改为阿里的镜像地址再次下载。
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext-rtf</artifactId>
<version>2.1.7</version>
</dependency>
public class fileUtil {
// 黑体
private static String TTF;
// 黑体16号字
private static String FONT_SIZE_16 ;
static {
try {
TTF = "C:\\Windows\\Fonts\\simhei.ttf";
BaseFont baseFont16 = BaseFont.createFont(TTF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
FONT_SIZE_16 = new Font(baseFont16, 16, Font.NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取txt文件的内容
*
* @param file 想要读取的文件对象
* @return 返回string
*/
public static String readContent(File file) {
StringBuilder result = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String s;
while ((s = br.readLine()) != null) {
result.append(System.lineSeparator()).append(s);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}
/**
* 读取txt文件的内容
*
* @param file 想要读取的文件对象
* @return 返回byte[]
*/
public static byte[] readFileToByte(File f) throws IOException{
byte buff[] = new byte[(int) f.length()];
FileInputStream in = null;
try {
in = new FileInputStream(f);
in.read(buff);
} catch (IOException e) {
throw e;
} finally {
if (in != null)
in.close();
}
return buff;
}
/**
* txt文件转pdf
* @param textPath text文件所在路径(包含文件名及格式 ps:C:\\test\\a.txt)
* @param pdfPath 生成的pdf文件所在路径(包含文件名及格式 ps:C:\\test\\a.pdf)
*/
public static void txtToPdf(String textPath, String pdfPath) throws DocumentException, IOException {
Document document = new Document();
FileOutputStream os = new FileOutputStream(pdfPath);
PdfWriter.getInstance(document, os);
document.open();
BaseFont baseFont = BaseFont.createFont(TTF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont);
InputStreamReader isr = new InputStreamReader(new FileInputStream(textPath), StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(isr);
String str;
while ((str = bufferedReader.readLine()) != null) {
document.add(new Paragraph(str, font));
}
document.close();
}
/**
* pdf文件添加水印
*
* @param srcFile 待转换前文件路径
* @param watermark 水印词
* @param endFile 输出目录
*/
public static void addWatermark(String srcFile, String watermark, String endFile)
throws IOException, DocumentException {
PdfReader reader = null;
PdfStamper stamper = null;
try {
// 待加水印的文件
reader = new PdfReader(new FileInputStream(srcFile));
// 加完水印的文件
stamper = new PdfStamper(reader, new FileOutputStream(endFile));
// 设置字体
BaseFont baseFont = BaseFont.createFont(TTF_16, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// 设置透明度
PdfGState gs = new PdfGState();
// pdf页数
int pageCount = reader.getNumberOfPages() + 1;
PdfContentByte content;
// 循环对每页插入水印
for (int i = 1; i < pageCount; i++) {
// 水印的起始
content = stamper.getOverContent(i);
gs.setFillOpacity(0.5f);
// 设置图形状态
content.setGState(gs);
// 开始
content.beginText();
// 设置颜色 默认为黑色
content.setColorFill(BaseColor.LIGHT_GRAY);
// 设置字体及字号
content.setFontAndSize(baseFont, 50);
// 开始写入水印
content.showTextAligned(Element.ALIGN_BASELINE, watermark, 180, 340, 45);
// 结束
content.endText();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(stamper!=null){
stamper.close();
}
if(reader!=null){
reader.close();
}
}
}
/**
* 生成word使用的类与itext同名了 所以加上了前缀
* @param content 内容
* @param saveFilePath 保存文件路径
*/
public static void createWordFile(String content,String saveFilePath){
try {
com.lowagie.text.Document document = new com.lowagie.text.Document(
com.lowagie.text.PageSize.A4);
RtfWriter2.getInstance(document, new FileOutputStream(saveFilePath));
document.open();
com.lowagie.text.Paragraph p = new com.lowagie.text.Paragraph(content,
new com.lowagie.text.Font(com.lowagie.text.Font.NORMAL, 24, com.lowagie.text.Font.BOLDITALIC, new java.awt.Color(0, 0, 0)) );
p.setAlignment(1);
document.add(p);
/*
* 创建有三列的表格
*/
com.lowagie.text.Table table = new com.lowagie.text.Table(4);
document.add(new com.lowagie.text.Paragraph("制作表格"));
table.setBorderWidth(1);
table.setBorderColor(java.awt.Color.BLACK);
table.setPadding(0);
table.setSpacing(0);
/*
* 添加表头的元素
*/
//单元格
com.lowagie.text.Cell cell = new com.lowagie.text.Cell("表头");
cell.setHeader(true);
//三列
cell.setColspan(3);
//三行
cell.setRowspan(3);
table.addCell(cell);
table.endHeaders();
// 表格的主体
cell = new com.lowagie.text.Cell("cell 2");
//当前单元格占两行,纵向跨度
cell.setRowspan(2);
table.addCell(cell);
table.addCell("1,1");
table.addCell("1,2");
table.addCell("1,3");
table.addCell("1,4");
table.addCell("1,5");
table.addCell(new com.lowagie.text.Paragraph("表格1"));
table.addCell(new com.lowagie.text.Paragraph("表格2"));
table.addCell(new com.lowagie.text.Paragraph("表格3"));
table.addCell(new com.lowagie.text.Paragraph("表格4"));
document.add(table);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}