Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

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

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

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

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: 154
Joined: Thu Oct 20, 2011 2:53 am

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

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: 154
Joined: Thu Oct 20, 2011 2:53 am

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

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: 154
Joined: Thu Oct 20, 2011 2:53 am

Wed Oct 30, 2019 6:08 pm

I'm receiving an error message ("The name 'Image' does not exist in the current context") from the following line of C# code:

Code: Select all
DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"E:\Logo\doclog.png"));


I have all of the "using" statements as indicated in the guide.

Whenever I select the "Show Potential Options" in Visual Studio, the only "using" statement suggested is the following:

Code: Select all
using static System.Net.Mime.MediaTypeNames;



What is this "Image" class/reference, and how can I use it in my project?

EKMconvertDoc
 
Posts: 5
Joined: Wed Sep 04, 2019 8:13 pm

Wed Oct 30, 2019 6:19 pm

BTW, I was able to insert the image by removing the Image reference altogether:

Code: Select all
DocPicture headerimage = paragraph.AppendPicture(@"E:\Logo\doclog.png");


Is the documentation outdated?

EKMconvertDoc
 
Posts: 5
Joined: Wed Sep 04, 2019 8:13 pm

Thu Oct 31, 2019 6:50 am

Hi,

Thanks for your inquiry.
Please reference System.Drawing.dll from the assembly in your project then use the namespace System.Drawing, the "Image" class belongs to it.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 293
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.Doc