How do I change the background on a existing PDF page?

Q:

I need to change the background color of a document that I am
creating.

I tried adding a rectangle the size of the page media box and changing
that rectangles color to make the background color but so far it hasnt
worked. Is this the appropriate way to do this?

pdftron.PDF.Element backgroundElement = builder.CreateRect(0, 0,
page.GetMediaBox().Width(), page.GetMediaBox().Height());

backgroundElement.GetGState().SetFillColorSpace
      (pdftron.PDF.ColorSpace.CreateDeviceRGB ());
backgroundElement.GetGState().SetStrokeColorSpace
     (pdftron.PDF.ColorSpace.CreateDeviceRGB());

backgroundElement.GetGState().SetStrokeColor(new
pdftron.PDF.ColorPt(0, 0, 1));
backgroundElement.GetGState ().SetFillColor(new pdftron.PDF.ColorPt(0,
0, 1));
writer.WritePlacedElement(backgroundElement);
----
A:

You are on the right track. You also need to specify that the
rectangle should be filled (a rectangle can be either filled, stroked,
stroked, or some kind of combination). So the code would look as
follows:

pdftron.PDF.Element backgroundElement = builder.CreateRect(0, 0,
page.GetMediaBox().Width(), page.GetMediaBox().Height());

backgroundElement.SetPathFill(true);
backgroundElement.SetPathStroke(true);
backgroundElement.SetPathClip(false); // ...
backgroundElement.GetGState().SetFillColorSpace(pdftron.PDF.ColorSpace.CreateDeviceRGB
());
backgroundElement.GetGState().SetStrokeColorSpace(pdftron.PDF.ColorSpace.CreateDeviceRGB());

backgroundElement.GetGState().SetStrokeColor(new
pdftron.PDF.ColorPt(0, 0, 1));
backgroundElement.GetGState ().SetFillColor(new pdftron.PDF.ColorPt(0,
0, 1));
writer.WritePlacedElement(backgroundElement);

Using PDFNet you could also directly write new page content in the
background/foreground layer. For example, you can use the following
code to add an image logo as background to an existing page:

writer.Begin(page, true, false);
pdftron.PDF.Image img = pdftron.PDF.Image.Create(doc, logoPath);
element = eb.CreateImage(img, pdftron.Common.Matrix2D(100, 0, 0, 50,
20, 50));
writer.WritePlacedElement(element);
writer.End();
...

Please keep in mind that if the page already has a background you will
need to remove the old background and replace it with a new background
rectangle.