Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.
Thu Mar 31, 2016 8:50 am
Hi, I am creating the word document from the aspx page using Spire.Doc and now I need to insert Header and Footer into the MS Word upon creating the word document. Here is what I am trying:
- Code: Select all
using (MemoryStream buffer = new MemoryStream())
{
using (TextWriter writer = new StreamWriter(buffer, Encoding.UTF8))
{
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(writer))
{
pnlPrint.RenderControl(htmlWriter);
htmlWriter.Flush();
writer.Flush();
buffer.Position = 0;
string stylesheet = "Default.css";
using (StreamReader reader = new System.IO.StreamReader(buffer, Encoding.UTF8))
{
String baseHref = Request.Url.AbsoluteUri;
document.LoadHTML(reader, baseHref, Spire.Doc.Documents.XHTMLValidationType.None);
document.HtmlExportOptions.CssStyleSheetType = CssStyleSheetType.External;
document.HtmlExportOptions.CssStyleSheetFileName = stylesheet;
//Initialize a Header Instance
HeaderFooter header = document.Sections[0].HeadersFooters.Header;
//Add Header Paragraph and Format
Paragraph paragraph = header.AddParagraph();
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
//Append Picture for Header Paragraph and Format
DocPicture headerimage = paragraph.AppendPicture(System.Drawing.Image.FromFile(@"E:\Barsan Lenovo\My Project\FreeSpireDoc\FreeSpireDoc\Docs\logo.jpg"));
headerimage.VerticalAlignment = ShapeVerticalAlignment.Bottom;
//Initialize a Footer Instance
HeaderFooter footer = document.Sections[0].HeadersFooters.Footer;
//Add Footer Paragraph and Format
Paragraph paragraph2 = footer.AddParagraph();
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left;
//Append Picture and Text for Footer Paragraph
DocPicture footerimage = paragraph2.AppendPicture(System.Drawing.Image.FromFile(@"E:\Barsan Lenovo\My Project\FreeSpireDoc\FreeSpireDoc\Docs\logo.jpg"));
TextRange TR = paragraph2.AppendText("Copyright © 2013 e-iceblue. All Rights Reserved.");
TR.CharacterFormat.FontName = "Arial";
TR.CharacterFormat.FontSize = 10;
TR.CharacterFormat.TextColor = Color.Black;
//Save and Launch
//document.SaveToFile("ImageHeaderFooter.docx", FileFormat.Docx);
document.SaveToFile("Sample.docx", FileFormat.Docx, Response, HttpContentType.Attachment);
System.Diagnostics.Process.Start("Sample.docx");
}
}
}
}
This above code is not throwing any error nor creating the header and footer into the document. Please help me on this. Thanks.
-
barsan101
-
- Posts: 7
- Joined: Thu Mar 31, 2016 7:26 am
Thu Mar 31, 2016 9:04 am
Hi,
Please first save to document stream and then reload the stream to add header and footer.
Best Regards,
Amy
E-iceblue support team
-
amy.zhao
-
- Posts: 2766
- Joined: Wed Jun 27, 2012 8:50 am
Thu Mar 31, 2016 9:12 am
Thanks for your reply. As I am very new to this Spire.Doc I would appreciate most if you could provide a sample code with to elaborate your answer. Thanks in advance.
-
barsan101
-
- Posts: 7
- Joined: Thu Mar 31, 2016 7:26 am
Thu Mar 31, 2016 10:26 am
I have changed my code as per your suggestion (though I am not so sure whether I am doing all correct or not). Still I am not getting the header footer after export. Please find my code below:
- Code: Select all
using (MemoryStream buffer = new MemoryStream())
{
using (TextWriter writer = new StreamWriter(buffer, Encoding.UTF8))
{
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(writer))
{
pnlPrint.RenderControl(htmlWriter);
htmlWriter.Flush();
writer.Flush();
buffer.Position = 0;
string stylesheet = "Default.css";
using (StreamReader reader = new System.IO.StreamReader(buffer, Encoding.UTF8))
{
String baseHref = Request.Url.AbsoluteUri;
document.LoadHTML(reader, baseHref, Spire.Doc.Documents.XHTMLValidationType.None);
document.HtmlExportOptions.CssStyleSheetType = CssStyleSheetType.External;
document.HtmlExportOptions.CssStyleSheetFileName = stylesheet;
document.SaveToStream(buffer, FileFormat.Docx);
//FileStream to_strem = new FileStream("Sample.docx", FileMode.Open);
//FileStream from_stream = File.OpenRead("Sample.docx");
Document doc = new Document();
doc.LoadFromStream(buffer, FileFormat.Docx);
Section section = doc.AddSection();
HeaderFooter header = section.HeadersFooters.Header;
Paragraph headerParagraph = header.AddParagraph();
DocPicture headerPicture = headerParagraph.AppendPicture(System.Drawing.Image.FromFile(@"E:\Barsan Lenovo\My Project\FreeSpireDoc\FreeSpireDoc\Docs\logo.jpg"));
HeaderFooter footer = section.HeadersFooters.Footer;
Paragraph footerParagraph = footer.AddParagraph();
DocPicture footerPicture = footerParagraph.AppendPicture(System.Drawing.Image.FromFile(@"E:\Barsan Lenovo\My Project\FreeSpireDoc\FreeSpireDoc\Docs\logo.jpg"));
doc.SaveToFile("Sample.docx", FileFormat.Docx, Response, HttpContentType.Attachment);
System.Diagnostics.Process.Start("Sample.docx");
}
}
}
}
-
barsan101
-
- Posts: 7
- Joined: Thu Mar 31, 2016 7:26 am
Fri Apr 01, 2016 2:29 am
Hi,
Thanks for further posting and sorry for the delay response.
I recreated a simple project to test your previous code below and it was ok, please ensure you were viewing your docx document is in Print Layout, and not in web Laout.
layout.png
- Code: Select all
using (MemoryStream buffer = new MemoryStream())
{
using (TextWriter writer = new StreamWriter(buffer, Encoding.UTF8))
{
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(writer))
{
pnlPrint.RenderControl(htmlWriter);
htmlWriter.Flush();
writer.Flush();
buffer.Position = 0;
string stylesheet = "Default.css";
using (StreamReader reader = new System.IO.StreamReader(buffer, Encoding.UTF8))
{
String baseHref = Request.Url.AbsoluteUri;
document.LoadHTML(reader, baseHref, Spire.Doc.Documents.XHTMLValidationType.None);
document.HtmlExportOptions.CssStyleSheetType = CssStyleSheetType.External;
document.HtmlExportOptions.CssStyleSheetFileName = stylesheet;
Section section = document.Sections[0];
HeaderFooter header = section.HeadersFooters.Header;
Paragraph headerParagraph = header.AddParagraph();
DocPicture headerPicture = headerParagraph.AppendPicture(System.Drawing.Image.FromFile(@"C:\\1.png"));
HeaderFooter footer = section.HeadersFooters.Footer;
Paragraph footerParagraph = footer.AddParagraph();
DocPicture footerPicture = footerParagraph.AppendPicture(System.Drawing.Image.FromFile(@"C:\\1.png"));
TextRange TR = footerParagraph.AppendText("Copyright © 2013 e-iceblue. All Rights Reserved.");
document.SaveToFile("Sample.docx", FileFormat.Docx, Response, HttpContentType.Attachment);
}
}
}
}
Best Regards,
Amy
E-iceblue support team
Login to view the files attached to this post.
-
amy.zhao
-
- Posts: 2766
- Joined: Wed Jun 27, 2012 8:50 am
Tue Apr 05, 2016 8:53 am
Hi,
Has your issue been resolved?
Thanks for your feedback in advance.
Best Regards,
Amy
E-iceblue support team
-
amy.zhao
-
- Posts: 2766
- Joined: Wed Jun 27, 2012 8:50 am