Question

I am generating a PDF file from a C# app using MigraDoc /PDFsharp I am trying to figure out if I can have the entire page color black (instead of the default white). I see how I can change styles for font, etc but I don't see where I can change the background color of the entire page.

Please let me know if this is possible. I see that I can have background images (so I guess I can create a solid color image but figured there would be an easier way. I can't find anything after lots of googling)

Was it helpful?

Solution

I can think of two ways I'd consider worth trying.

One way is using PDFsharp to call AddPage() for each new page and draw a rectangle in the color you like before calling RenderPage() to draw the MigraDoc content.

Here's a sample that draws nine MigraDoc pages in thumbnail size on a single PDFsharp page:
http://www.pdfsharp.net/wiki/MixMigraDocAndPdfSharp-sample.ashx
You would call AddPage/RenderPage to draw each page in full size.

Another way that uses MigraDoc only: add a TextFrame() to the page header and set the FillFormat of that TextFrame to your background color. Set the TextFrame to fill the whole page.
This should work and it requires only MigraDoc (no PDFsharp code required).

I think I'd use the first method.

OTHER TIPS

An example - create document, add a page, fill the entire page with black color and save the document.

using(PdfDocument doc = new PdfDocument()){
    PdfPage page = doc.AddPage();
    using(XGraphics gfx = XGraphics.FromPdfPage(page)){
        gfx.DrawRectangle(XBrushes.Black, new XRect(0, 0, page.Width.Point, page.Height.Point));
    }
    doc.Save("pdfDocument.pdf");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top