| | |
| | | package com.whyc.util; |
| | | |
| | | import com.aspose.words.Document; |
| | | import com.aspose.words.SaveFormat; |
| | | import com.aspose.cad.internal.bouncycastle.util.test.Test; |
| | | import com.aspose.words.Shape; |
| | | import com.aspose.words.*; |
| | | |
| | | import java.awt.*; |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | |
| | | public class Word2PdfAsposeUtil { |
| | | |
| | | /**去除水印专用*/ |
| | | /*public static boolean getLicense() { |
| | | public static boolean getLicense() { |
| | | boolean result = false; |
| | | try { |
| | | InputStream is = Test.class.getClassLoader().getResourceAsStream("\\license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下 |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | }*/ |
| | | } |
| | | |
| | | public static boolean doc2pdf(String inPath, String outPath) { |
| | | /*if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生 |
| | |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | public static boolean doc2pdfWithWaterMark(String inPath, String outPath) { |
| | | if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生 |
| | | return false; |
| | | } |
| | | FileOutputStream os = null; |
| | | try { |
| | | File file = new File(outPath); // 新建一个空白pdf文档 |
| | | os = new FileOutputStream(file); |
| | | Document doc = new Document(inPath); // Address是将要被转化的word文档 |
| | | insertWaterMark(doc,"无效签名"); |
| | | doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return false; |
| | | }finally { |
| | | if (os != null) { |
| | | try { |
| | | os.flush(); |
| | | os.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | private static void insertWaterMark(Document doc, String watermarkText) throws Exception { |
| | | Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT); |
| | | watermark.getTextPath().setText(watermarkText); |
| | | |
| | | //设置水印样式 |
| | | //watermark.getTextPath().setFontFamily("Arial");//设置字体 |
| | | watermark.getTextPath().setFontFamily("宋体");//设置字体 |
| | | watermark.setRotation(-40);//设置偏移角度 |
| | | watermark.setHeight(100);//水印高度 |
| | | watermark.setWidth(200);//水印宽度 |
| | | watermark.setFillColor(Color.pink);//设置填充颜色 |
| | | watermark.setStrokeColor(Color.pink);//设置描边颜色 |
| | | |
| | | //官方api给的解释 太官方了 (只插入一条水印 默认为这些数据) |
| | | watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);//指定相对于水平放置的位置 |
| | | watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);//指定相对于形状垂直定位的位置 |
| | | |
| | | //watermark.setWrapType(WrapType.TOP_BOTTOM);//定义水印的环绕模式 |
| | | watermark.setWrapType(WrapType.NONE);//定义水印的环绕模式 |
| | | watermark.setVerticalAlignment(VerticalAlignment.CENTER);// 指定形状垂直放置的方式 |
| | | watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);// 指定形状如何水平放置 |
| | | |
| | | |
| | | Paragraph watermarkPara = new Paragraph(doc); |
| | | watermarkPara.appendChild(watermark); |
| | | |
| | | /*String text = watermarkPara.getText(); |
| | | System.out.println("++++++++++" + text); |
| | | System.out.println("______________" + watermarkPara); |
| | | NodeCollection childNodes = watermarkPara.getChildNodes(); |
| | | String text1 = childNodes.get(0).getText(); |
| | | System.out.println("111111" + text);*/ |
| | | for (Section section : doc.getSections()) { |
| | | InsertWatermarkIntoHeader(watermarkPara, section, HeaderFooterType.HEADER_PRIMARY); |
| | | InsertWatermarkIntoHeader(watermarkPara, section, HeaderFooterType.HEADER_FIRST); |
| | | InsertWatermarkIntoHeader(watermarkPara, section, HeaderFooterType.HEADER_EVEN); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 将水印插入图像头部 |
| | | * @param watermarkPara |
| | | * @param section |
| | | * @param headerType |
| | | */ |
| | | private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section section, int headerType) throws Exception { |
| | | HeaderFooterCollection headerFooters = section.getHeadersFooters(); |
| | | HeaderFooter footer = headerFooters.getByHeaderFooterType(headerType); |
| | | if (footer == null) { |
| | | footer = new HeaderFooter(section.getDocument(), headerType); |
| | | section.getHeadersFooters().add(footer); |
| | | } |
| | | footer.appendChild(watermarkPara.deepClone(true)); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | doc2pdfWithWaterMark("C:\\Users\\29550\\Desktop\\当前项目\\202207图纸管理\\流程卡和SOP的doc审批注入\\新FBI系列流程卡签字2.doc", |
| | | "C:\\Users\\29550\\Desktop\\当前项目\\202207图纸管理\\流程卡和SOP的doc审批注入\\新FBI系列流程卡签字2-doc.pdf"); |
| | | } |
| | | } |
| | | |