Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Aug 23, 2018 5:01 am

Hi, i tried add pdf file to word, but it does not work
can you look what is wrong, thank you


Spire.Doc.Document doc = new Spire.Doc.Document();
doc.LoadFromFile("d:\\1 Page template.docx");

Spire.Doc.Fields.DocPicture DocPicture = new Spire.Doc.Fields.DocPicture(doc);
byte[] src = File.ReadAllBytes("d:\\5160.pdf");
doc.AddSection().AddParagraph().AppendOleObject(src, DocPicture, Spire.Doc.Documents.OleObjectType.AdobeAcrobatDocument);

doc.SaveToFile("d:\\1 Page template.pdf", Spire.Doc.FileFormat.PDF);
doc.Close();

sserra
 
Posts: 76
Joined: Tue Jun 13, 2017 4:03 pm

Thu Aug 23, 2018 8:02 am

Hello,

Thanks for your post.
Please refer to below code to set the OLE picture correctly. If there is any question, welcome to write back.
Code: Select all
 static void Main(string[] args)
 {
     Spire.Doc.Document doc = new Spire.Doc.Document();
     doc.LoadFromFile(@"1 Page template.docx");
     Section mysec = doc.AddSection();
     Paragraph para = mysec.AddParagraph();
     Spire.Doc.Fields.DocPicture picture = new Spire.Doc.Fields.DocPicture(doc);
     byte[] src = File.ReadAllBytes("5160.pdf");
     Image image = GetPDFImage(src);
     picture.LoadImage(image);
     DocOleObject obj = para.AppendOleObject(src, picture, Spire.Doc.Documents.OleObjectType.AdobeAcrobatDocument);
     //doc.SaveToFile("result.docx", Spire.Doc.FileFormat.Docx2013);
     doc.SaveToFile("result.pdf", Spire.Doc.FileFormat.PDF);
 }
 private static Image GetPDFImage(byte[] src)
 {
     PdfDocument pdf = new PdfDocument();
     pdf.LoadFromBytes(src);
     Image image = pdf.SaveAsImage(0);
     return image;
 }

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Thu Aug 23, 2018 3:42 pm

Thank you
that works, but I don't have license for Spire Pdf.
so, is there a way to use it with out Spire Pdf?

sserra
 
Posts: 76
Joined: Tue Jun 13, 2017 4:03 pm

Fri Aug 24, 2018 2:01 am

Hi,

Thanks for your feedback.
Of course, you could directly load an image from disk like below code.
Code: Select all
...
//Load image from disk
Image image = Image.FromFile("C:\\sample.png");
picture.LoadImage(image);
...

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Fri Aug 24, 2018 3:44 pm

I not need load image, i need attach PDF to Doc file with out use Spire Pdf.
Does Spire.Doc have direct method to do it?


thank you

sserra
 
Posts: 76
Joined: Tue Jun 13, 2017 4:03 pm

Mon Aug 27, 2018 4:07 am

Dear Steve,

Sorry for late reply as weekend.
The DocPicture parameter in AppendOleObject method is aimed to render the Pdf object. If you do not load an image to DocPicture, there will be no flag to show where the object has been added. Hence, loading an image is necessary.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Mon Aug 27, 2018 5:22 pm

Dear Nina,
my original message was "How to Add PD to Doc file".
I understand that i need create a image from PDF. You code use Spire.Pdf for create image from pdf file.
I don't have a license for Spire.Pdf - it's mean i can' use you approach.

it's a another way to do image from Pdf file?

thank you

sserra
 
Posts: 76
Joined: Tue Jun 13, 2017 4:03 pm

Tue Aug 28, 2018 6:15 am

Hello,

Thanks for your feedback.
Yes, I used the related code (save Pdf page to image) of our Spire.Pdf in my first reply, but after that, I provide the code to load an image from disk, which has no use of Spire.Pdf. The complete code is as follows. If there is any confusion, welcome to write back.
Code: Select all
Spire.Doc.Document doc = new Spire.Doc.Document();
doc.LoadFromFile(@"1 Page template.docx");
Section mysec = doc.AddSection();
Paragraph para = mysec.AddParagraph();
Spire.Doc.Fields.DocPicture picture = new Spire.Doc.Fields.DocPicture(doc);
byte[] src = File.ReadAllBytes("5160.pdf");
//Load an image from disk.
Image image = Image.FromFile("C:\\sample.png");
picture.LoadImage(image);
DocOleObject obj = para.AppendOleObject(src, picture, Spire.Doc.Documents.OleObjectType.AdobeAcrobatDocument);
//doc.SaveToFile("result.docx", Spire.Doc.FileFormat.Docx2013);
doc.SaveToFile("result.pdf", Spire.Doc.FileFormat.PDF);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Fri Aug 31, 2018 10:31 am

Hello,

Greetings from E-iceblue.
Has your issue been resolved?
Your feedback will be greatly appreciated.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Fri Aug 31, 2018 5:57 pm

yes,
thank you

sserra
 
Posts: 76
Joined: Tue Jun 13, 2017 4:03 pm

Mon Sep 03, 2018 1:39 am

Dear Steve,

Thanks for your feedback.
If you need assistance in the future, just feel free to contact us.
Have a nice day!

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.Doc