aspose-words简易操作word文件转成pdf_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > aspose-words简易操作word文件转成pdf

aspose-words简易操作word文件转成pdf

 2022/3/20 12:27:37  xiaojundream  程序员俱乐部  我要评论(0)
  • 摘要:importcom.aspose.words.*;importcom.deepoove.poi.XWPFTemplate;importcom.deepoove.poi.data.PictureRenderData;importcom.deepoove.poi.data.TextRenderData;importcom.deepoove.poi.data.style.Style;importcom.deepoove.poi.util.BytePictureUtils
  • 标签:文件 操作
import com.aspose.words.*;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;
import com.deepoove.poi.util.BytePictureUtils;

public static void main(String[] args) {
        try {
            String str = "/Users/xiaojundream/Desktop/general.docx";
            //XWPFTemplate 模版渲染  word 模板 
            XWPFTemplate template = XWPFTemplate.compile(str)
                    .render(initWordMap());


            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            template.write(baos);

            byte[] bytes = baos.toByteArray();

            baos.close();
            Document doc = new Document(new ByteArrayInputStream(bytes));
           
            //添加 wps fonts字体  转换成PDF
            FontSourceBase[] fontSourceBases = FontSettings.getDefaultInstance().getFontsSources();
            ArrayList<FontSourceBase> fontSources = new ArrayList<>();
            for (int var3 = 0; var3 < fontSourceBases.length; ++var3) {
                fontSources.add(fontSourceBases[var3]);
            }
            FolderFontSource folderFontSource = new FolderFontSource("/Users/xiaojundream/Desktop".concat("/fonts"), true);
            fontSources.add(folderFontSource);
            FontSourceBase[] fonts = new FontSourceBase[fontSources.size()];

            FontSettings.getDefaultInstance().setFontsSources(fontSources.toArray(fonts));
            //将改好的 docx转成PDF
            doc.save("/Users/xiaojundream/Desktop/222.pdf",SaveFormat.PDF);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

//world模版操作
    private static Map<String, Object> initWordMap() {

        Map<String, Object> wordData = new HashMap<String, Object>();
       //checkbox :o->未打勾; R->打勾
        wordData.put("c".concat("006").concat("box1"), new TextRenderData("o",new com.deepoove.poi.data.style.Style("Wingdings",10)));
        wordData.put("c".concat("006").concat("box2"), new TextRenderData("R",new com.deepoove.poi.data.style.Style("Wingdings 2",10)));
//
        //普通的变量替换
        wordData.put("memberName", "张三");

        wordData.put("signatureOwnerDate", wordData.put("signatureDate", "____年__月__日"));
        //用于word签字  图片插入
        wordData.put("rentImage", new PictureRenderData(200, 50, ".png", BytePictureUtils.getUrlByteArray("https://xxx.com/9e728e4e-59f9-477a-a21c-442a4a3a0b58.png")));
        return wordData;
    }
发表评论
用户名: 匿名