We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

To add background color and border for Pdf page other than header and footer

Hi,

I am using Aspose Pdf for .net 3.5 framework. I am trying to add background color and border for Pdf page other than header and footer but I am not able to do it. Can you please help me on this?

Hi Ashokkumar,


Thanks for contacting support and sorry for the delayed response.

In order to set page background color, please try using following code lines.

[C#]

Aspose.Pdf.Document
pdfDocument = new Aspose.Pdf.Document();<o:p></o:p>

var page = pdfDocument.Pages.Insert(1);

page.Background = Aspose.Pdf.Color.Red;

pdfDocument.Save(“c:/pdftest/PageWithBackGround_Color.pdf”);



However concerning to requirement of adding page border, you can accomplish this requirement when creating PDF document from scratch using Aspose.Pdf.Generator namespace.

[C#]

Pdf pdf = new Pdf();<o:p></o:p>

Aspose.Pdf.Generator.Section sec1 = pdf.Sections.Add();

sec1.PageInfo.PageBorder = new ASPDF.Generator.BorderInfo((int)Aspose.Pdf.Generator.BorderSide.All);

sec1.PageInfo.PageBorder.SetBorderStyle((int)Aspose.Pdf.Generator.BorderSide.All, ASPDF.Generator.BorderStyle.Double);

sec1.Paragraphs.Add(new Aspose.Pdf.Generator.Text("Hello WOrld.."));

pdf.Save(“c:/pdftest/PDFwithBorder.pdf”);

Hi,
Thanks for the reply, I added header, body and footer in sec1(pdf page). I want to set background color and border for only body section but not header and footer part.

Can you look into the request.

Hi Ashokkumar,


In order to set the background color for body section only, you may consider adding FloatingBox object and set the background color for FloatingBox. Please note that FloatingBox is a collection of paragraphs so you can add subsequent elements (i.e. Text, Graph etc) inside this object and add FloatingBox to paragraphs collection of main document page. Please take a look over following code snippet.

In following approach, first we have created an instance of Pdf so that we can define border for PDF file and then we can instantiated Aspose.Pdf.Document object by passing Pdf object as an argument to its constructor so that we can perform further manipulation (addition of Floatingbox, background color etc). For your reference, I have also attached the resultant PDF generated over my end.

[C#]

Pdf pdf = new Pdf();<o:p></o:p>

Aspose.Pdf.Generator.Section sec1 = pdf.Sections.Add();

sec1.PageInfo.PageBorder = new Aspose.Pdf.Generator.BorderInfo((int)Aspose.Pdf.Generator.BorderSide.All);

sec1.PageInfo.PageBorder.SetBorderStyle((int)Aspose.Pdf.Generator.BorderSide.All, Aspose.Pdf.Generator.BorderStyle.Double);

// instantiate Document object with Pdf instance

Document document = new Document(pdf);

// create headerfooter object

Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();

header.Margin.Top = 5;

header.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Header Text..."));

document.Pages[1].Header = header;

int pageHeight = (int)document.Pages[1].CropBox.Height;

int pageWidth = (int)document.Pages[1].CropBox.Width;

// set floating box width equal to page width minus left marin

// -6 is the width of double border being used

Aspose.Pdf.FloatingBox fbox = new Aspose.Pdf.FloatingBox((pageWidth - (int)document.Pages[1].PageInfo.Margin.Left -6), (pageHeight - (int)document.Pages[1].PageInfo.Margin.Top -6));

// specify the left origin of FloatingBox

fbox.Left = -42;

fbox.Top = -33;

// background color for FloatingBox

fbox.BackgroundColor = Aspose.Pdf.Color.Yellow;

// background color for whole PDF file

document.Background= Aspose.Pdf.Color.LightBlue;

fbox.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Hello WOrld..in FloatingBox"));

document.Pages[1].Paragraphs.Add(fbox);

document.Save("c:/pdftest/PDFwithBorder.pdf");