Attach PDF to word document

Hi,

I am exploring different ways to attach/add PDF to the end of a word document. So far, one of the option I tried is to embed the PDF as an OLE object in the word document. Following is the code I tried :

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertOleObject("sample.pdf",true,true,null);
string dataDir = "output_1.pdf";
doc.Save(dataDir);

Above code embed the PDF in the word document but How can I render the attached PDF during print or save ?

In other words, If I print/save the final “output_1.pdf” file, I want to see the contents of the attached/linked PDF.

I am attaching the sample.pdf

Hi Imran,

Thanks for your inquiry. Yes, you can embed your PDF document as OLE object using Aspose.Words. However, to view the PDF document you should extract your embedded PDF document from Word document using following code of Aspose.Words and later can use Aspose.Pdf. Please note Aspose.Pdf is a class library, it does not provide any control to view PDF. However, you may render PDF document to Image and display it in canvas object. We have a showcase project HTML5 PDF Editor using this approach. Please check the PDF Editor Getting Started page from our API documentation for details. You can download the project sample code and customize it as per your requirements.

Furthermore, you may also check GroupDocs online document viewer, a product of our sister concern supports view and edit PDF document in browser. You may contact GroupDocs support to get any further details of this API.

Document doc = new Document("Sample.docx");
int i = 0;
// Get collection of shapes
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
Console.Out.WriteLine("No. of Shapes:" + shapes.Count);
// Loop through all shapes
foreach (Aspose.Words.Drawing.Shape shape in shapes)
{
    if (shape.OleFormat != null)
    {
        Console.Out.WriteLine(shape.OleFormat.SuggestedExtension);
        shape.OleFormat.Save(String.Format("E:\\Data\\out_{0}.{1}", i++, shape.OleFormat.SuggestedExtension));
    }
}

Please feel free to contact us for any further assistance.

Best Regards,

Thanks for the quick reply.

Actually, my project is not web based. I think, I did not explain my problem well. Suppose I embedded “sample.pdf” in the word document that has only title page. So is it possible that when I print\save\convert this document. It should print\save in this order

  • Title Page ( From Word document )
  • Contents of “sample.pdf” (embedded in the doc)

Please let me know
Thanks
Imran

Hi Imran,

Thanks for sharing additional information. You can achieve your requirement with collaboration of Aspose.Words and Aspose.Pdf.

Please feel free to contact us for any further assistance.

Best Regards,

How about if my word document is in this pattern:

 [ SOME TEXT ]
 [ EMBEDDED PDF ]
 [ SOME TEXT ]
 [ SOME TEXT ]
 [ EMBEDDED PDF ]
 [ SOME TEXT ]

( There is line feed/carriage return between each item )

How can I render the PDF content at that specific location in the word document ?

Hi Imran,

Thanks for your inquiry. We will appreciate it if you please share your sample input and expected output documents here. It will help us to understand your requirement exactly and guide you accordingly.

Best Regards,

Hi Tilal,

So, I have this “Initial_Document.docx” document. I have embedded the “sample.pdf” on the second page of the initial docx file and get “After_Embedding_PDF.docx” file.

Is there a way to convert this docx to PDF in order to get the “Desired_Output.pdf” ?

Thanks

Imran

Hi Imran,

Thanks for your inquiry. Please check following documentation links and suggested solution. Hopefully it will help you to accomplish the task.

In your case, we suggest you following solution.

  1. Insert Bookmarks without contents at the start and end of your different text areas.

  2. Extract embedded PDF files from your document.

  3. Extract the contents between bookmarks.

  4. Generate the document from extracted nodes(contents) and convert to PDF.

  5. Concatenate your PDF documents (using Aspose.Pdf as suggested above) to get final PDF document.

    Bkstart_1

    [ SOME TEXT ]
    Bkend_1
    [ EMBEDDED PDF ]
    Bkstart_2
    [ SOME TEXT ]

    [ SOME TEXT ]

    Bkend_2
    [ EMBEDDED PDF ]
    Bkstart_3
    [ SOME TEXT ]

    Bkend_3

Best Regards,

About the last step:

  1. Concatenate your PDF documents (using Aspose.Pdf as suggested above) to get final PDF document.

Is it possible to insert PDF pages to an existing PDF file ?

Also should it be inserted at the last or is it possible to insert between pages ?

I saw this article on your website about inserting pages into PDF file.

https://docs.aspose.com/pdf/net/insert-pdf-pages/

Is there a way to insert all the pages from a pdf file into a PDF file ?

Thanks

Hi Imran,

Thanks for your inquiry. Yes, you can follow the article to insert specific pages from a PDF document to other PDF document.

Best Regards,

Thank you !

Let me try your suggestions. I will let you know if I have any questions.