杜洪波
23 小时以前 3dd3bc576dada8216099348ad8fd93fc070622b6
PDF操作类
已添加1个文件
已修改1个文件
195 ■■■■■ 文件已修改
pom.xml 17 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/file/util/PDFOperateUtil.java 178 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml
@@ -28,6 +28,14 @@
            <groupId>com.lx</groupId>
            <artifactId>product-server-lucene</artifactId>
        </dependency>
        <dependency>
            <groupId>com.lx</groupId>
            <artifactId>product-server-quartz</artifactId>
        </dependency>
        <dependency>
            <groupId>com.lx</groupId>
            <artifactId>product-server-tool-table</artifactId>
        </dependency>
        <!--releases  -->
        <dependency>
            <groupId>com.lx</groupId>
@@ -93,14 +101,7 @@
            <artifactId>aspose-words</artifactId>
            <version>21.11.1</version>
        </dependency>
        <dependency>
            <groupId>com.lx</groupId>
            <artifactId>product-server-quartz</artifactId>
        </dependency>
        <dependency>
            <groupId>com.lx</groupId>
            <artifactId>product-server-tool-table</artifactId>
        </dependency>
    </dependencies>
</project>
src/main/java/com/product/file/util/PDFOperateUtil.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,178 @@
package com.product.file.util;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.text.PDFTextStripper;
public class PDFOperateUtil {
    /**
     * æ ¹æ®æ–‡æœ¬æ–‡ä»¶åˆ›å»º PDF(使用 PDFBox)
     *
     * @param textFilePath è¾“入文本文件路径
     * @param pdfFilePath  è¾“出 PDF æ–‡ä»¶è·¯å¾„
     * @throws IOException
     */
    public static void createPdfFromTextFile(String textFilePath, String pdfFilePath) throws IOException {
        String content = readTextFile(textFilePath);
        try (PDDocument document = new PDDocument()) {
            PDPage page = new PDPage(PDRectangle.A4);
            document.addPage(page);
            // èŽ·å–å­—ä½“æ–‡ä»¶ï¼ˆä»…æ”¯æŒ Windows/Linux)
            File fontFile = getSystemFontFile();
            if (!fontFile.exists()) {
                throw new IOException("未找到支持的中文字体文件(Windows/Linux)");
            }
            // åŠ è½½å­—ä½“
            PDType0Font font;
            try (FileInputStream fontStream = new FileInputStream(fontFile)) {
                font = PDType0Font.load(document, fontStream);
            }
            // å†™å…¥ PDF å†…容
            try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
                contentStream.beginText();
                contentStream.setFont(font, 12);
                contentStream.newLineAtOffset(50, 700);
                String[] lines = content.split("\n");
                for (String line : lines) {
                    contentStream.showText(line);
                    contentStream.newLineAtOffset(0, -15);
                }
                contentStream.endText();
            }
            document.save(pdfFilePath);
        }
    }
    /**
     * è¯»å– PDF å†…容(使用 PDFBox)
     *
     * @param pdfFilePath PDF æ–‡ä»¶è·¯å¾„
     * @return æå–的文本内容
     * @throws IOException
     */
    public static String readPdfContent(String pdfFilePath) throws IOException {
        try (PDDocument document = PDDocument.load(new File(pdfFilePath))) {
            PDFTextStripper stripper = new PDFTextStripper();
            return stripper.getText(document);
        }
    }
    /**
     * å°† PDF è½¬æ¢ä¸ºå›¾ç‰‡ï¼ˆä½¿ç”¨ PDFBox)
     *
     * @param pdfFilePath    PDF æ–‡ä»¶è·¯å¾„
     * @param outputFolder    è¾“出图片文件夹
     * @param imageName        è¾“出图片文件名
     * @param imageFormat    å›¾ç‰‡æ ¼å¼ï¼ˆå¦‚ "png", "jpg")
     * @throws IOException
     */
    public static void convertPdfToImages(String pdfFilePath, String outputFolder, String imageName, String imageFormat) throws IOException {
        try (PDDocument document = PDDocument.load(new File(pdfFilePath))) {
            PDFRenderer renderer = new PDFRenderer(document);
            // ç¡®ä¿è¾“出文件夹存在
            new File(outputFolder).mkdirs();
            // é€é¡µæ¸²æŸ“为图片(300 DPI)
            for (int page = 0; page < document.getNumberOfPages(); page++) {
                BufferedImage image = renderer.renderImageWithDPI(page, 300);
                String outputFileName = String.format("%s/%s%d.%s", outputFolder, imageName, page + 1, imageFormat);
                ImageIO.write(image, imageFormat, new File(outputFileName));
            }
        }
    }
    /**
     * è¾…助方法:读取文本文件内容
     *
     * @param filePath æ–‡æœ¬æ–‡ä»¶è·¯å¾„
     * @return æ–‡ä»¶å†…容
     * @throws IOException
     */
    private static String readTextFile(String filePath) throws IOException {
        StringBuilder content = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(filePath), StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }
        }
        return content.toString();
    }
    /**
     * èŽ·å–ç³»ç»Ÿå­—ä½“æ–‡ä»¶ï¼ˆä»… Windows/Linux)
     */
    private static File getSystemFontFile() {
        String os = System.getProperty("os.name").toLowerCase();
        String[] possiblePaths;
        if (os.contains("win")) {
            // Windows å­—体目录(优先检查常见中文字体)
            possiblePaths = new String[]{
                System.getenv("WINDIR") + "\\Fonts\\simhei.ttf",  // é»‘体
                System.getenv("WINDIR") + "\\Fonts\\msyh.ttc",   // å¾®è½¯é›…黑
                System.getenv("WINDIR") + "\\Fonts\\mingliu.ttc"  // æ˜Žä½“(备用)
            };
        } else {
            // Linux å­—体目录(优先检查开源中文字体)
            possiblePaths = new String[]{
                "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",  // æ–‡æ³‰é©¿å¾®ç±³é»‘
                "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",  // æ€æºé»‘体
                "/usr/share/fonts/truetype/arphic/ukai.ttc"       // æ–‡é¼Žå­—体(备用)
            };
        }
        // æ£€æŸ¥æ‰€æœ‰å¯èƒ½çš„路径
        for (String path : possiblePaths) {
            File fontFile = new File(path);
            if (fontFile.exists()) {
                return fontFile;
            }
        }
        // å¦‚果系统字体未找到,尝试从项目资源目录加载(需手动放入字体文件)
        File embeddedFont = new File("fonts/simhei.ttf"); // å‡è®¾å­—体文件放在项目根目录的 fonts/ æ–‡ä»¶å¤¹ä¸‹
        return embeddedFont.exists() ? embeddedFont : null;
    }
    // ç¤ºä¾‹ç”¨æ³•
    public static void main(String[] args) {
        try {
            // 1. ä»Žæ–‡æœ¬æ–‡ä»¶åˆ›å»º PDF
            createPdfFromTextFile("E://新建1.txt", "E://新建1.pdf");
            // 2. è¯»å– PDF å†…容
            String content = readPdfContent("E://新建1.pdf");
            System.out.println("PDF å†…容:\n" + content);
            // 3. å°† PDF è½¬æ¢ä¸ºå›¾ç‰‡
            convertPdfToImages("E://新建1.pdf", "E://", "新建1", "png");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}