Inserting image

Technical support for Spire.Doc

Moderator: iceblue support

Inserting image

Postby ahamed » Fri Aug 26, 2011 2:41 pm

Following your example does not seems to right.

I have added the following code to add image to the page

Paragraph paragraph = new Paragraph(document);
paragraph.AppendText("This is an image.");

//Insert an image.
paragraph.AppendPicture(Image.FromFile("image.jpg"));

seems to be names space confliction please let me know the names space for the Image.FromFile I am currently using following spaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using smp_class.Partbuilderbuild;
using smp_class.Quote;
using smp_class.Redirect;
using System.Data.SqlClient;

using Spire.Doc;
using Spire.Doc.Interface;
using Spire.Doc.Documents;
using Spire.Doc.Utilities;
using Spire.Doc.Collections;

Thank you.
ahamed
 
Posts: 15
Joined: Mon Aug 15, 2011 1:09 pm

Re: Inserting image

Postby harry.support » Mon Aug 29, 2011 8:33 am

Dear ahamed,

Thanks for your inquiry.
For your codes
Code: Select all
Paragraph paragraph = new Paragraph(document);
paragraph.AppendText("This is an image.");

//Insert an image.
paragraph.AppendPicture(Image.FromFile("image.jpg"));


3 namespaces are enough:
Code: Select all
using System.Drawing;

using Spire.Doc;
using Spire.Doc.Documents;
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar
harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Re: Inserting image

Postby andreas.andersson » Tue Jan 03, 2012 2:42 pm

Can I somehow control the size of the image?
And how do I do that?
andreas.andersson
 
Posts: 3
Joined: Wed Dec 21, 2011 12:58 pm

Re: Inserting image

Postby Suvi.Wu » Thu Jan 05, 2012 9:33 am

Hello andreas,

Thank you for your inquiry.

Since image.Width and Image.Height are readonly, we can't modify them.
We provided you a workaround to modify the image size. Please try the following code:
Code: Select all
            Document document = new Document();
            document.LoadFromFile(@"..\..\Test.docx", FileFormat.Docx);

            Image image = Image.FromFile(@"..\..\Winter.jpg");

            Paragraph paragraph = document.Sections[0].AddParagraph();
            DocPicture pic = paragraph.AppendPicture(image);

            pic.Height = pic.Height * 0.3f;
            pic.Width = pic.Width* 0.2f;
           
            document.AddSection().AddParagraph().AppendPicture(pic.Image);

            document.Sections.Remove(document.Sections[1]);
       
            document.SaveToFile(@"..\..\result.docx",FileFormat.Docx);
            System.Diagnostics.Process.Start(@"..\..\result.docx");


If you still have any other questions, please don't hesitate to contact us.
Have a nice day.
User avatar
Suvi.Wu
 
Posts: 156
Joined: Thu Oct 20, 2011 2:53 am

Re: Inserting image

Postby magnus.frodell » Mon Feb 20, 2012 9:55 pm

How do I combine this "resizing" with selecting which paragraph to place the image at?
magnus.frodell
 
Posts: 3
Joined: Sat Feb 18, 2012 3:51 pm

Re: Inserting image

Postby Suvi.Wu » Tue Feb 21, 2012 3:07 am

Hello magnus.frodell,

Sorry for the inconvenience caused by us.

Our product allocate the position through the "Section" and "Paragraph", so you have to know exactly the section and the paragraph which you want to place the image at. I attached the optimized code with detail comment line.

Code: Select all
 //load the docx template
            Document document = new Document();
            document.LoadFromFile(@"..\..\Test.docx", FileFormat.Docx);

            //load the image to insert into the the docx template
            Image image = Image.FromFile(@"..\..\Winter.jpg");

            //select the paragraph to place the image at
            Paragraph pa2 = document.Sections[0].Paragraphs[2];
           
            //insert the image at the selected paragraph
            DocPicture pic = pa2.AppendPicture(image);

            //resize the image
            pic.Height = pic.Height * 0.3f;
            pic.Width = pic.Width * 0.2f;

            //save the result file
            document.SaveToFile(@"..\..\result.docx", FileFormat.Docx);

            //launch the result file
            System.Diagnostics.Process.Start(@"..\..\result.docx");


Hoping this can be helpful.

Have a great day.

In case of any ambiguity, please feel free to contact us.

Kind regards.
Suvi
e-iceblue support
User avatar
Suvi.Wu
 
Posts: 156
Joined: Thu Oct 20, 2011 2:53 am

Re: Inserting image

Postby magnus.frodell » Mon Feb 27, 2012 10:11 am

Suvi.Wu wrote:Hello magnus.frodell,

Sorry for the inconvenience caused by us.

Our product allocate the position through the "Section" and "Paragraph", so you have to know exactly the section and the paragraph which you want to place the image at. I attached the optimized code with detail comment line.

Code: Select all
 //load the docx template
            Document document = new Document();
            document.LoadFromFile(@"..\..\Test.docx", FileFormat.Docx);

            //load the image to insert into the the docx template
            Image image = Image.FromFile(@"..\..\Winter.jpg");

            //select the paragraph to place the image at
            Paragraph pa2 = document.Sections[0].Paragraphs[2];
           
            //insert the image at the selected paragraph
            DocPicture pic = pa2.AppendPicture(image);

            //resize the image
            pic.Height = pic.Height * 0.3f;
            pic.Width = pic.Width * 0.2f;

            //save the result file
            document.SaveToFile(@"..\..\result.docx", FileFormat.Docx);

            //launch the result file
            System.Diagnostics.Process.Start(@"..\..\result.docx");


Hoping this can be helpful.

Have a great day.

In case of any ambiguity, please feel free to contact us.

Kind regards.
Suvi
e-iceblue support


Hi,

Thank you for the information. Is there any way to find the section and paragraph for a specified text string?

Br,

Magnus
magnus.frodell
 
Posts: 3
Joined: Sat Feb 18, 2012 3:51 pm

Re: Inserting image

Postby Suvi.Wu » Fri Mar 02, 2012 7:40 am

Hi Magnus,

I am sorry for that Spire.Doc can't find the section and paragraph for a specified text string as different paragraphs' text string can be same.

Have a great day.

Kind Regards.
Suvi
e-iceblue support
User avatar
Suvi.Wu
 
Posts: 156
Joined: Thu Oct 20, 2011 2:53 am


Return to Spire.Doc

Who is online

Users browsing this forum: No registered users and 1 guest