springboot operation pdf (1) word to pdf


foreword

The pdf file is a file format commonly used in the market today. Its file format and content are not easy to change during transmission, so it is used by many users. But how to generate pdf in the process of java development is a problem that we need to solve.


1. What is PDF?

insert image description here
PDF is an e-book file, generally opened with Ford Reader/WPS Office.

PDF is the abbreviation of Portable Document Format (Portable Document Format). It is an electronic file format, which has nothing to do with the operating system platform and is developed by Adobe. PDF files are based on the PostScript language image model, which can guarantee accurate colors and accurate printing effects no matter what kind of printer it is on. PDF will faithfully reproduce every character, color and image of the original. PDF files are universal whether they are on Windows, Unix or Apple's Mac OS operating system.

This performance makes it an ideal document format for electronic document distribution and digital information dissemination on the Internet. More and more e-books, product descriptions, company announcements, network materials, and e-mails start to use PDF format files.

PDF is mainly composed of three technologies:

· Derived from PostScript, it can be said to be a reduced version of PostScript;

· Font embedding system, which enables the font to be transmitted together with the file;

· Data compression and transmission system.

PDF files use industry-standard compression algorithms and are usually smaller than PostScript files, making them easy to transfer and store. It is also page-independent, a PDF file contains one or more "pages", each page can be processed independently, especially suitable for the work of multi-processor systems. In addition, a PDF file also includes the PDF format version used in the file, as well as positioning information of some important structures in the file. It is precisely because of the various advantages of PDF files that it has gradually become the new favorite in the publishing industry.

With PDF file technology as the core, Adobe provides a complete set of electronic and network publishing solutions, including commercial software Acrobat for generating and reading PDF files and Illustrator for editing and making PDF files. Adobe also provides font packs needed to read and print Asian scripts, namely Chinese, Japanese, and Korean scripts.

The purpose of Adobe's design of the PDF file format is to support cross-platform, multimedia-integrated information publication and distribution, especially to provide support for network information distribution. In order to achieve this purpose, PDF has many advantages that other electronic document formats cannot compare with. The PDF file format can encapsulate text, fonts, formats, colors, and graphic images independent of devices and resolutions in one file. Files in this format can also contain electronic information such as hypertext links, sounds, and dynamic images. It supports extra-long files, and has a high degree of integration, security and reliability.

Two, the solution

1. Solution 1: Use POI tools to convert word files into pdf

1.1 Import dependencies

<dependency>
	<groupId>fr.opensagres.xdocreport</groupId>
	<artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
	<version>2.0.1</version>
</dependency>

1.2 The code is as follows (example)

public static void wordToPdf(String wordPath, String pdfPath) {
    
    
    try {
    
    

        FileInputStream fileInputStream = new FileInputStream(wordPath);
        XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
        PdfOptions pdfOptions = PdfOptions.create();
        FileOutputStream fileOutputStream = new FileOutputStream(pdfPath);
        PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
        fileInputStream.close();
        fileOutputStream.close();
    } catch (FileNotFoundException e) {
    
    
        throw new RuntimeException(e);
    } catch (IOException e) {
    
    
        throw new RuntimeException(e);
    }
}

1.3 Disadvantages

The generated pdf file will appear, there are some inconsistencies in the format, but you can use it if you don’t mind

2. Use spire.doc.free to convert the doc file to pdf

2.1 Import dependencies

<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.doc.free</artifactId>
    <version>5.2.0</version>
</dependency>

2.2 The code is as follows (example)

public static void wordToPdf2(String wordPath, String pdfPath) {
    
    
    //实例化Document类的对象
    Document doc = new Document();
    //加载Word
    doc.loadFromFile(wordPath);
    //保存为PDF格式
    doc.saveToFile(pdfPath,FileFormat.PDF);
}

2.3 Disadvantages

Fees are charged, only the first three pages of pdf can be converted for free. For small data requirements, it can be satisfied. Comparing the generated pdf file format with the original word file, there is no change, so it is strongly recommended to use


Summarize

To convert word to pdf, I strongly recommend that you can use the second method to convert word files into pdf files. If you want to know more about how to use it, or get java code, please pay attention to the official account.

Welcome to pay attention to the public account 'CV Algorithm House'

Guess you like

Origin blog.csdn.net/weixin_43228814/article/details/129776270