aspose操作实例,合成word,插入word,图片,文字,PDF转图片
- aspose操作方法
- WROD.PDF转换图片
- WROD.PDF转换成html文件
aspose操作方法
WROD.PDF转换图片
具体的操作都在这个下边了,可能写的比较粗糙一些,这个是简单入门告诉你简单的思路一般情况应该是够用,不够用在补充。https://www.aspose.com/ <-官网地址这里需要加的依赖,如果maven依赖下载不来的话,就导入aspose的远程仓库这里提供jar包,需要的话你就联系我留言我发给你也行!
<!-- word开发包 -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>20.7.0</version>
</dependency>
<!-- pdf开发包 -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>20.3</version>
</dependency>
这里是代码的具体实现可以参考一下!不得不说功能确实挺强大的。
/*这里是导入的包*/
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.poi.hssf.record.PageBreakRecord.Break;
import com.aspose.pdf.devices.JpegDevice;
import com.aspose.pdf.devices.Resolution;
import com.aspose.words.ControlChar;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.Font;
import com.aspose.words.ImportFormatMode;
import com.aspose.words.Run;
//这里new一个空的document
Document doc = new Document();
DocumentBuilder docBuider = new DocumentBuilder(doc);
//首先来一个插入word文档到doc中,在这个过程中会有一个问题就是插入的文档位置是在
//文档的最开头部分,这个位置不对所以这里在插入到一个文档之前应该先来一个
//移动到末尾的操作
docBuider.moveToDocumentEnd();
//新建分页符 插入一次就调用一次分页符操作
Run pageBreakRen = new Run(doc,ControlChar.PAGE_BREAK);
docBuider.insertNode(pageBreakRen);
//插入标题设置居中或者靠左靠右
docBuider.insertHtml("<h1 style='text-align:center;font-family:Simsun;'>"
+ "一级标题" +"</h1>");
//这里设置write写入的字体样式
Font font = docBuider.getFont();
font.setSize(16);
font.setBold(true);
font.setColor(Color.BLACK);
font.setName("abc");
//这里才是写入的内容
docBuider.write("测试标题");
//插入word文档到doc中
Document doc2 = new Document("D:\\2.docx");
docBuider.insertDocument(doc2,ImportFormatMode.KEEP_DIFFERENT_STYLES );
//直接导入图片
docBuider.insertImage("D:/abc.jpg");
//接下来将调用pdf转换成图片然后把图片地址获取到存入word中
//这里需要注意的就是这个document跟上边word的那个document引入的包不一样
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(pdfurl);
//图片宽度:800
//图片高度:100
// 分辨率 130
//Quality [0-100] 最大100
//例: new JpegDevice(800, 1000, resolution, 90);
Resolution resolution = new Resolution(130);
JpegDevice jpegDevice = new JpegDevice(resolution);
for (int index=1;index<=pdfDocument.getPages().size();index++) {
//设置转换成图片的地址
String fileurl = "D:/filing/word/"
+StrUtils.getRTimeString()+"-"
+index+".jpg";
File file = new File(fileurl);// 输出路径
FileOutputStream fileOS = new FileOutputStream(file);
//转换特定页面并将图像保存到流
jpegDevice.process(pdfDocument.getPages().get_Item(index), fileOS);
fileOS.close();
//这里就是循环一次插入的操作的,如果需要提出来的话可以用添加到list在进行加入
docBuider.insertImage(fileurl );
}
WROD.PDF转换成html文件
//PDF转换方法
// load the document
Document pdoc = new Document(dir + "template.pdf");
// Convert to DOCX, HTML and PPTX
pdoc.save(dir + "output.docx", SaveFormat.DocX);
pdoc.save(dir + "output.pptx", SaveFormat.Pptx);
pdoc.save(dir + "output.html", SaveFormat.Html);
//WORD转换方法
// Load DOC file to be converted
Document wpd = new Document(dir + "template.doc");
// Convert DOC to DOCX
wpd.save(dir + "output.docx", SaveFormat.DOCX);
// Load HTML file to be converted
Document wpd = new Document(dir + "template.html");
wpd.save(dir + "output.docx", SaveFormat.DOCX);
// Load RTF file to be converted
Document wpd = new Document(dir + "template.rtf");
// Convert RTF to PDF
wpd.save(dir + "output.pdf", SaveFormat.PDF);