How can you insert a PDF file into a word document

I want to import a PDF document in a word document.

can this be done with InsertOleObject
if so how.

Hi there,

Thanks for your inquiry. Yes, you can insert Pdf file into Word document using Aspose.Words. Please read the detail of DocumentBuilder.InsertOleObject methods from here:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/insertoleobject/
https://reference.aspose.com/words/net/aspose.words/documentbuilder/insertoleobject/

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertOleObject(MyDir + "in.pdf", false, false, null);
doc.Save(MyDir + "Out.docx");

That does not show the PDF document it shows the Aspose Logo. (see printscreen)

What I am looking for is what to fill in on the question marks.

Shape oleObjects = builder.InsertOleObject(memoryStream, "???", true, null);

Hi there,

Thanks for your inquiry.
Please check the parameter detail of DocumentBuilder.InsertOleObject method from
here:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/insertoleobject/

Please use the following code example to achieve your requirements.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
MemoryStream stream = new MemoryStream(File.ReadAllBytes(MyDir + "in.pdf"));
builder.InsertOleObject(stream, "AcroExch.Document.7", true, null);
doc.Save(MyDir + "Out.docx");

That solves one problem.

but now the customer wants to see the PDF content as well.

If in Word I use insert Object with the option Create as File.

it shows the content of the PDF in the word document.

can this been done using Aspose?

Hi there,

Thanks for your inquiry.
Please check last parameter of InsertOleObject method. This is image presentation of OLE object. If value is null Aspose.Words will use one of the predefined images.

You can use Aspose.Pdf to convert the Pdf to image and use that image in InsertOleObject method. Please read about converting Pdf pages into images from here:
https://docs.aspose.com/pdf/net/converting/

Please use the following code example. Hope this helps you.

// Open document
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(MyDir + "in.pdf");
MemoryStream imageStream = new MemoryStream();
// Create Resolution object
Aspose.Pdf.Devices.Resolution resolution = new Aspose.Pdf.Devices.Resolution(300);
// Create JPEG device with specified attributes (Width, Height, Resolution, Quality)
// where Quality [0-100], 100 is Maximum
Aspose.Pdf.Devices.JpegDevice jpegDevice = new Aspose.Pdf.Devices.JpegDevice(400, 600, resolution, 100);
// Convert a particular page and save the image to stream
jpegDevice.Process(pdfDocument.Pages[1], imageStream);
Image image = Image.FromStream(imageStream);
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
MemoryStream stream = new MemoryStream(File.ReadAllBytes(MyDir + "in.pdf"));
Shape shape = builder.InsertOleObject(stream, "AcroExch.Document.7", true, image);
doc.Save(MyDir + "Out.docx");
// Close stream
imageStream.Close();

I got error confused Aspose.pdf vs system.drawing on the word Image.

Dim image As Image = New Image.FromStream(imageStream)

if I add the qualifier to Image, I got error Image.FromStream not defined

Dim image As Aspose.Pdf.Image = New Image.FromStream(imageStream)

Any help appreciated.

Hi there,

Thanks for your inquiry. Please use System.Drawing.Image as shown in following line of code to fix this issue.

Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(imageStream)

I did as suggested

Dim image As System.Drawing.Image = New System.Drawing.Image.FromStream(imageStream)

I got Type ‘System.Drawing.Image.FromStream’ is not defined

Hi there,

Thanks for your inquiry. Please include the reference of System.Drawing.dll in your application to fix this issue. You can find it under .NET Framework folder. E.g. its path at my end is “C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Drawing.dll”.