PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 6:40 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Thu Mar 20, 2014 7:05 pm 
Offline

Joined: Thu Mar 20, 2014 5:51 pm
Posts: 4
I have an application that uses Migradoc to create a PDF with one or more tables from a set of data. There are page breaks between tables, and a table may also "overflow" to the next page. This works fine so far.

However, depending on the data I also need to add a "draft" flag to each page. For this I am adding an image to the header, which is meant to overlay the table like a watermark.

The problem is that the table always appears "in front", and I can't figure out how to make the image lay over the table.

Is there some equivalent of z-index from CSS that I can use?

My code looks basically like this:

Code:

Section sec = doc.AddSection();
sec.PageSetup.TopMargin = 5;
sec.PageSetup.RightMargin = 45;
sec.PageSetup.LeftMargin = 45;

if(draftFlagNeeded){
    Image draftFlag = sec.Headers.Primary.AddImage("path-to-file.png");
    draftFlag.Left = ShapePosition.Center;
    draftFlag.Top = ShapePosition.Top;
    draftFlag.RelativeVertical = RelativeVertical.Page;
    draftFlag.RelativeHorizontal = RelativeHorizontal.Page;
    draftFlag.WrapFormat.Style = WrapStyle.Through;
}

foreach(/*set of data to make into a table*/){
    Table tbl = sec.AddTable();
    tbl.Borders.Width = 0.25;
    tbl.Borders.Left.Width = 0.5;
    tbl.Borders.Right.Width = 0.5;
    tbl.Rows.LeftIndent = 0;
    tbl.KeepTogether = false;

    /*add columns and rows to table*/

    if(/*we still have more tables to write*/){
        sec.AddPageBreak();
    }

}



I am aware of the watermarking technique described in the samples, but I would prefer to stay in pure Migradoc territory if I can, since using that technique would require a substantial rewrite.


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 24, 2014 11:11 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Hi!

Try adding the image to the footer - this might work.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 24, 2014 4:59 pm 
Offline

Joined: Thu Mar 20, 2014 5:51 pm
Posts: 4
Hi Thomas,

That is a good idea, and I should have thought to try it before. Unfortunately, it did not work.


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 24, 2014 5:08 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
I'm sorry the footer trick doesn't work.
devzt wrote:
I am aware of the watermarking technique described in the samples, but I would prefer to stay in pure Migradoc territory if I can, since using that technique would require a substantial rewrite.
Not a big rewrite IMHO.
Use MigraDoc to create the PDF without watermark.

Then open the PDF file and run the PDFsharp sample code when you need the watermark.
The sample works with text, but using images is also possible.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 24, 2014 9:15 pm 
Offline

Joined: Thu Mar 20, 2014 5:51 pm
Posts: 4
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 25, 2014 8:29 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Well, "XGraphicsPdfPageOptions.Prepend" tells PDFsharp to add the new stuff below the old stuff.
"Append" is what you need for a real overlay - see variation 3.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 25, 2014 3:53 pm 
Offline

Joined: Thu Mar 20, 2014 5:51 pm
Posts: 4
Well, that'll teach me to copy-paste example code without paying closer attention to what it says. All this time I've had that "drawing beneath existing content" comment staring me in the face...

In any case, switching from Prepend to Append did the trick. Many thanks, Thomas.

I assume that this means there's not any way to do finely-grained order control when multiple layers are involved, so you need to be very careful about order of placement since you can only add graphics to the top or bottom of the stack (or replace them entirely). Is that correct?


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 25, 2014 2:02 pm 
Offline

Joined: Tue Apr 29, 2014 10:21 am
Posts: 27
I have used this method to do page numbering when the MigraDoc method didn't work for me.
Thanks :) 8)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 384 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group