文档星空最火知名网站>>Spire.Doc系列教程>>Spire.Doc功能演示:将外部文件插入Java中的Word文档
Spire.Doc功能演示:将外部文件插入Java中的Word文档
Spire.Doc for Java 是一款专业的Java Word组件,开发人员使用它可以轻松地将Word文档创建、读取、编辑、转换和打印等功能集成到自己的Java应用程序中。
Spire.Doc for Java支持将外部文件(Word,Excel,PowerPoint,PDF,图片,视频等)作为OLE对象嵌入到Word文档中。本文为您提供了一个如何将PDF文件插入Word文档的示例。可点击此处下载最新版测试。
示例代码如下:
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.OleObjectType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocOleObject;
import com.spire.doc.fields.DocPicture;
public class InsertOLE {
public static void main(String[] args) {
//Create a Document object and load a Word document
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\source.docx");
//Get the last section
Section section = doc.getLastSection();
//Add a paragraph
Paragraph par = section.addParagraph();
//Load an image which will be inserted to Word document representing the embedded file
DocPicture pdfIcon = new DocPicture(doc);
pdfIcon.loadImage("C:\\Users\\Administrator\\Desktop\\pdf-icon.jpg");
//Insert a PDF file to the Word document as an OLE object
par.appendOleObject("C:\\Users\\Administrator\\Desktop\\report.pdf", pdfIcon, OleObjectType.Adobe_Acrobat_Document);
//Save to another file
doc.saveToFile("EmbedDocument.docx", FileFormat.Docx_2013);
}
}
让人兴奋的是整合所有格式API处理套包Spire.office for Java正在慧都网火热销售中!立马1分钟了解全部咨询!

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线