Insert PDF into Word Document using C#

This article explains how to insert PDF into Word document using C#. You will get details to set up the development environment, step-by-step instructions, and sample code showing how to insert a PDF into Word using C#. You will also get suggestions to enhance the sample code for attaching other file types and changing the icon of the embedded file.

Steps to Add a PDF into Word using C#

  1. Set the environment to use Aspose.Words for .NET to embed a PDF in a Word file
  2. Load the target Word file using the Document object
  3. Create a DocumentBuilder object and write some text before the attached PDF
  4. Open the source PDF file as a stream
  5. Embed the PDF stream as an icon with a caption using the InsertOleObjectAsIcon() method
  6. Set the file name and display name of the embedded PDF package
  7. Save the output Word file with the attached PDF

These steps describe how to embed a PDF in Word using C#. Load the target Word file, write some text using the DocumentBuilder object, and open the source PDF file as a stream. Pass this stream to the InsertOleObjectAsIcon() method to embed the PDF as an icon with a caption, and set the file name and display name of the package before saving the output Word file.

Code to Embed a PDF in Word using C#

This code demonstrates how to attach a PDF file to a Word document using C#. You can move the cursor to a specific position using the MoveToBookmark() or MoveToCell() method before embedding the PDF. Pass the path of an ICO file as the third argument of the InsertOleObjectAsIcon() method to show a custom icon instead of the default one. Similarly, you can embed other files such as XLSX, PPTX, ZIP, or DOCX using the same code.

This article has taught us to insert a PDF into a Word file as an attachment. To remove bookmarks in a Word file, refer to the article Remove Bookmark in Word using C#.