¶Ô±ÈÐÂÎļþ |
| | |
| | | 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(); |
| | | } |
| | | } |
| | | } |