I have been having trouble setting PDF page background.
Whenever I try to set a background for page, I get the following error message.
"Invalid index: index should be in the range [1..n] where n equals to the operators count."
You can see the exception message picture in the attachment
What does the message mean? I couldn't find out about it anywhere on the web or on the forum...
Hi Sung,
Thanks for your inquiry. Please check the following code snippet to set the page background color. You can either create a page object and set its background color or set the background property of any page index from the page collection. Hopefully, it will help you to accomplish your requirements.
//open document
Document pdfDocument = new Document();
//add a page in document
Page page = pdfDocument.Pages.Add();
set page background color
page.Background = Aspose.Pdf.Color.SandyBrown;
//Create text fragment
TextFragment textFragment = new TextFragment("Hello World");
textFragment.Position = new Position(100, 600);
//Set text properties
textFragment.TextState.FontSize = 12;
textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman");
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray);
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
//Create TextBuilder object
TextBuilder textBuilder = new TextBuilder(page);
//Append the text fragment to the PDF page
textBuilder.AppendText(textFragment);
pdfDocument111.Pages[1].Background =Aspose.Pdf.Color.RoyalBlue;
pdfDocument.Save(myDir + "pagebackgroud.pdf");
Best Regards,