PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Oct 25, 2014 10:04 pm

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Overlay image on table
PostPosted: Wed Nov 09, 2011 2:06 pm 
Offline

Joined: Wed Nov 09, 2011 11:39 am
Posts: 5
Hi

I'm using Migradoc version 1.31 and I want to be able to place an image over a table. I'm trying to achieve it by:

- Adding a TextFrame with wrapstyle through
- Adding a small spacer image
- Adding my Table to the TextFrame
- Adding a second TextFrame which will start at the same places as the last textframe
- Adding an icon Image to the second TextFrame, which will appear on top of the table

The issue with this method is that the first TextFrame does not grow to encompass of content inside it. Because of that, I can't set the height of the second TextFrame to match the first, and subsequent elements are added at about half way through the table.

Code is below, mostly irrelevant but rather too much than too little.

Code:
        private void testOverlap(object sender, EventArgs e)
        {
            Document document = new Document();
            DefineStyles(ref document);

            document.DefaultPageSetup.TopMargin = "10mm";
            document.DefaultPageSetup.LeftMargin = "10mm";
            document.DefaultPageSetup.RightMargin = "10mm";
            document.DefaultPageSetup.BottomMargin = "10mm";

            document.DefaultPageSetup.PageHeight = "297mm";
            document.DefaultPageSetup.PageWidth = "210mm";

            Section s1 = document.AddSection();
            s1.AddParagraph("Opening paragraph...");
            s1.AddParagraph("");
            TextFrame FirstTextFrame = s1.AddTextFrame();
            FirstTextFrame.WrapFormat.Style = WrapStyle.Through;
            FirstTextFrame.LineFormat.Width = "0.1mm";
            Image trans =  FirstTextFrame.AddImage("Z:\\Migradoc Testing\\Images\\transpix.png");
            trans.Height = "1mm";

            CreateFlightTable(ref FirstTextFrame);
           
            FirstTextFrame.AddParagraph("");


            TextFrame OverlappingTextFrame = s1.AddTextFrame();
            OverlappingTextFrame.Height = FirstTextFrame.Height;
            OverlappingTextFrame.LineFormat.Width = "0.1mm";
            OverlappingTextFrame.LineFormat.Color = Colors.Red;

            OverlappingTextFrame.MarginLeft = "2mm";
            OverlappingTextFrame.AddImage("Z:\\Migradoc Testing\\Images\\test_flight_logo.png");
            s1.AddParagraph("End paragraph.");

            //Render
            MigraDoc.Rendering.PdfDocumentRenderer pdfRenderer = new MigraDoc.Rendering.PdfDocumentRenderer(false, PdfSharp.Pdf.PdfFontEmbedding.Always);
            pdfRenderer.Document = document;
            pdfRenderer.RenderDocument();

            PdfSharp.Pdf.PdfDocument pdfdoc = pdfRenderer.PdfDocument;
            pdfdoc.ViewerPreferences.FitWindow = false;
            pdfdoc.PageLayout = PdfSharp.Pdf.PdfPageLayout.SinglePage;
            pdfdoc.PageMode = PdfSharp.Pdf.PdfPageMode.UseNone;

            string filename = "Z:\\scratch\\test.pdf";
            pdfdoc.Save(filename);
            System.Diagnostics.Process.Start(filename);
           
        }

        private void DefineStyles(ref Document doc)
        {
            Style style = doc.Styles["Normal"];
            style.Font.Name = "Arial";
            style.Font.Size = 8;

            style = doc.Styles["Heading1"];
            style.Font.Name = "Arial";
            style.Font.Bold = true;
            style.Font.Size = 10;
            style.Font.Color = Colors.Black;

            style = doc.Styles["Heading2"];
            style.Font.Name = "Arial";
            style.Font.Bold = true;
            style.Font.Size = 9;
            style.Font.Color = Colors.Black;

            style = doc.Styles["Heading3"];
            style.Font.Name = "Arial";
            style.Font.Bold = true;
            style.Font.Size = 10;
            style.Font.Color = Colors.White;

            style = doc.Styles["Heading4"];
            style.Font.Name = "Arial";
            style.Font.Bold = true;
            style.Font.Size = 9;
            style.Font.Color = Colors.White;

           
        }

        private void CreateFlightTable(ref TextFrame Obj)
        {
           
            MigraDoc.DocumentObjectModel.Tables.Table ftable = Obj.AddTable();
            ftable.LeftPadding = "0mm";
            ftable.RightPadding = "0mm";
            ftable.TopPadding = "0mm";
            ftable.BottomPadding = "0mm";


            //columns 6
            MigraDoc.DocumentObjectModel.Tables.Column c = ftable.AddColumn("50mm"); //from airport
            c.Format.Alignment = ParagraphAlignment.Left;
            c.Format.LeftIndent = "1mm";
            c.Format.RightIndent = "1mm";

            c = ftable.AddColumn("50mm"); //to airport
            c.Format.Alignment = ParagraphAlignment.Left;
            c.Format.LeftIndent = "1mm";
            c.Format.RightIndent = "1mm";

            c = ftable.AddColumn("20mm"); // dep date
            c.Format.Alignment = ParagraphAlignment.Left;
            c.Format.LeftIndent = "1mm";
            c.Format.RightIndent = "1mm";

            c = ftable.AddColumn("20mm"); //dep time
            c.Format.Alignment = ParagraphAlignment.Left;
            c.Format.LeftIndent = "1mm";
            c.Format.RightIndent = "1mm";

            c = ftable.AddColumn("20mm"); //arr time
            c.Format.Alignment = ParagraphAlignment.Left;
            c.Format.LeftIndent = "1mm";
            c.Format.RightIndent = "1mm";

            c = ftable.AddColumn("30mm"); //flight number
            c.Format.Alignment = ParagraphAlignment.Left;
            c.Format.LeftIndent = "1mm";
            c.Format.RightIndent = "1mm";


            //First row is a header row, black background

            MigraDoc.DocumentObjectModel.Tables.Row r = ftable.AddRow();
            r.HeadingFormat = false;
            r.Format.Alignment = ParagraphAlignment.Left;
            //r.Format.Font.Bold = true;
            r.VerticalAlignment = MigraDoc.DocumentObjectModel.Tables.VerticalAlignment.Center;
            //r.Height = "6mm";
            r.Style = "Heading3";
            r.BottomPadding = "0.2mm";
            r.TopPadding = "0.2mm";
            Color org = new Color(0, 60, 100, 0);
            r.Shading.Color = Color.FromCmyk(0, 60, 100, 0);
            Paragraph p = r.Cells[0].AddParagraph();
            p.AddTab();
            p.AddText("Flight Details");

            r.Cells[0].MergeRight = 5;

            //next row is the column headers
            r = ftable.AddRow();

            r.HeadingFormat = false;
            r.Format.Alignment = ParagraphAlignment.Left;
            r.Format.Font.Bold = true;
            r.TopPadding = "0.2mm";
            r.BottomPadding = "0.2mm";
            r.Cells[0].AddParagraph("Departure Airport");
            r.Cells[1].AddParagraph("Arrival Airport");
            r.Cells[2].AddParagraph("Date");
            r.Cells[3].AddParagraph("Depart");
            r.Cells[4].AddParagraph("Arrive");
            r.Cells[5].AddParagraph("Flight Number");

            r = ftable.AddRow();
            r.TopPadding = "0.2mm";
            r.BottomPadding = "0.2mm";
            r.Cells[0].AddParagraph("London Gatwick");
            r.Cells[1].AddParagraph("Sharm el Sheikh, Egypt");
            r.Cells[2].AddParagraph("24/05/2012");
            r.Cells[3].AddParagraph("11:30");
            r.Cells[4].AddParagraph("17:10");
            r.Cells[5].AddParagraph("EZY5366");

            r = ftable.AddRow();
            r.TopPadding = "0.2mm";
            r.BottomPadding = "0.2mm";
            r.Cells[0].AddParagraph("Sharm el Sheikh, Egypt");
            r.Cells[1].AddParagraph("London Gatwick");
            r.Cells[2].AddParagraph("02/06/2012");
            r.Cells[3].AddParagraph("16:30");
            r.Cells[4].AddParagraph("22:10");
            r.Cells[5].AddParagraph("EZY5367");

            //add spacer to before hte flight blurb
            r = ftable.AddRow();
            r.TopPadding = "0.2mm";
            r.BottomPadding = "0.2mm";

            //add the flight blurb
            r = ftable.AddRow();
            r.TopPadding = "0.2mm";
            r.BottomPadding = "0.2mm";
            r.Format.Alignment = ParagraphAlignment.Justify;
            r.Cells[0].MergeRight = 5;
            r.Cells[0].AddParagraph("All the flight times are based on local time, flight timings and airling details are subject to change. " +
                "We recommend you check-in for your flight two hours before your scheduled departure time. The desks close promptly, so make " +
                "sure you've completed check-in at least an hour before take-off.");

            //add spacer to before hte flight options header
            r = ftable.AddRow();
            r.TopPadding = "0.2mm";
            r.BottomPadding = "0.2mm";

            //add flight options header
            r = ftable.AddRow();

            r.TopPadding = "0.2mm";
            r.BottomPadding = "0.2mm";
            r.HeadingFormat = true;
            r.Format.Alignment = ParagraphAlignment.Left;
            //r.Format.Font.Bold = true;
            r.VerticalAlignment = MigraDoc.DocumentObjectModel.Tables.VerticalAlignment.Center;
            //r.Height = "6mm";
            r.Style = "Heading4";
            r.Shading.Color = Color.FromCmyk(0, 60, 100, 0);
            p = r.Cells[0].AddParagraph();
            p.AddTab();
            p.AddText("Pre-booked Flight Options:");
            r.Cells[0].MergeRight = 5;

            //add small spacer
            r = ftable.AddRow();
            r.TopPadding = "0.2mm";
            r.BottomPadding = "0.2mm";
            r.Height = "2mm";


            //add flight options
            r = ftable.AddRow();
            r.TopPadding = "0.2mm";
            r.BottomPadding = "0.2mm";
            r.Cells[0].MergeRight = 5;
            r.Cells[0].AddParagraph("In-flight meals: None");
            r.Cells[0].AddParagraph("Extra leg-room: None");
            r.Cells[0].AddParagraph("Speedy boarding: None");

            //add small spacer
            r = ftable.AddRow();
            r.TopPadding = "0.2mm";
            r.BottomPadding = "0.2mm";


            ftable.SetEdge(0, 1, ftable.Columns.Count, ftable.Rows.Count - 1, MigraDoc.DocumentObjectModel.Tables.Edge.Box, MigraDoc.DocumentObjectModel.BorderStyle.Single, "0.1mm", Color.FromCmyk(0, 60, 100, 0));

        }



It ends up looking like this - I bordered the text frames so you can see them.

Image

I am open to alternative methods, like just drawing the image absolutely just above the table, if I could a) determine the position of the table and b) draw the image there, but I can't seem to figure that out in Migradoc.

Can you help please?

Thank you.

Lee


Top
 Profile  
 
PostPosted: Wed Nov 09, 2011 3:42 pm 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2144
Location: Cologne, Germany
You can set wrapstyle through for the image, so no need to put the image into a textframe.

You can add images to table cells, so maybe you can do it all just with a table and an image, without textframes and wrapstyle through.
You can create "irregular tables" by using MergeRight and MergeDown.

Is the plane at the right location on your screen shot? You could add a small column for the icon and use MergeRight=1 in the second row to have text that spreads across two columns.
I would use a single table, maybe with one or two extra columns - but neither textframes nor wrapstyle through to achieve this.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
 
PostPosted: Wed Nov 09, 2011 5:43 pm 
Offline

Joined: Wed Nov 09, 2011 11:39 am
Posts: 5
Hi Thomas

Thanks for coming back to me so quickly.

Quote:
You can set wrapstyle through for the image, so no need to put the image into a textframe.


On that point, the image would have been under the table so would not have fully appeared. Is there a way to make something 'TopMost' so that it is still fully visible after using wrapstyle through?

Quote:
You can create "irregular tables" by using MergeRight and MergeDown.


I had thought of that previously, but wanted to have the image clearly floating above the table, or at least look that way, and I don't think that can be achieved by merging the cells.

But I've done it anyway - I think it looks OK:

Image

Couple of white space issues at the sides but I think we can address them by fiddling with the image and making it fit snugly into the cell.

So there is no way to get the top left position of a table, and draw an image at or near that spot? If not, I'll use this solution as it is probably good enough, especially as it can be tweaked.

I love the product by the way, really cool.

Thank you Thomas

Lee.


Top
 Profile  
 
PostPosted: Thu Nov 10, 2011 8:27 am 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2144
Location: Cologne, Germany
It should be possible to add the image to a table cell and still set wrapstyle through. The anchor can be set to paragraph and character (that should be the current drawing position in the cell) and then set the position of the image relative to that point (using positive or negative values).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
 
PostPosted: Thu Nov 10, 2011 1:16 pm 
Offline

Joined: Wed Nov 09, 2011 11:39 am
Posts: 5
Hi Thomas

OK, I'm trying this angle but I don't know how to move the image relative to the table/cell. The code for this first row is now:

Code:
            MigraDoc.DocumentObjectModel.Tables.Row r = ftable.AddRow();
            Image icn = r.Cells[0].AddImage("Z:\\Migradoc Testing\\Images\\flight-details5.png");
           
            icn.WrapFormat.Style = WrapStyle.Through;
            //*******
            //need to tweak the position of the image here somehow, slightly right, and slightly up
            //*******
            r.HeadingFormat = false;
            r.Format.Alignment = ParagraphAlignment.Left;
            r.VerticalAlignment = MigraDoc.DocumentObjectModel.Tables.VerticalAlignment.Center;
            r.Style = "Heading3";
            r.BottomPadding = "0.2mm";
            r.TopPadding = "0.2mm";
            Color org = new Color(0, 60, 100, 0);
            r.Shading.Color = Color.FromCmyk(0, 60, 100, 0);
            Paragraph p = r.Cells[0].AddParagraph();
            p.AddTab();
            p.AddText("Flight Details");

            r.Cells[0].MergeRight = 5;


The output is this:

Image

Presumably after setting the wrapstyle in the above code, I need to set the position but I can't work out how to do it. The .Top.Position and .Left.Position values are read only. I need to squeeze it right and up a bit.

Regards

Lee


Top
 Profile  
 
PostPosted: Thu Nov 10, 2011 1:34 pm 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2144
Location: Cologne, Germany
Untested code:
Code:
icn.WrapFormat.Style = WrapStyle.Through;
icn.RelativeHorizontal = RelativeHorizontal.Character;
icn.RelativeVertical = RelativeVertical.Line;
icn.Left = Unit.FromMillimeter(5);
icn.Top = Unit.FromMillimeter(-5);

It does compile, Left and Top have setters.

Edit: Warning: The code does not work as intended.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
 
PostPosted: Thu Nov 10, 2011 2:00 pm 
Offline

Joined: Wed Nov 09, 2011 11:39 am
Posts: 5
Hi Thomas

I'm afraid the image doesn't move in that instance. If I remove the wrapstyle, I am able to make the image move down (positive top) but not up (negative top), and the Left position setting doesn't have any effect at all. I guess I cannot move the image outside the limits of the cell to which it was added.

Should I stick to the irregular tables approach?

Lee


Top
 Profile  
 
PostPosted: Thu Nov 10, 2011 2:35 pm 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2144
Location: Cologne, Germany
theleeski wrote:
Should I stick to the irregular tables approach?
Yeah, never change a running system.

Here's something I tried successfully (MDDDL, not C#):
Code:
      RelativeHorizontal = Page
      RelativeVertical = Page
      WrapFormat
      {
        Style = None
        DistanceTop = "4.5cm"
        DistanceLeft = "2.5cm"
      }


So if your table has a fixed position on the page, then you can use code like that to set the absolute position of the image. This won't work if your table can move on the page.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
 
PostPosted: Thu Nov 10, 2011 2:48 pm 
Offline

Joined: Wed Nov 09, 2011 11:39 am
Posts: 5
Quote:
Yeah, never change a running system.


It's not quite running yet, still fairly early stages. :)

Quote:
So if your table has a fixed position on the page...


The tables do move, and vary in size, so I think that is out.

We'll continue to fiddle with the columns, rows and image sizes to get the best effect we can.

Thank you for your efforts, Thomas.

Lee


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

All times are UTC


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 10 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:  
cron
Powered by phpBB® Forum Software © phpBB Group