package com.product.file.util;
|
|
import com.lowagie.text.Document;
|
import com.lowagie.text.Font;
|
import com.lowagie.text.HeaderFooter;
|
import com.lowagie.text.Paragraph;
|
import com.lowagie.text.Phrase;
|
import com.lowagie.text.Rectangle;
|
import com.lowagie.text.pdf.BaseFont;
|
import com.lowagie.text.pdf.PdfWriter;
|
import com.product.common.lang.DateUtils;
|
|
import java.io.*;
|
import java.nio.charset.StandardCharsets;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.regex.Pattern;
|
|
/**
|
* @Author cheng
|
* @Description
|
* @Date 2021/10/28 11:47
|
* @Version 1.0
|
*/
|
public class JavaFileAllTopdfUtil {
|
|
|
/**
|
* window 自带的字体文件
|
*/
|
private static final String FONT = "C:\\Windows\\Fonts\\msyh.ttf";
|
/**
|
* 转换文件的目录 获取该目录下所有的 .java 类型的文件
|
*/
|
private static final String convertingFileDirectories = "D:\\product\\产品运营\\产品著作权\\java";
|
|
// 给定目录的绝对路径,获取该目录下的所有文件(子目录的文件也可递归得到)
|
private static List<File> getFile(String path) {
|
// File对象 可以是文件或者目录
|
File file = new File(path == null ? convertingFileDirectories : path);
|
File[] array = file.listFiles();
|
List<File> result = new ArrayList<>();
|
for (int i = 0; i < array.length; i++) {
|
if (array[i].isFile()) {
|
File file1 = array[i];
|
String name = file1.getName();
|
String file_type = name.substring(name.lastIndexOf(".") + 1);
|
if (file_type.equalsIgnoreCase("java")) {
|
result.add(array[i]);
|
}
|
} else if (array[i].isDirectory()) {
|
result.addAll(getFile(array[i].getPath()));
|
}
|
}
|
return result;
|
}
|
|
|
public static StringBuilder readFileToString() {
|
String pattern = "^[0-9]*[\\s]*$";
|
// 定义返回结果
|
System.out.println("正在查找java文件....");
|
List<File> fileList = getFile(null);
|
StringBuilder sb = new StringBuilder(256);
|
System.out.println("正在读取文件......");
|
for (int i = 0; i < fileList.size(); i++) {
|
if(fileList.get(i).getName().indexOf("前")>=0
|
|| fileList.get(i).getName().indexOf("后")>=0
|
) {
|
try (FileInputStream in = new FileInputStream(fileList.get(i));) {
|
int len;
|
byte[] bytes = new byte[1024];
|
while ((len = in.read(bytes)) != -1) {
|
sb.append(new String(bytes, 0, len));
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
System.out.println("文件读取完成,共读取到" + fileList.size() + "个文件");
|
// 返回拼接好的JSON String
|
|
try (OutputStream os = new FileOutputStream(new File(convertingFileDirectories + "\\javaCode.pdf"));) {
|
Document document = new Document();
|
InputStream streamReader = new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8));
|
Reader reader = new InputStreamReader(streamReader);
|
BufferedReader bufferedReader = new BufferedReader(reader);
|
PdfWriter.getInstance(document, os);
|
|
//方法一:使用Windows系统字体(TrueType)
|
BaseFont baseFont = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
|
Font font = new Font(baseFont);
|
font.setSize(8);
|
HeaderFooter header=new HeaderFooter(new Phrase("企业运营管理平台",font),false);
|
header.setBorder(Rectangle.BOTTOM);
|
header.setAlignment(1);
|
HeaderFooter footer=new HeaderFooter(new Phrase("-",font),new Phrase("-",font));
|
footer.setAlignment(1);
|
footer.setBorder(Rectangle.TOP);
|
document.setFooter(footer);
|
document.setHeader(header);
|
|
document.open();
|
|
|
String str = "";
|
System.out.println("正在写入pdf,请稍等....");
|
while ((str = bufferedReader.readLine()) != null) {
|
boolean isMatch = Pattern.matches(pattern, str);
|
if(!isMatch) {
|
String x="";
|
for(int i=0;i<str.length();i++) {
|
if((int)str.charAt(i)==9) {
|
x+=" ";
|
}else if((int)str.charAt(i)==32) {
|
x+=" ";
|
}else {
|
break;
|
}
|
}
|
document.add(new Paragraph(x+str, font));
|
}
|
}
|
document.close();
|
bufferedReader.close();
|
System.out.println("写入完成,文件保存路径:" + convertingFileDirectories + "\\javaCode.pdf");
|
} catch (Exception e) {
|
System.out.println("写入出错");
|
new File(convertingFileDirectories + "\\javaCode.pdf").delete();
|
e.printStackTrace();
|
}
|
return sb;
|
}
|
|
|
public static void main(String[] args) {
|
readFileToString();
|
|
|
// String str=
|
//// "import java.util.TreeMap;\r\n"
|
// "*\r\n"
|
// //+ "import org.springframework.beans.factory.annotation.Autowired;"
|
// ;
|
// String pattern = "^[0-9]*[\\s]*$";
|
// boolean isMatch = Pattern.matches(pattern, str);
|
//
|
// System.out.println(isMatch);
|
}
|
}
|