We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Unable to create Image of first page of a Word document

Thread ID:

Created:

Updated:

Platform:

Replies:

78102 Nov 28,2008 04:32 AM Jan 23,2009 08:10 AM ASP.NET Web Forms (Classic) 2
loading
Tags: PDF
vishnu
Asked On November 28, 2008 04:32 AM

Hi,

I want to create an image of first page of the word document. I am using the following code to create the image but it creates image of whole page. please suggest me how to create the image of first page


protected void Button2_Click(object sender, EventArgs e)
{
ConvertToImage(FileUpload1.PostedFile.InputStream);

}
protected void ConvertToImage(Stream st)
{


HtmlConverter html = new HtmlConverter();
WordDocument wd = new WordDocument(st, FormatType.Doc); // Save stream to word document

WordDocument wd1 = wd.Sections[0].Document;

HTMLExport htmlExport = new HTMLExport();
MemoryStream HTML_STREAM = new MemoryStream();


htmlExport.SaveAsXhtml(wd1, HTML_STREAM); // Convert Doc to html


Byte[] bt = HTML_STREAM.GetBuffer();


using (MemoryStream PDF_STREAM1 = new MemoryStream(bt))
{
using (System.Drawing.Image img = html.ConvertToImage(PDF_STREAM1, System.Text.Encoding.UTF8, ImageType.Metafile, 1024, -1, AspectRatio.KeepWidth))
{
img.Save(Server.MapPath("~/test.jpg"));
}
}

}

Thanks and Regards
Vishnu



Bhuvaneswari P [Syncfusion]
Replied On January 22, 2009 08:41 AM

Hi Vishnu,

Thank you for your interest in Syncfusion products.

We are working on this issue and will update the details by tomorrow.

Best Regards,
Bhuvana


Bhuvaneswari P [Syncfusion]
Replied On January 23, 2009 08:10 AM

Hi Vishnu,

We don't have direct support for this option. However, we can work around this by creating new document which contains the first page of the original document. Now convert this first page document to HTML then convert this page to PDF document. Please refers to the below code snippet to do so:


WordDocument doc = new WordDocument(@"..\..\Doc to HTML.doc");
WordDocument doc1 = new WordDocument();
doc1.EnsureMinimal();
int flag = 0;
foreach (Entity ent in doc.ChildEntities)
{
if (ent is WSection)
{
WSection section = ent as WSection;
foreach (WParagraph paragraph in section.Body.Paragraphs)
{
if (flag != 1)
{
doc1.LastSection.Paragraphs.Add(paragraph.Clone() as IWParagraph);
foreach (ParagraphItem item in paragraph.Items)
{
if (item.EntityType == EntityType.Break)
{
Break br = item as Break;
if (br.BreakType == Syncfusion.DocIO.DLS.BreakType.PageBreak)
{
MessageBox.Show("break");
flag = 1;
doc1.LastParagraph.Items.Remove(doc1.LastParagraph.Items.LastItem) ;
break;
}
}
}

}
else
break;
}
}
}
if (doc.LastSection.HeadersFooters.Header.Paragraphs.Count != 0)
doc1.LastSection.HeadersFooters.Header.Paragraphs.Add(doc.LastSection.HeadersFooters.Header.Paragraphs[0].Clone() as IWParagraph ) ;
if (doc.LastSection.HeadersFooters.Footer.Paragraphs.Count !=0)
doc1.LastSection.HeadersFooters.Footer.Paragraphs.Add(doc.LastSection.HeadersFooters.Footer.Paragraphs[0].Clone() as IWParagraph);
//doc1.Save("sample.doc");
HTMLExport htmlExport = new HTMLExport();
htmlExport.SaveAsXhtml(doc1, "doctohtml_res.html");

System.Diagnostics.Process.Start("sample.doc");
System.Diagnostics.Process.Start("doctohtml_res.html");



Please refers to the below sample:
http://websamples.syncfusion.com/samples/PDF.Windows/F78102/main.htm

In this sample I have converted one page word document to HTML, then convert this page to PDF or image.

Please let me know if this helps you.

Best Regards,
Bhuvana



CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;