1821349743@qq.com
2023-02-20 6bc78be53ddf8a6474ea71477fbc9e92149d7938
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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);
    }
}