java aspose例子_在Java中使用Aspose对文档操作示例

本文介绍了如何在Java中使用Aspose处理文件,包括设置版权、将Word转换为PDF、Excel转PDF,以及详细展示了文档拼接的方法。Aspose是一个强大的商业库,支持多种文件格式的操作。
摘要由CSDN通过智能技术生成

Aspose简介

Aspose是一个商业.NET类库,可以使得应用程序处理大量的文件任务。Aspose可以支持Doc,Docx,PDF,Excel 等格式的文件处理。我们可以通过使用Aspose生成、修改、转换和打印文档。

Aspose并非开源,所以在使用的时候需要获取版权,否则在操作文档中会显示版权相关信息的水印。

版权注册代码如下

/**

* @Description TODO

* @param licensePath 版权文件所在路径

* @Return void

* @Author Mr.Walloce

* @Date 2019/7/27 22:18

*/

private static void setLicense(String licensePath) {

try {

InputStream inputStream = FileCustUtil.class.getClassLoader().getResourceAsStream(licensePath);

License aposeLicense = new License();

aposeLicense.setLicense(inputStream);

} catch (Exception e) {

e.printStackTrace();

}

}

文档转换 -- word转PDF

/**

* @Description TODO

* @param inputPath 需要被转换的word全路径带文件名

* @param outPath 转换之后pdf的全路径带文件名

* @Return void

* @Author Mr.Walloce

* @Date 2019/7/27 13:47

*/

public static void docToPdf(String inputPath, String outPath) {

try {

//word文档

Document doc = new Document(inputPath);

//新建一个pdf文档

File file = new File(outPath);

FileOutputStream os = new FileOutputStream(file);

//保存为pdf文件,saveFormat取的是words包下的,值为:40

doc.save(os, com.aspose.words.SaveFormat.PDF);

os.close();

} catch (Exception e) {

e.printStackTrace();

}

}

word文档转换为PDF文档时,是将word中的内容完全转换为PDF文档,包括word中的图片、表格等特殊的数据。

文档转换 -- excel转PDF

/**

* @Description TODO

* @param inputPath 需要被转换的excel全路径带文件名

* @param outPath 转换之后pdf的全路径带文件名

* @Return void

* @Author Mr.Walloce

* @Date 2019/7/27 13:48

*/

public static void excelToPdf(String inputPath, String outPath) {

/*if (!getLicense()) {

return;

}*/

try {

//Excel文件数据

Workbook wb = new Workbook(inputPath);

FileOutputStream fileOS = new FileOutputStream(new File(outPath));

//保存为pdf文件,saveFormat取的是cells包下的,值为:13

wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);

fileOS.close();

} catch (Exception e) {

e.printStackTrace();

}

}

Excel表格数据转换为PDF数据时,有多个sheet时会自动按sheet的顺序将数据填充到PDF文档中。每个sheet都会启动一个新的页,按顺序拼接在后面。

word文档拼接

/**

* @Description TODO 文档拼接

* @param mainDoc 主文档

* @param addDoc 要拼接的文档

* @param isPortrait 是否横向拼接

* @Return com.aspose.words.Document 返回拼接后新的文档

* @Author Mr.Walloce

* @Date 2019/7/27 18:31

*/

public static Document appendDocument(Document mainDoc, Document addDoc, boolean isPortrait) {

String bookmark = "单位信息";

DocumentBuilder builder = null;

try {

builder = new DocumentBuilder(mainDoc);

BookmarkCollection bms = mainDoc.getRange().getBookmarks();

Bookmark bm = bms.get(bookmark);

if (bm != null) {

builder.moveToBookmark(bookmark, true, false);

builder.writeln();

builder.getPageSetup().setPaperSize(PaperSize.A4);

Node insertAfterNode = builder.getCurrentParagraph().getPreviousSibling();

insertDocumentAfterNode(insertAfterNode, mainDoc, addDoc);

}

//builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

if (isPortrait) {

// 纵向纸张

builder.getPageSetup().setOrientation(Orientation.PORTRAIT);

//builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

} else {

// 横向

builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);

}

builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

// builder.insertBreak(BreakType.PAGE_BREAK);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return mainDoc;

}

/**

* @Description 向书签后插入文档

* @param mainDoc 主文档

* @param tobeInserted 拼接的文档

* @param bookmark 书签

* @Return com.aspose.words.Document

* @Author Mr.Walloce

* @Date 2019/7/27 18:33

*/

private static Document insertDocumentAfterBookMark(Document mainDoc, Document tobeInserted, String bookmark)

throws Exception {

if (mainDoc == null) {

return null;

} else if (tobeInserted == null) {

return mainDoc;

} else {

//构建新文档

DocumentBuilder mainDocBuilder = new DocumentBuilder(mainDoc);

if (bookmark != null && bookmark.length() > 0) {

//获取到书签

BookmarkCollection bms = mainDoc.getRange().getBookmarks();

Bookmark bm = bms.get(bookmark);

if (bm != null) {

mainDocBuilder.moveToBookmark(bookmark, true, false);

mainDocBuilder.writeln();

//获取到插入的位置

Node insertAfterNode = mainDocBuilder.getCurrentParagraph().getPreviousSibling();

insertDocumentAfterNode(insertAfterNode, mainDoc, tobeInserted);

}

} else {

appendDoc(mainDoc, tobeInserted, true);

}

return mainDoc;

}

}

/**

* @Description TODO

* @param insertAfterNode 插入的位置

* @param mainDoc

* @param srcDoc

* @Return void

* @Author Mr.Walloce

* @Date 2019/7/27 14:51

*/

private static void insertDocumentAfterNode(Node insertAfterNode, Document mainDoc, Document srcDoc)

throws Exception {

if (insertAfterNode.getNodeType() != 8 & insertAfterNode.getNodeType() != 5) {

throw new Exception("The destination node should be either a paragraph or table.");

} else {

CompositeNode dstStory = insertAfterNode.getParentNode();

while (null != srcDoc.getLastSection().getBody().getLastParagraph()

&& !srcDoc.getLastSection().getBody().getLastParagraph().hasChildNodes()) {

srcDoc.getLastSection().getBody().getLastParagraph().remove();

}

NodeImporter importer = new NodeImporter(srcDoc, mainDoc, 1);

int sectCount = srcDoc.getSections().getCount();

for (int sectIndex = 0; sectIndex < sectCount; ++sectIndex) {

Section srcSection = srcDoc.getSections().get(sectIndex);

int nodeCount = srcSection.getBody().getChildNodes().getCount();

for (int nodeIndex = 0; nodeIndex < nodeCount; ++nodeIndex) {

Node srcNode = srcSection.getBody().getChildNodes().get(nodeIndex);

Node newNode = importer.importNode(srcNode, true);

dstStory.insertAfter(newNode, insertAfterNode);

insertAfterNode = newNode;

}

}

}

}

/**

* @Description 文档拼接

* @param dstDoc

* @param srcDoc

* @param includeSection

* @Return void

* @Author Mr.Walloce

* @Date 2019/7/27 14:53

*/

private static void appendDoc(Document dstDoc, Document srcDoc, boolean includeSection) throws Exception {

if (includeSection) {

Iterator var3 = srcDoc.getSections().iterator();

while (var3.hasNext()) {

Section srcSection = (Section) var3.next();

Node dstNode = dstDoc.importNode(srcSection, true, 0);

dstDoc.appendChild(dstNode);

}

} else {

Node node = dstDoc.getLastSection().getBody().getLastParagraph();

if (node == null) {

node = new Paragraph(srcDoc);

dstDoc.getLastSection().getBody().appendChild(node);

}

if (node.getNodeType() != 8 & node.getNodeType() != 5) {

throw new Exception("Use appendDoc(dstDoc, srcDoc, true) instead of appendDoc(dstDoc, srcDoc, false)");

}

insertDocumentAfterNode(node, dstDoc, srcDoc);

}

}

文档拼接主要是用于业务在不同地方生成的文档根据需求拼接成一个完整的主文档。

以上仅为学习内容,如内容有错希望提醒指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值