Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to convert word documents to pdf and add watermarks

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use java to achieve word documents to pdf and add watermarks, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

The details are as follows:

Some time ago, the project needs to automatically generate word documents, using WordFreeMarker to generate word documents, and then require the generated documents to be browsed in the browser, thinking about it, the word documents into pdf is good, so I studied it.

Converting word documents into PDF is one of the common requirements in projects. At present, the mainstream methods can be divided into two categories, one is to use a variety of Office applications for conversion, such as Microsoft Office, WPS and LiberOffice, and the other is to use various languages to provide interfaces for reading Office documents (such as Apache POI,jacob,docx4j,openoffice), which either charge, plug-ins are installed, or the style is out of shape after conversion, garbled and so on.

I can export complex WORD PDF HTML multiple data formats using Aspose.Words for Java

Official download address: http://www.aspose.com/java/word-component.aspx

The version I use is aspose-words-14.9.0-jdk16. Need to go to the two jar packages (download the required jar packages. ).

The above code:

Import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import org.slf4j.Logger;import com.aspose.words.Document;import com.aspose.words.SaveFormat;/** @ ClassName: Word2PdfUtilt* @ Description: PDF conversion tool class * @ author mzl* @ date July 30, 2018 5: 00 p.m. 26 Description 44 * * / public class Word2PdfUtilt {protected static Logger log = Log.get () Public static void main (String [] args) {doc2pdf ("D:\\ Workspaces\\ floret test _ 20180731094521.doc", "D:\\ Workspaces\\ test.pdf");} public static void doc2pdf (String inPath, String outPath) {FileOutputStream os = null; try {File file = new File (outPath); / / create a blank pdf document os = new FileOutputStream (file); Document doc = new Document (inPath) / / Address is the word document doc.save (os, SaveFormat.PDF) to be converted;} catch (Exception e) {e.printStackTrace ();} finally {if (ossified null) {try {os.close ();} catch (IOException e) {e.printStackTrace () }

After being converted to PDF format, the pictures and other styles are not messy.

These are the steps for producing PDF. Next, add a watermark:

/ * @ Title: insertWatermarkText* @ Description: PDF generates watermark * @ author mzl* @ param doc* @ param watermarkText* @ throws Exception* @ throws*/private static void insertWatermarkText (Document doc, String watermarkText) throws Exception {Shape watermark = new Shape (doc, ShapeType.TEXT_PLAIN_TEXT); / / watermark content watermark.getTextPath (). SetText (watermarkText); / / Watermark font watermark.getTextPath (). SetFontFamily ("Verdana") / / watermark width watermark.setWidth (500); / / watermark height watermark.setHeight (100); / / rotate watermark watermark.setRotation (- 40); / / watermark color watermark.getFill (). SetColor (Color.lightGray); watermark.setStrokeColor (Color.lightGray); watermark.setRelativeHorizontalPosition (RelativeHorizontalPosition.PAGE); watermark.setRelativeVerticalPosition (RelativeVerticalPosition.PAGE); watermark.setWrapType (WrapType.NONE); watermark.setVerticalAlignment (VerticalAlignment.CENTER) Watermark.setHorizontalAlignment (HorizontalAlignment.CENTER); Paragraph watermarkPara = new Paragraph (doc); watermarkPara.appendChild (watermark); for (Section sect: doc.getSections ()) {insertWatermarkIntoHeader (watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY); insertWatermarkIntoHeader (watermarkPara, sect, HeaderFooterType.HEADER_FIRST); insertWatermarkIntoHeader (watermarkPara, sect, HeaderFooterType.HEADER_EVEN);} System.out.println ("Watermark Set") } private static void insertWatermarkIntoHeader (Paragraph watermarkPara, Section sect, int headerType) throws Exception {HeaderFooter header = sect.getHeadersFooters (). GetByHeaderFooterType (headerType); if (header = = null) {header = new HeaderFooter (sect.getDocument (), headerType); sect.getHeadersFooters (). Add (header);} header.appendChild (watermarkPara.deepClone (true)) } Thank you for reading this article carefully. I hope the article "how to use java to convert word documents to pdf and add watermarks" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report