C# .NET - insert html image in word document using docbuilder not working....

Asked By deepi singh on 14-Jun-12 07:56 AM
see my below code iam not able to write html image into word document and also in pdf?

when i kept image tag src as c:\winter.jpg it is working ...if i kept image tag src as url i mean 'http://wkt002:20649/SelectImage.aspx?ImageID=8   it is not wokring in this case...pls help me out....



var oImgChkDocument = new Document();

DocumentBuilder builder = new DocumentBuilder(oImgChkDocument);

string stylesheetPath = @"C:\Footnote.css";

StringBuilder sbHfnContent=new StringBuilder();

string value = "<p><img class='' height='101' width='144' src='http://wkt002:20649/SelectImage.aspx?ImageID=8' /></p>";

sbHfnContent.Append(value);

sbHfnContent.Insert(0,

"<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"></meta></head>" +

"<link href=\"" + stylesheetPath + "\" type=\"text/css\" rel=\"Stylesheet\" />" +

"<body>");

sbHfnContent.Append(

"</body></html>");

sbHfnContent.Replace(

"border=\"1\"", "border=\"0\"");

builder.InsertHtml(sbHfnContent.ToString());


oImgChkDocument.Save(

@"C:\htmlimage.docx");

oImgChkDocument.Save(

@"C:\out.pdf");
Asked By deepi singh on 15-Jun-12 07:01 AM
i want using docbuilder and aspose.words?
Asked By deepi singh on 15-Jun-12 10:08 AM
check this conversion and let me know any update ....

http://www.aspose.com/community/forums/thread/390091/insert-html-image-in-word-document-using-docbuilder-not-working.aspx#390096
Vikram Singh Saini replied to deepi singh on 16-Jun-12 04:49 AM
Hi,

Tested your code with little bit modifications. Modification is highlighted in code. Code here:

protected void CreateDocument()

    {

      var oImgChkDocument = new Document();

 

      DocumentBuilder builder = new DocumentBuilder(oImgChkDocument);

 

      string stylesheetPath = Server.MapPath(@"~/FootNote.css");

 

      StringBuilder sbHfnContent = new StringBuilder();

 

      string imagePath = @"http://www.orkutbuddy.com/albums/glitters/cartoon/cartoons_20_63_.gif";

      string value = "<p><img src='" + imagePath + "' /></p>";

 

      sbHfnContent.Append(value);

 

      sbHfnContent.Insert(0,

      "<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"></meta></head>" +

      "<link href=\"" + stylesheetPath + "\" type=\"text/css\" rel=\"Stylesheet\" />" +

      "<body>");

     

      sbHfnContent.Append("</body></html>");

 

      sbHfnContent.Replace("border=\"1\"", "border=\"0\"");

 

      builder.InsertHtml(sbHfnContent.ToString());

 

      oImgChkDocument.Save(@"C:\MyFile.docx");

 

      oImgChkDocument.Save(@"C:\MyPdfFile.pdf");

 

      Response.Write("File saved.");

 

    }

 
Well! I noticed that you have set url as http://wkt002:20649/SelectImage.aspx?ImageID=8  and I put the same url in the address bar with no image or output. So when the url is not returning any image output, how the same can be shown in the word and pdf files.

Basically you need to modify the http://url part so that it can display image. Even I tested the code with hosted website on localhost, and the code failed. However when you would test the above code url it will work fine. And the url will return image if executed in browser.

Hope this helps.


deepi singh replied to Vikram Singh Saini on 20-Jun-12 02:26 AM
not working.....
Vikram Singh Saini replied to deepi singh on 21-Jun-12 08:56 AM
not working.....

Could be possible for number of reasons. For any case, I am adding the zip file of demo application I used for testing. You can also share yours with us.

Let us know the steps you are using for testing. For e.g. are you trying to achieve objective by hosting or running same from VS IDE.

DocumentBuilder.zip

I have used Aspose Words version 11.4.0.0 for testing. The same dll files have been not included in zip. You can download same from aspose website.

Let us know whether the zip file solution worked or not. If not, share your code in .zip file.
deepi singh replied to Vikram Singh Saini on 22-Jun-12 03:04 AM
here iam getting image from remote serve url....i too used latest version aspose.words 11.4.0.0 version..actually aspose.words downloads images before isnerting into word document.....the url which u have given works fine.......but here iam getting image from server thts the probs..in tht case aspose.words is not downloading image from remote server ..i think thts y iam geting cross mark in final output word document or pdf......


how to insert remote image in word document using documentbuilder.inserthtml in aspose.words?

When the URL for the image is "localhost," the image displays correctly in the document.  When the url is a remote host, the image does not display.  It is replaced with a small box containing an x. 

How do I get a remote image to display correctly?  The image displays fine in IE or Outlook, just not a document created from Aspose.


iam unable to give tht sampel remote image url..becoz it will be accessble from here only..not from other systems....if any one try to access tht remote image url from outside systems....then no image will display...

Vikram Singh Saini replied to deepi singh on 22-Jun-12 05:06 AM
Some lines from your problem statement:

1. aspose.words is not downloading image from remote server
2. When the url is a remote host, the image does not display
3. The image displays fine in IE or Outlook, just not a document created from Aspose.

OK! Considering all points I have some questions:

Q.1. When you say remote server or remote host, do you mean a server in Local Area Network with some IP Address?
Q.2. Image displays fine in IE, that is, you paste remote url of image in IE and it's showing there. True or False?
Q.3. If I have to replicate your problem on my side, what steps do I need to follow for same?
Q.4. The images hosted on website are also remote to our system. Are you trying to access image other than http protocol?

Please answer to questions as the problem cannot be solved until they are experienced anyhow.
deepi singh replied to Vikram Singh Saini on 22-Jun-12 06:11 AM
Q.1. When you say remote server or remote host, do you mean a server in Local Area Network with some IP Address?
yes.......and the server where the application is hosted...

Q.2. Image displays fine in IE, that is, you paste remote url of image in IE and it's showing there. True or False?
  True

Q.3. If I have to replicate your problem on my side, what steps do I need to follow for same?

i have a serevr url where my images are stored .....for sample my server url looks like this

http://rthfjkl002:20649/SelectImage.aspx?ImageID=8

to replicate the problem u pls insert one image in any server and use tht image path in aspose.words code..



Q.4. The images hosted on website are also remote to our system. Are you trying to access image other than http protocol?

yes .....iam trying to access images through http protocol..


Note:iam facing this problem in test , stage sites and Dev servers not in production.......
Vikram Singh Saini replied to deepi singh on 22-Jun-12 10:04 PM
Well! I think I have framed solution but need your confirmation for same.

Understanding Requirement

I have server X with IP address as 223.176.59.170 and website hosted on it with name as ImageHub with SelectImage.aspx as home page of website. In the same website we have stored two images in folder named Images. Now as per our requirement we want to use the following url http://223.176.59.170/ImageHub/SelectImage.aspx?ImageID=4
for returning image in Aspose document.

Solution for requirement


Here is the code for returning image as response to caller. We have used ContentType for gif type only. However you can use switch case for modifying same based on file type. (Code snippet from SelectImage.aspx.cs):

public partial class _Default : System.Web.UI.Page

{

    string imageUrl;

    protected void Page_Load(object sender, EventArgs e)

    {

      if (Request.QueryString["ImageID"] != null)

      {

        string imageId = Request.QueryString["ImageID"].ToString();

 

        // Set imageUrl

        switch (imageId)

        {

          case "8":

            imageUrl = VirtualPathUtility.ToAbsolute("~") + @"/Images/save.gif";

            break;

 

          case "4":

            imageUrl = VirtualPathUtility.ToAbsolute("~") + @"/Images/register.png";

            break;

        }

 

        // Check if file exists and set headers appropriately

        // Return response directly to output steam

        System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath(imageUrl));

        if (file.Exists)

        {

          Response.Clear();

          Response.AddHeader("Content-Disposition", @"inline; filename=\" + file.Name);

          Response.AddHeader("Content-Length", file.Length.ToString());

 

          // Put switch..case for modifying ContentType on basis of file extension

          // I have written code for gif type

          Response.ContentType = "image/gif";

          Response.TransmitFile(file.FullName);

          Response.End();

        }

 

      }

 

    }

}


And in the code part of DocBuilder we have modified the imagePath as: (You can use website name rthfjkl002 in place of the IP address)

string imagePath = @"http://223.176.59.170/ImageHub/SelectImage.aspx?ImageID=8";


That's all! Test it and let us know.

deepi singh replied to Vikram Singh Saini on 25-Jun-12 03:23 AM
can u pls let me know is it working from documentbuilder.inserthtml with aspose.words?

Vikram Singh Saini replied to deepi singh on 25-Jun-12 03:30 AM
Yes! I have tested it first and then shared with you.

The code part as I suggested is returning image contents as response. And so it's working absolutely fine.
deepi singh replied to Vikram Singh Saini on 25-Jun-12 04:13 AM
http://223.176.59.170/ImageHub/SelectImage.aspx?ImageID=4

In this site u had used ip address ..in my case my site looks like this wht is this 20649?is it server name?

http://wht002:20649/SelectImage.aspx?ImageID=8

Vikram Singh Saini replied to deepi singh on 25-Jun-12 04:18 AM
http://wht002:20649/SelectImage.aspx?ImageID=8

The 20649 is the port number which listens request to your site.

While developing websites or web application on local system using VS as IDE, and when we execute same for testing, you might have noticed it shows url as http://localhost:2160/DocBuilder/Default.aspx and so here 2160 is the port which is listening my requests.

By default the port number 80 is reserved for IIS on Windows. On your server I believe that port has been in used by some other web application or service.
deepi singh replied to Vikram Singh Saini on 25-Jun-12 06:40 AM

Thanks for sharing the information. The error you facing is not related to Aspose.Words. It seems that you can not access image URL over network. Please check your network setting, firewall etc

i tried with the below code

const string href = "here i gave remote image path url"

WebRequest request = WebRequest.Create(href);

WebResponse response = request.GetResponse();--iam getting error at this line given below

The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF

 

In my local system windows firewalls is not installed and iam not able to see tht windows firewalls in controlpanel-security centre..in tht case y iam not able to accesss tht remote image url in aspsoe.words code and y it is getting cross mark in word document......

where as in remote servers(Dev servers) windows firewall is in off mode.....even though

iam not able to accesss tht remote image url in aspsoe.words code and y it is getting cross mark in word document......





Vikram Singh Saini replied to deepi singh on 25-Jun-12 07:49 AM
Ok! As you mentioned http://wht002:20649/SelectImage.aspx?ImageID=8

Can you share the source code of SelectImage.aspx and SelectImage.aspx.cs both files? I would like to see how the things are working in this page.

Second you didn't respond to me about the code I shared with you. It worked or not? What error you faced? And let us know the Server OS?
deepi singh replied to Vikram Singh Saini on 26-Jun-12 03:00 AM

selctimage.aspx.cs

int imageID = 0;


if (Request.QueryString["ImageID"] != null)

{

imageID =

Convert.ToInt32(Request.QueryString["ImageID"].ToString());

}


if (imageID != 0)


byte[] barrImg = dataccess.GetFootNoteImage(sParams, ref _error);

System.IO.

MemoryStream ms = new System.IO.MemoryStream(barrImg);


Bitmap bmp = (Bitmap)System.Drawing.Image.FromStream(ms);


Bitmap bmp1 = new Bitmap(bmp, new Size(Convert.ToInt32(bmp.PhysicalDimension.Width), Convert.ToInt32(bmp.PhysicalDimension.Height)));

Response.ContentType =

"image/gif";

bmp1.Save(Response.OutputStream, System.Drawing.Imaging.

ImageFormat.Jpeg);

ms.Close();

deepi singh replied to Vikram Singh Saini on 26-Jun-12 09:44 AM
Your code works fine for ur given url...but not for my url

My Remote Server Image url Syntax looks as:

http://IPAddress:PortNumber/SelectImage.aspx?ImageID=8

or

http://WebsiteName:PortNumber/SelectImage.aspx?ImageID=8



Vikram Singh Saini replied to deepi singh on 26-Jun-12 10:55 AM
I would ask you to retry with modification in web.config of the website on that server as:

<configuration> 
 
<system.net>
 
<settings>
   
<httpWebRequest useUnsafeHeaderParsing="true" />
 
</settings>
 
</system.net>
</configuration>


Credit: http://stackoverflow.com/questions/1491841/httpwebrequesterror-the-server-committed-a-protocol-violation-section-response

You could try suggested solution at link: http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/ff098248-551c-4da9-8ba5-358a9f8ccc57/

And last host some simple asp.net website on that server, and if possible specify same port number for that website too. If again it throws same type of error, I think there is something that is not allowing to listen to request from that port number. But we would discuss on it later. First give these suggestions try.
deepi singh replied to Vikram Singh Saini on 27-Jun-12 03:17 AM
Yeah grt it is working fine.....Thanks a lot...just i kept wht u said in local application Config file and also i kept in remote application config file it is working fine.....
 

<configuration>
 
<system.net>

 
<settings>

   
<httpWebRequest useUnsafeHeaderParsing="true" />

 
</settings>

 
</system.net>

</configuration>


now iam able to see image in final word document through aspose.wods code.....

Document


doc = new Document();


DocumentBuilder builder = new DocumentBuilder(doc);


string html = @"<p><img src='Remote Server Image url' alt='test image' width='178' height='129' /></p>";

builder.InsertHtml(html);

doc.Save(

@"c:\New.docx");


1.can u pls explain me how it works by putting code in config file...

2.In my case it is not working in test site where as it works fine in production site means ...In production i have tht code in config file...where as in test site i dont have tht code in config file?did i miss tht code in config file in test site?

Vikram Singh Saini replied to deepi singh on 27-Jun-12 03:24 AM
1.can u pls explain me how it works by putting code in config file...

AND
2.In my case it is not working in stage,test sites where as it works fine in production means ...In production i have tht code in config file...where as in test and stage site i dont have tht code in config file?we missed tht cod ein config file in test and stage sites?


Answer to both questions:

The SERVER on which you have hosted your website ( which is hosting images) is returning response with characters that are prohibited. And so the exception was thrown in our case.

What do the line <httpWebRequest useUnsafeHeaderParsing="true" /> do?

Your response's header value would not be checked for any unsafe things in that. Unsafe things could be:

(1) CR or LF alone is used for marking end of line in header. CRLF (Carriage Return and Line Feed) are special characters that represent the End Of Line (EOL) marker for many Internet protocols. If the url header contains pattern %0d%0a, this pattern is the HTTP equivalent of CRLF.

You might remember you shared with me about such CR LF error that elaborate more as Protocol violation.

(2) Space in header names.

(3) Header names containing non-ASCII characters.

So if it is set to true there is risk of CRLF injection attacks by hackers ( visit http://www.acunetix.com/websitesecurity/crlf-injection.htm for more).

In essence, the programmer cannot control, it's the server behavior. Check server manual about how it adheres to HTTP protocol. I believe that the server is not adhering to the HTTP protocol somehow and hence was the error.

I can recommend you to change your server if you are concerned about risks attached. (From http://msdn.microsoft.com/en-us/library/65ha8tzh.aspx )
deepi singh replied to Vikram Singh Saini on 27-Jun-12 04:12 AM
Thanks for ur detailed explanation:).....
Vikram Singh Saini replied to deepi singh on 27-Jun-12 04:44 AM
welcome :)