Java converts Word to picture perfect solution (free, lossless and garbled)

This tutorial is reproduced from GitHub, and the library files used will be available for download for free!
Reprint address: https://github.com/conghaoyuan/Word2Image

Advantages of this solution:

  • Free (you can use Spire.Office, this is the fastest solution, but the cost of a developer of around 16,188 yuan a year is not low)
  • Lossless/simple (directly call Office official API, call Office for conversion, simple code can be done)

Disadvantages of this solution:

  • Can only be used in Windows environment
  • Office needs to be installed on the server

Conversion idea:
first convert the Word document into a PDF document, and then convert the PDF document into an image.

Jar used and its download:
click to download (do not use points)

1. First install the Office suite of Windows, and copy the Dll file in the Jacob compression package to the C:\Windows\System32 directory
Insert picture description here
2. Add the Jar package in the file to the project to
Insert picture description here
introduce the tool code: pdf2Bmp, word2Pdf

click to download

First convert Word to PDF file:

		 word2Pdf(String wordPath,String pdfPath)

wrodPath is the path of the Word file, and pdfPath is the path of the generated pdf file, which needs to be specific to the file.

Then convert the PDF file to an image:

		 pdf2Bmp(String pdfPath,String imgName,String imgPath)

pdfPath is the path of the pdf file, which needs to be specific to the file, imgName is the name of the generated file as an identifier, and imgPath is the path of the picture file, because multiple pictures will be generated according to the page number, just go to the path

Finally, a picture of imgName_页号.jpg will be generated under the imgPath folder.

You can change the generation logic according to your needs:
Insert picture description here

Finally, the test found that complex documents can be converted perfectly, generally without garbled characters, each picture size is about 500k~

Guess you like

Origin blog.csdn.net/qq_42628989/article/details/106565357