aspose.word.pdf操作实例

本文介绍了使用Aspose库进行Word文档操作,包括插入文本、图片,以及将PDF转换为图片和HTML。示例代码详细展示了如何合并Word文档,插入分页符、标题和图片,以及如何利用Aspose将PDF转换成多个高分辨率的JPEG图片并插入到Word中。此外,还演示了PDF和Word之间的相互转换方法。
摘要由CSDN通过智能技术生成

aspose操作实例,合成word,插入word,图片,文字,PDF转图片

aspose操作方法

WROD.PDF转换图片

具体的操作都在这个下边了,可能写的比较粗糙一些,这个是简单入门告诉你简单的思路一般情况应该是够用,不够用在补充。
https://www.aspose.com/ <-官网地址
这里需要加的依赖,如果maven依赖下载不来的话,就导入aspose的远程仓库
这里提供jar包,需要的话你就联系我留言我发给你也行!
我在这里设置0积分下载的地址,你们可以试试
在这里插入图片描述

		<!-- 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);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值