Convert Word and PDF to pictures Java
The document content needs to be displayed on the mini-program terminal, so each page of the document is converted into a picture and displayed.
References and other solutions:
Three ways to implement PDF to picture in Java
Detailed explanation of Java implementation of word document conversion into pictures
Convert Word to Picture
<!-- wordTransfer tool --> <dependency> <groupId></groupId> <artifactId>poi-tl</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId></groupId> <artifactId>aspose-words</artifactId> <version>23.1</version> </dependency>
import ; import ; import .slf4j.Slf4j; import ; import ; import ; import ; @Slf4j public class WordToImageUtil { public static void main(String[] args) { wordToImage("C:\\Users\\Administrator\\Documents\\Enter script\\324\\", ""); } public static List<String> wordToImage(String filePath, String fileName) { ("Start word to picture"); List<String> resultList = new ArrayList<String>(); // Get file path separator String separator = ; try { // After filling the data, it is converted into a picture File file1 = new File(filePath + separator + fileName); // Use the system's path separator // Open the generated Word file Document doc = new Document((())); // Save Word files as pictures page by page (PNG format) for (int i = 0; i < (); i++) { Document extractedPage = (i, 1); // Splice the file name String path = filePath + separator + "tmpImg" + separator + removeFileExtension(fileName) + separator + "img" + () + "_" + i + ".png"; // Use the system's path separator // Create a directory (if it does not exist) File tmpDir = new File(filePath + separator + "tmpImg" + separator + removeFileExtension(fileName)); if (!()) { (); // Create a directory } // Save Word files as PNG format (path, ); (path); } } catch (Exception e) { (); return new ArrayList<>(); } ("End Word to Picture"); return resultList; } /** * Remove the suffix name * @param fileName * @return */ public static String removeFileExtension(String fileName) { int dotIndex = ("."); if (dotIndex == -1) { return fileName; // If no point is found, return the original file name } return (0, dotIndex); // Return the file name with the suffix name removed } }
Convert PDF to image
<!--PDFConvert to picture--> <dependency> <groupId></groupId> <artifactId>fontbox</artifactId> <version>2.0.9</version> </dependency> <!-- /artifact//pdfbox --> <dependency> <groupId></groupId> <artifactId>pdfbox</artifactId> <version>2.0.9</version> </dependency> <!-- /artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency>
import ; import ; import ; import ; import ; import ; public class PdfToImage { /** * Use pdfbox to convert the entire pdf into an image * * @param fileAddress File address For example: C:\\Users\\user\\Desktop\\test * @param filename PDF filename without suffix name * @param type Image types png and jpg */ public static void pdf2png(String fileAddress, String filename, String type) { long startTime = (); // Splice the file address and file name into a path. Note: Online environment cannot use \\ splicing File file = new File(fileAddress + "\\" + filename + ".pdf"); try { // Write to the file PDDocument doc = (file); PDFRenderer renderer = new PDFRenderer(doc); int pageCount = (); for (int i = 0; i < pageCount; i++) { // dpi is 144, the higher the clearer the more you are, the slower the conversion BufferedImage image = (i, 144); // Windows native DPI // Write the picture to this path (image, type, new File(fileAddress + "\\" + filename + "_" + (i + 1) + "." + type)); } long endTime = (); ("Total time spent:" + ((endTime - startTime) / 1000.0) + "Second"); //Conversion time } catch (IOException e) { (); } } public static void main(String[] args) { pdf2png("C:\\Users\\Administrator\\Documents\\xxx\\pdf", "file name", "png"); } }
Garbage code problem
If you encounter Chinese garbled code when running on Linux, you can try installing the font and restarting the program to run
refer to:
existCentOSInstall Chinese fonts in the system
- WillWindowsCopy the font file under the system to the CentOS system and modify the font file name. The general font location is: /usr/share/fonts
- Install the fontconfig package, which is a tool for handling font configuration and caching. You can use the yum command to install, for example: yum -y install fontconfig (see if it already exists)
- Refresh the font cache so that the newly installed font can be recognized by the system. Use the fc-cache command to complete this step.
- Use the fc-list command to view the installed font list in the system
- Search for "simmun" through grep command to verify whether the font is installed successfully
1. Find the font from the local area: C:\Windows\Fonts
2. Copy to Linux, I use centos here, copy the font to /usr/share/fonts
3. Install the fontconfig tool yum -y install fontconfig
4. Refresh font cache fc-cache
5. Check whether the font list is installed fc-list
The above is a detailed explanation of the method of converting Word and PDF into pictures in Java. For more information about Java Word PDF to pictures, please follow my other related articles!