Insert an External File into Word Documents Using Java

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.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("Input 1.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);

//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);




Sharing Java Code

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Sandboxing data crunches, chapter 3: containerize

Agile Testing — Part 1

Translating streams real-time | Tokyo 2020 Olympics

Open Source at Indeed: Heading to OSCON

oscon logo

A Simple Item for Better Software Engineering

Technical chapters at Mindsay

The 4 steps of compilation in C

Handy Excel Keyboard Shortcuts for Windows and Mac

Handy Excel Keyboard Shortcuts for Windows and Mac

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Wilson

Andrew Wilson

Sharing Java Code

More from Medium

XML External Entity Attacks in JAVA:

Exceptions Handling in Java..

HelloWorld in Java