Search code examples
javakotlinasposeaspose.words

Set different backgrounds in the document using Aspose


I wanted to set different background in my document with the following conditions

  1. The first page of the document should contain the first.png as a background.
  2. The remaining pages of the document should contain a second.png as a background.

I'm able to set backgrounds as per my expectations if the first page of the input document doesn't contain an image in the header. If the document's first page contains an image in the header it is removing that header on the first page only.

I'm using the following code snippet to achieve my functionality.

val headerPrimary = document.firstSection.headersFooters[HEADER_PRIMARY]
if (!document.firstSection.pageSetup.differentFirstPageHeaderFooter && headerPrimary != null) {
    // If so create first page header and copy content of the primary header into it.
    val headerFirst = HeaderFooter(document, HEADER_FIRST);
    headerPrimary.childNodes.forEach {
        headerFirst.appendChild(it.deepClone(true));
    }
    // Put the first page header into the document.
    document.firstSection.headersFooters.add(headerFirst);
}

val builder = DocumentBuilder(document)
document.firstSection.pageSetup.differentFirstPageHeaderFooter = true

builder.moveToHeaderFooter(HEADER_FIRST)
setImageInBackground(builder, firstImageDataString)

builder.moveToHeaderFooter(HEADER_PRIMARY)
setImageInBackground(builder, SecondImageDataString)

private fun setImageInBackground(builder: DocumentBuilder, imageString: String) {
    val background = builder.insertImage(imageString)
    background.wrapType = NONE
    background.behindText = true
    background.relativeHorizontalPosition = RelativeHorizontalPosition.PAGE
    background.relativeVerticalPosition = RelativeVerticalPosition.PAGE
    background.left = (builder.pageSetup.pageWidth - background.width) / 2
    background.top = (builder.pageSetup.pageHeight - background.height) / 2
}

For input-document/expected-result and images, I've uploaded an attachment in the response forum Aspose forum link

For Input-Output of the code: Files.zip


Solution

  • The problem occurs because your input document contains only the primary header. When you move the DocumentBuilder’s cursor into the first page header, Aspose.Words create new empty first page header. To keep the content of the primary header on the first page you should simply copy the content of the primary header into the newly created first page header. Here is a simple code that does this.

    // Check whether there is only primary header.
    HeaderFooter headerPrimary = (HeaderFooter)document.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary];
    if (!document.FirstSection.PageSetup.DifferentFirstPageHeaderFooter
        && headerPrimary != null)
    {
        // If so create first page header and copy content of the primary header into it.
        HeaderFooter headerFirst = new HeaderFooter(document, HeaderFooterType.HeaderFirst);
        foreach (Node primaryHeaderChild in headerPrimary.ChildNodes)
            headerFirst.AppendChild(primaryHeaderChild.Clone(true));
    
        // Put the first page header into the document.
        document.FirstSection.HeadersFooters.Add(headerFirst);
    }
    
    // Your original code goes after.
    DocumentBuilder builder = new DocumentBuilder(document);
    document.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = true;
    
    builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
    SetImageInBackground(builder, firstImageDataString);
    
    builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
    SetImageInBackground(builder, secondImageDataString);