Add custom text or image in Header/Footer of Word Document with Aspose.Words Cloud

I am using node js api and i want to add the custom header and footer. for example in the header i want to add an image and some text and in the footer i want to add some text.

i can see only one cloud api (/words/#!/HeadersFooters/PutHeaderFooter) for header and footer. but its not working for me.

Can you please guide me how to add the text or image into header and footer using cloud api.

@arunkamboj

Thanks for your inquiry. To add text to the Header/Footer you need to use PutParagraph API and for image, you can use PutDrawingObject API. Hopefully it will help you to accomplish the task. Please let us know of any further assistance in this regard.

PutParagraph Nodejs Example
PutDrawingObject Nodejs Example

@tilal.ahmad But it will create the text/image in starting and end of the page.
Don’t we need to create the header and footer before using these api? If need then please provide me api for that.

Also if we want to move the text/image to particular position in the header then how can we move.

@arunkamboj

Yes, first you need to use the PutHeaderFooter API to add Header/Footer in your document and then above mentioned APIs to add Text/Image to child nodes of Header/Footer.

I am checking this requirement and will share the details with you soon.

Now i am able to add the paragraph using given api but the problem is that header text is not displaying by default. i have to enable the “Different first page” property from the “header & footer tools” after that it show me header on document.

I am using following api for create the header
words/#!/HeadersFooters/PutHeaderFooter

parameters

name : This is test document.docx
headerFooterType: “HeaderFirst”

also i think there is wrong information provided by you in document for the headerFooterType

when i used these value then it gives me error. (for example values for headerfooterType is FirstHeader, OddHeaders,EvenHeaders etc)

Also how can i add the image in the header bar, i have tried to use the putDrawing api in php but its not showing me image there.

$remoteName = “smiley_text_with_header.docx”;
$image = realpath(DIR . ‘/…’) . ‘/docs/footerlogo.png’;
$folder = null; // Input file exists at the root of the storage
$nodePath = “sections/0/headersfooters/0”;
$request = new Requests\PutDrawingObjectRequest($remoteName, “{"Left": 0}”,$image, $folder,
null, null, null, null,
null, null, $nodePath);
$result = $this->wordsApi->putDrawingObject($request);

Please guide me for this

@arunkamboj

We are sorry for your inconvenience. We have updated the documentation for correct values.

Please note you need to pass paragraph path as nodePath and Drawing Object parameters for required formatting. Please try these values, it will help you to resolve the issue.

nodepath: “sections/0/headersfooters/0/paragraphs/0”
DrawingObject parameter: { “RelativeHorizontalPosition”: “Page”, “Left”: 500, “RelativeVerticalPosition”: “Page”, “Top”: 10, “Width”: 50, “Height”: 50, “WrapType”: “None”}

Furthermore please check the details of DrawingObect resource properties for reference.

Property Name Type Description
Width double width of drawing object
Height double height of drawing object
Left double position of the left edge of the containing block of the drawing object
RelativeHorizontalPosition RelativeHorizontalPosition Specifies to what the horizontal position of a shape or text frame is relative. See possible values and more info at .NET API Reference
Top double position of the top edge of the containing block of the drawing object
RelativeVerticalPosition RelativeVerticalPosition Specifies to what the vertical position of a shape or text frame is relative. See possible values and more info at .NET API Reference
WrapType WrapType Specifies how text is wrapped around a shape or picture. See possible values and more info at .NET API Reference
imageDataLink link link to embedded image resource, if present
oleDataLink link link to embedded OLE resource, if present

@arunkamboj

Above, we have described how to format image in the Header/Footer of the Word Document.

For Text formatting, you may consider PostDocumentParagraphRunFont API.

Here is an example:
name = SampleDocument.docx
fontDto = {
“AllCaps”: false,
“Bidi”: false,
“Bold”: true,
“BoldBi”: false,
“Border”: {
“LineStyle”: “None”,
“LineWidth”: 0,
“Color”: {
@Web”: “”,
@Alpha”: 0
},
“DistanceFromText”: 0,
“Shadow”: false
},
“Color”: {
@Web”: “”,
@Alpha”: 0
},
“ComplexScript”: false,
“DoubleStrikeThrough”: false,
“Emboss”: false,
“Engrave”: false,
“Hidden”: false,
“HighlightColor”: {
@Web”: “”,
@Alpha”: 0
},
“Italic”: true,
“ItalicBi”: false,
“Kerning”: 0,
“LocaleId”: 1033,
“LocaleIdBi”: 1025,
“LocaleIdFarEast”: 1033,
“Name”: “Calibri”,
“NameAscii”: “Calibri”,
“NameBi”: “Times New Roman”,
“NameFarEast”: “Calibri”,
“NameOther”: “Calibri”,
“NoProofing”: false,
“Outline”: false,
“Position”: 0,
“Scaling”: 100,
“Shadow”: false,
“Size”: 11,
“SizeBi”: 11,
“SmallCaps”: false,
“Spacing”: 0,
“StrikeThrough”: false,
“StyleIdentifier”: “DefaultParagraphFont”,
“StyleName”: “Default Paragraph Font”,
“Subscript”: false,
“Superscript”: false,
“TextEffect”: “None”,
“Underline”: “Dash”,
“UnderlineColor”: {
@Web”: “#A5A5A5
}
}
paragraphPath = sections/0/headersfooters/0/paragraphs/0
index = 0

Following is the description of Font object attributes:

Property Name Type Description
AllCaps bool True if the font is formatted as all capital letters.
Bidi bool Specifies whether the contents of this run shall have right-to-left characteristics.
Bold bool True if the font is formatted as bold.
BoldBi bool True if the right-to-left text is formatted as bold.
Border Border Border object that specifies border for the font.
Color Color Gets or sets the color of the font.
ComplexScript bool Specifies whether the contents of this run shall be treated as complex script text regardless of their Unicode character values when determining the formatting for this run.
DoubleStrikeThrough bool True if the font is formatted as double strikethrough text.
Emboss bool True if the font is formatted as embossed.
Engrave bool True if the font is formatted as engraved.
Hidden bool True if the font is formatted as hidden text.
HighlightColor Color Gets or sets the highlight (marker) color.
Italic bool True if the font is formatted as italic.
ItalicBi bool True if the right-to-left text is formatted as italic.
Kerning double Gets or sets the font size at which kerning starts.
LocaleId int Gets or sets the locale identifier (language) of the formatted characters. For the list of locale identifiers see http://www.microsoft.com/globaldev/reference/lcid-all.mspx
LocaleIdBi int Gets or sets the locale identifier (language) of the formatted right-to-left characters. For the list of locale identifiers see http://www.microsoft.com/globaldev/reference/lcid-all.mspx
LocaleIdFarEast int Gets or sets the locale identifier (language) of the formatted Asian characters. For the list of locale identifiers see http://www.microsoft.com/globaldev/reference/lcid-all.mspx
Name string Gets or sets the name of the font.
NameAscii bool Returns or sets the font used for Latin text (characters with character codes from 0 (zero) through 127).
NameBi bool TReturns or sets the name of the font in a right-to-left language document.
NameFarEast bool Returns or sets an East Asian font name.
NameOther bool Returns or sets the font used for characters with character codes from 128 through 255.
NoProofing bool True when the formatted characters are not to be spell checked.
Outline bool True if the font is formatted as outline.
Position double Gets or sets the position of text (in points) relative to the base line. A positive number raises the text, and a negative number lowers it.
Scaling int Gets or sets character width scaling in percent.
Shadow bool True if the font is formatted as shadowed.
Size double Gets or sets the font size in points.
SizeBi double Gets or sets the font size in points used in a right-to-left document.
SmallCaps bool True if the font is formatted as small capital letters.
Spacing double Returns or sets the spacing (in points) between characters.
StrikeThrough bool True if the font is formatted as strikethrough text.
StyleIdentifier StyleIdentifier Gets or sets the locale independent style identifier of the character style applied to this formatting.
StyleName string Gets or sets the name of the character style applied to this formatting.
Subscript bool True if the font is formatted as subscript.
Superscript bool True if the font is formatted as superscript.
TextEffect TextEffect Gets or sets the font animation effect.
Underline Underline Gets or sets the type of underline applied to the font.
UnderlineColor Color Gets or sets the color of the underline applied to the font.

@tilal.ahmad

Can we add header and footer with single api or we need to send separate api for adding header and footer?

for example currently i send 2 api for adding the header and footer

public function addHeader($fileName) {
$remoteName = $fileName;
$folder = null; // Input file exists at the root of the storage
$headerType = “HeaderPrimary”;
$request = new Requests\PutHeaderFooterRequest($remoteName, $headerType, $folder,
null, null, null, null,
null, null, “”);
$result = $this->wordsApi->putHeaderFooter($request);
$this->addFooter($remoteName);
}

    public function addFooter($fileName)
    {
        $remoteName = $fileName;
        $folder = null; // Input file exists at the root of the storage
        $footerType = "FooterPrimary";
        $request = new Requests\PutHeaderFooterRequest($remoteName, $footerType, $folder,
        null, null, null, null,
        null, null, "");
         $result = $this->wordsApi->putHeaderFooter($request);
         $this->addHeaderText($remoteName);
    }

@arunkamboj,

Thanks for your inquiry. I am afraid you can not add Header and Footer with a single API call, you have to call API twice for adding a header and a footer in the document.