How to set page border for all pages in a document

Hi Team,

I want to set up an HTML or PNG image for the page border for all pages.

I have to add content within the border. Please assist me.

The expected format as this image →

Thanks,
Krishna.S

@krishnass Generally you can use PageSetup.Borders to set page border. For example see the following code:

Document doc = new Document();
doc.FirstSection.PageSetup.Borders.Color = Color.Red;
doc.FirstSection.PageSetup.Borders.LineWidth = 3;
doc.Save(@"C:\Temp\out.docx");

In your case if it is required to use an image as a background shape you can insert the shape into the document header. For example see the following code:

HeaderFooterType[] headers = new HeaderFooterType[] { HeaderFooterType.HeaderFirst, HeaderFooterType.HeaderPrimary, HeaderFooterType.HeaderEven };

Document doc = new Document(@"C:\Temp\in.docx");
foreach (Section s in doc.Sections)
{
    // Create shape.
    Shape shape = new Shape(doc, ShapeType.Image);
    shape.ImageData.SetImage(@"C:\Temp\aspose_page_border.png");
    shape.AspectRatioLocked = false;
    shape.Width = s.PageSetup.PageWidth;
    shape.Height = s.PageSetup.PageHeight;
    shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    shape.VerticalAlignment = VerticalAlignment.Center;
    shape.HorizontalAlignment = HorizontalAlignment.Center;
    shape.WrapType = WrapType.None;
    shape.BehindText = true;

    // Put the shape ino the sections headers
    foreach (HeaderFooterType hft in headers)
    {
        HeaderFooter hf = s.HeadersFooters[hft];
        if (hf == null)
        {
            hf = new HeaderFooter(doc, hft);
            hf.AppendChild(new Paragraph(doc));
            s.HeadersFooters.Add(hf);
        }
        hf.LastParagraph.AppendChild(shape.Clone(true));
    }
}

doc.Save(@"C:\Temp\out.docx");

Hi @alexey.noskov - thanks for the reply.

This response has a coding format that supports .net. Would you kindly assist me with the code flow for the Java words library?

@krishnass Java code is pretty much the same.
Here is code to set page border:

Document doc = new Document();
doc.getFirstSection().getPageSetup().getBorders().setColor(Color.RED);
doc.getFirstSection().getPageSetup().getBorders().setLineWidth(3);
doc.save("C:\\Temp\\out.docx");

Here is code to insert shape in the background:

int[] headers = new int[] { HeaderFooterType.HEADER_FIRST, HeaderFooterType.HEADER_PRIMARY, HeaderFooterType.HEADER_EVEN };

Document doc = new Document("C:\\Temp\\in.docx");
for (Section s : doc.getSections())
{
    // Create shape.
    Shape shape = new Shape(doc, ShapeType.IMAGE);
    shape.getImageData().setImage("C:\\\Temp\\\aspose_page_border.png");
    shape.setAspectRatioLocked(false);
    shape.setWidth(s.getPageSetup().getPageWidth());
    shape.setHeight(s.getPageSetup().getPageHeight());
    shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
    shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
    shape.setVerticalAlignment(VerticalAlignment.CENTER);
    shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
    shape.setWrapType(WrapType.NONE);
    shape.setBehindText(true);

    // Put the shape ino the sections headers
    for (int hft : headers)
    {
        HeaderFooter hf = s.getHeadersFooters().getByHeaderFooterType(hft);
        if (hf == null)
        {
            hf = new HeaderFooter(doc, hft);
            hf.appendChild(new Paragraph(doc));
            s.getHeadersFooters().add(hf);
        }
        hf.getLastParagraph().appendChild(shape.deepClone(true));
    }
}

doc.save("C:\\Temp\\out.docx");

@alexey.noskov - Really Thank you so much. Its working fine.

But Back ground image showing as dull shade not bright. How to display same as image? Is there any libraries there? Please let me know.

Please check this image -

Thanks,
Krishna.S

@krishnass This is the same image. MS Word simply shows header content such way. If you print or convert document to PDF the image will be shown normally.

Everything working fine in word format, I am trying to save final document(aspose lib) with PDF format. outer border or outer image not populated. Please help me.

response.setHeader("content-disposition",
						MessageFormat.format("attachment; filename=\"{0}.{1}\"", test, "pdf"));
response.setContentType("application/pdf");
document.save(response.getOutputStream(), SaveFormat.PDF);

@krishnass Could you please attach your input and output documents along with code that will allow us to reproduce the problem? We will check the issue and provide you more information.

@alexey.noskov , Please find below sample code and advise me ASAP.

package com.asposetest;

import java.text.MessageFormat;

import com.aspose.cells.Color;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.Orientation;
import com.aspose.words.ParagraphAlignment;
import com.aspose.words.ParagraphFormat;
import com.aspose.words.SaveFormat;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

class Test {

	public void setAsposeData(HttpServletRequest request, HttpServletResponse response) {
		Document document = new Document();
		DocumentBuilder builder = new DocumentBuilder(document);
		ParagraphFormat paragraphFormat = builder.getParagraphFormat();
		paragraphFormat.setAlignment(ParagraphAlignment.CENTER);
		builder.getPageSetup().setOrientation(Orientation.PORTRAIT);

		String fontName = "Arial";
		String fontColour = "Orange";
		Double lineheight = 100.0;

		String pageTitle = "Aspose Test";

		pageTitle = MessageFormat.format(
				"<p style=\"margin:0px;font-weight: bold; font-family:{0}; font-size:{1}px; color:{2}; line-height:{3}%;\">{4}</p>",
				fontName, 22, fontColour, lineheight, pageTitle);
		builder.insertHtml(pageTitle, true);

		String pageContent = "XXXXXXXX";

		pageContent = MessageFormat.format(
				"<p style=\"margin:0px;font-weight: bold; font-family:{0}; font-size:{1}px; color:{2}; line-height:{3}%;\">{4}</p>",
				fontName, 22, fontColour, lineheight, pageContent);
		builder.insertHtml(pageContent, true);

		document.getFirstSection().getPageSetup().getBorders().setColor(Color.RED);
		document.getFirstSection().getPageSetup().getBorders().setLineWidth(3);

		String fileName = "aspose_test";

		response.setHeader("content-disposition",
				MessageFormat.format("attachment; filename=\"{0}.{1}\"", fileName, "pdf"));
		response.setContentType("application/pdf");
		document.save(response.getOutputStream(), SaveFormat.PDF);

	}
}

@krishnass Thank you for additional information. Unfortunately, I cannot reproduce the problem on my side. Here is the output PDF produced on my side using your code:
out.pdf (14.0 KB)

Really thanks for all the clarifications. I have identified the document issue. Now its working properly.

Thank you so much @alexey.noskov

1 Like