Word Document
Maximum file size: 1 MB. Files accepted: doc, docx, txt, rtf.
Click here to browse files.
fileerrors
Page Size
Source file:
filename
Target file type:
Page Header
Header Text: | Header Image: |
Click here to browse files
CustomValidator |
||
Header Font: | Font Size: | The font size must be a valid number. The font size must be a valid number. | ||
Color: | The format of color value must be rrggbb. | Alignment : |
Page Footer
Footer Text: | Footer Image: |
Click here to browse files
CustomValidator |
||
Footer Font: | Font Size: | The font size must be a valid number. The font size must be a valid number. | ||
Color: | The format of color value must be rrggbb. | Alignment: |
- Demo
- Java
- C# source
This demo shows you how to set the page size, page header and footer of the Word document. We show you also set some picture as the background of the header/footer.
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.DocPicture; import com.spire.doc.fields.TextRange; import java.awt.*; import java.awt.geom.Dimension2D; import java.io.IOException; public class AddHeaderAndFooterDemo { public void addHeaderAndFooter(String docFile,String headerImageFile, String footerImageFile, String headerColor, String footerColor, String resultFilePath) throws IOException { Document document = new Document(); document.loadFromFile(docFile,FileFormat.Auto); pageSet(document,PageSize.A4); setHeader(document, String2Color(headerColor),headerImageFile); setFooter(document,String2Color(footerColor),footerImageFile); document.saveToFile(resultFilePath,FileFormat.Doc); } private void pageSet(Document document, Dimension2D pageSize){ for (int i = 0; i < document.getSections().getCount(); i++) { //Page size Section section = document.getSections().get(i); section.getPageSetup().setPageSize(pageSize); section.getPageSetup().getMargins().setTop(72f); section.getPageSetup().getMargins().setBottom(72f); section.getPageSetup().getMargins().setLeft(89f); section.getPageSetup().getMargins().setRight(89f); } } private void setFooter(Document document,Color color,String footerImageFile ) throws IOException { String footerText = "Spire.Doc"; String fontName = "Calibri"; float fontSize = (float)12.0; for (int i = 0; i < document.getSections().getCount(); i++) { Section section = document.getSections().get(i); HeaderFooter footer = section.getHeadersFooters().getFooter(); //Footer Paragraph footerParagraph = footer.addParagraph(); DocPicture footerPicture = footerParagraph.appendPicture(footerImageFile); footerPicture.setTextWrappingStyle(TextWrappingStyle.Behind); footerPicture.setHorizontalOrigin(HorizontalOrigin.Page); footerPicture.setHorizontalAlignment(ShapeHorizontalAlignment.Center); footerPicture.setVerticalOrigin(VerticalOrigin.Page); footerPicture.setVerticalAlignment(ShapeVerticalAlignment.Bottom); //Footer text TextRange text = footerParagraph.appendText(footerText); text.getCharacterFormat().setFontName(fontName); text.getCharacterFormat().setFontSize(fontSize); text.getCharacterFormat().setTextColor(color); footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Left); //border footerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Hairline); footerParagraph.getFormat().getBorders().getBottom().setSpace(0.03f); //Insert page number footerParagraph = footer.addParagraph(); footerParagraph.appendField("page number", FieldType.Field_Page); footerParagraph.appendText("of"); footerParagraph.appendField("number of pages", FieldType.Field_Num_Pages); footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right); } } private void setHeader(Document document, Color color, String headerImageFile){ String headerText = "E-iceblue"; String fontName = "Calibri"; float fontSize = (float)12.0; for (int i = 0; i < document.getSections().getCount(); i++) { Section section = document.getSections().get(i); HeaderFooter header = section.getHeadersFooters().getHeader(); Paragraph headerParagraph = header.addParagraph(); DocPicture headerPicture = headerParagraph.appendPicture(headerImageFile); headerPicture.setTextWrappingStyle(TextWrappingStyle.Behind); headerPicture.setHorizontalOrigin(HorizontalOrigin.Page); headerPicture.setHorizontalAlignment(ShapeHorizontalAlignment.Center); headerPicture.setVerticalOrigin(VerticalOrigin.Page); headerPicture.setVerticalAlignment(ShapeVerticalAlignment.Top); //Header text TextRange text = headerParagraph.appendText(headerText); text.getCharacterFormat().setFontName(fontName); text.getCharacterFormat().setFontSize(fontSize); text.getCharacterFormat().setTextColor(color); headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //border headerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Hairline); headerParagraph.getFormat().getBorders().getBottom().setSpace(0.03f); } } public static Color String2Color(String str) { int rgb = Integer.parseInt(str.substring(1), 16); Color color = null; try { color = new Color(rgb); return color; } catch (Exception e) { e.printStackTrace(); return null; } } }
using System; using System.Drawing; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace DemoOnlineCode { class HeaderAndFooter { public void demoHeaderAndFooter(String docFile, String headerImageFile = null, String footerImageFile = null, string pageSizeName = "A4", String headerFontColor = null, String footerFontColor = null) { Document document = new Document(docFile, FileFormat.Auto); SizeF pageSize = (SizeF)typeof(PageSize).GetField(pageSizeName).GetValue(null); PageSetup(document, pageSize); SetHeader(document, ConvertStrToColor(headerFontColor), headerImageFile); SetFooter(document, ConvertStrToColor(footerFontColor), footerImageFile); document.SaveToFile("demo.docx", FileFormat.Docx); } private void PageSetup(Document document, SizeF pageSize) { for (int i = 0; i < document.Sections.Count; i++) { Section section = document.Sections[i]; section.PageSetup.PageSize = pageSize; section.PageSetup.Margins.Top = (float)72.0; section.PageSetup.Margins.Bottom = (float)72.0; section.PageSetup.Margins.Left = (float)89.0; section.PageSetup.Margins.Right = (float)89.0; } } private void SetFooter(Document document, Color fontColor, string footerImageFile = null, string footerText = "Spire.Doc", string fontName = "Calibri", float fontSize = (float)12.0, HorizontalAlignment alignment = HorizontalAlignment.Left) { for (int i = 0; i < document.Sections.Count; i++) { Section section = document.Sections[i]; HeaderFooter footer = section.HeadersFooters.Footer; Paragraph footerParagraph = footer.AddParagraph(); TextRange text = footerParagraph.AppendText(footerText); text.CharacterFormat.FontName = fontName; text.CharacterFormat.FontSize = fontSize; text.CharacterFormat.TextColor = fontColor; footerParagraph.Format.HorizontalAlignment = alignment; //border footerParagraph.Format.Borders.Top.BorderType = (BorderStyle)Spire.Doc.Documents.BorderStyle.Hairline; footerParagraph.Format.Borders.Top.Space = (float)0.03; if (footerImageFile != null) { Byte[] imagedata = System.IO.File.ReadAllBytes(footerImageFile); //insert picture to footer DocPicture footerPicture = footerParagraph.AppendPicture(imagedata); //footer picture layout footerPicture.TextWrappingStyle = TextWrappingStyle.Behind; footerPicture.HorizontalOrigin = HorizontalOrigin.Page; footerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left; footerPicture.VerticalOrigin = VerticalOrigin.Page; footerPicture.VerticalAlignment = ShapeVerticalAlignment.Bottom; } //insert page number footerParagraph = footer.AddParagraph(); footerParagraph.AppendField("page number", FieldType.FieldPage); footerParagraph.AppendText("of"); footerParagraph.AppendField("number of pages", FieldType.FieldNumPages); footerParagraph.Format.HorizontalAlignment = (HorizontalAlignment)HorizontalAlignment.Right; } } private void SetHeader(Document document, Color fontColor, string headerImageFile = null, string headerText = "E-iceblue", string fontName = "Calibri", float fontSize = (float)12.0, HorizontalAlignment alignment = HorizontalAlignment.Center) { for (int i = 0; i < document.Sections.Count; i++) { Section section = document.Sections[i]; HeaderFooter header = section.HeadersFooters.Header; //insert picture and text to header Paragraph headerParagraph = header.AddParagraph(); if (headerImageFile != null) { Byte[] imagedata = System.IO.File.ReadAllBytes(headerImageFile); DocPicture headerPicture = headerParagraph.AppendPicture(imagedata); //header picture layout - text wrapping headerPicture.TextWrappingStyle = TextWrappingStyle.Behind; //header picture layout - position headerPicture.HorizontalOrigin = HorizontalOrigin.Page; headerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left; headerPicture.VerticalOrigin = VerticalOrigin.Page; headerPicture.VerticalAlignment = ShapeVerticalAlignment.Top; } //header text TextRange text = headerParagraph.AppendText(headerText); text.CharacterFormat.FontName = fontName; text.CharacterFormat.FontSize = Convert.ToSingle(fontSize); text.CharacterFormat.TextColor = fontColor; headerParagraph.Format.HorizontalAlignment = alignment; //border headerParagraph.Format.Borders.Bottom.BorderType = (Spire.Doc.Documents.BorderStyle)Spire.Doc.Documents.BorderStyle.Hairline; headerParagraph.Format.Borders.Bottom.Space = (float)0.03; } } private Color ConvertStrToColor(string strColor) { if (String.IsNullOrWhiteSpace(strColor)) { return Color.Empty; } else { return ColorTranslator.FromHtml("#" + strColor); } } } }
No Matter How Big or Small Your Project is,
Any technical question related to our product, contact us at support@e-iceblue.com.
Any question related to the purchase of product, contact us at sales@e-iceblue.com.
If you don't find the function you want, please request a free demo from us.