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. Image for the cookie policy date

Unable to create Image of first page of a Word document

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



2 Replies

BP Bhuvaneswari P Syncfusion Team January 22, 2009 01:41 PM UTC

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



BP Bhuvaneswari P Syncfusion Team January 23, 2009 01:10 PM UTC

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



Loader.
Live Chat Icon For mobile
Up arrow icon