Well, this is strange. I did as you suggested, and the problem persists.
Code:
// Create the initial file in MigraDoc
Document doc = new Document();
new TableMaker().makeTable(doc, planCols, optionCol, companyName); //this creates the tables and adds them to doc object
PdfDocumentRenderer rnd = new PdfDocumentRenderer(true);
rnd.Document = doc;
rnd.RenderDocument();
rnd.Save(filePath);
if (watermarkIsNeeded)
{
PdfDocument pdfIn = PdfReader.Open(filePath, PdfDocumentOpenMode.Import);
PdfDocument pdfOut = new PdfDocument();
for (int i = 0; i < pdfIn.PageCount; i++)
{
PdfPage pg = pdfIn.Pages[i];
pg = pdfOut.AddPage(pg);
string draftFlagStr = "DRAFT";
// Get an XGraphics object for drawing beneath the existing content
XGraphics gfx = XGraphics.FromPdfPage(pg, XGraphicsPdfPageOptions.Prepend);
// Get the size (in point) of the text
XFont font = new XFont("Verdana", 72, XFontStyle.Bold);
XSize size = gfx.MeasureString(draftFlagStr, font);
// Define a rotation transformation at the center of the page
gfx.TranslateTransform(pg.Width / 2, pg.Height / 2);
gfx.RotateTransform(-Math.Atan(pg.Height / pg.Width) * 180 / Math.PI);
gfx.TranslateTransform(-pg.Width / 2, -pg.Height / 2);
// Create a string format
XStringFormat format = new XStringFormat();
format.Alignment = XStringAlignment.Near;
format.LineAlignment = XLineAlignment.Near;
// Create a dimmed red brush
XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
// Draw the string
gfx.DrawString(draftFlagStr, font, brush,
new XPoint((pg.Width - size.Width) / 2, (pg.Height - size.Height) / 2),
format);
}
pdfOut.Save(filePath);
}
I can tell that the watermark
is being written, because it is visible on pages where the table does not take up the full page. But on pages where the table takes up more space (and thus the watermark should be superimposed over the table), the watermark is hidden by the table.
What the heck am I doing wrong?