PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Help with Mixing Paragraph text and tables
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2117
Page 1 of 1

Author:  csalernocsit [ Sun Aug 26, 2012 2:43 pm ]
Post subject:  Help with Mixing Paragraph text and tables

I'm new to these assemblies so pardon my ignorance.

I'm using MigraDoc in trying to display a paragraph then a table. There are three different paragraphs within the section and a table needs to follow each paragraph. The problem is that the tables are created and displayed after the last paragraph.

Thanks in advance for any\all help.

Code:
            DataParagraph.Append(CurrentBlock.BlockHeader, true, false);
            DataParagraph.Append(CurrentBlock.CoveragesText);
            DataParagraph.Append(CurrentBlock.Limits.Any() ? "Limits of not less than:" : " ");
           
            //DataParagraph.Append(CurrentBlock.LimitsText, CurrentBlock.Limits.Any());

            // Table Test Below


            MigraDoc.DocumentObjectModel.Shapes.TextFrame addressFrame;
            addressFrame = DataParagraph.Section.AddTextFrame();
            addressFrame.LineFormat.Width = 0.5; //Only for visual purposes
            addressFrame.Height = "2.5cm";//any number
            addressFrame.Width = "10.0cm";//sum of col widths
            addressFrame.Left = MigraDoc.DocumentObjectModel.Shapes.ShapePosition.Center;
            addressFrame.RelativeHorizontal = MigraDoc.DocumentObjectModel.Shapes.RelativeHorizontal.Margin;//irrelevant
            addressFrame.RelativeVertical = MigraDoc.DocumentObjectModel.Shapes.RelativeVertical.Line;
            MigraDoc.DocumentObjectModel.Shapes.TopPosition XTop = new MigraDoc.DocumentObjectModel.Shapes.TopPosition();

            addressFrame.Top = XTop.Position;
           

            //addressFrame.Top = "10.0cm";//irrelevant

            //addressFrame.RelativeVertical = MigraDoc.DocumentObjectModel.Shapes.RelativeVertical.Page;//irrelevant
           

            MigraDoc.DocumentObjectModel.Tables.Table DTable = addressFrame.AddTable();


            //MigraDoc.DocumentObjectModel.Tables.Table DTable = DataParagraph.Section.AddTable();
            //DTable.Format.Alignment = ParagraphAlignment.Center;
            DTable.Borders.Visible = false;
            DTable.Borders.ClearAll();
           
           

            MigraDoc.DocumentObjectModel.Tables.Cell cell = new MigraDoc.DocumentObjectModel.Tables.Cell();
            MigraDoc.DocumentObjectModel.Tables.Row row = new MigraDoc.DocumentObjectModel.Tables.Row();
         
            // Before you can add a row, you must define the columns
            //MigraDoc.DocumentObjectModel.Tables.Column column = DTable.AddColumn(MigraDoc.DocumentObjectModel.Unit.FromCentimeter(2));
            MigraDoc.DocumentObjectModel.Tables.Column column = new MigraDoc.DocumentObjectModel.Tables.Column();

            column = DTable.AddColumn();
            column.Borders.Visible = false;
            column.Width = "5cm";
            column.Format.Alignment = MigraDoc.DocumentObjectModel.ParagraphAlignment.Left;

            column = DTable.AddColumn();
            column.Borders.Visible = false;
            column.Width = "5cm";
            column.Format.Alignment = MigraDoc.DocumentObjectModel.ParagraphAlignment.Right;

            foreach (TableStructure str in CurrentBlock.LimitList)
            {
                row = DTable.AddRow();
                row.Borders.Visible = false;
                cell = row.Cells[0];
                cell.Borders.Visible = false;
                cell.AddParagraph(str.TableCol1);
                cell = row.Cells[1];
                cell.Borders.Visible = false;
                cell.AddParagraph(str.TableCol2);
            }

Author:  () => true [ Sun Aug 26, 2012 9:47 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

Hi!

I don't want to analyze your code snippet (much code, not many helpful comments).

With MigraDoc, it doesn't matter when you add something, it only matter where you add it.

If you add a section to the document, then add paragraph, table, paragraph, table, paragraph, table to that section, they will appear in that order.
TextFrames are a different topic.

Author:  csalernocsit [ Sun Aug 26, 2012 10:04 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

Logically that's what I thouth too but not what I'm seeing.

I used the TextFrame simply to center the tables in the document. Is there a better way of centering tables?

Author:  Thomas Hoevel [ Mon Aug 27, 2012 7:53 am ]
Post subject:  Re: Help with Mixing Paragraph text and tables

Hi!
csalernocsit wrote:
Is there a better way of centering tables?
Yes. You can set table.Rows.LeftIndent for the table. If you know the width of the page and the width of the table (and you should know both :wink: ) you can calculate the LeftIndent to get the table centered.
Indentation starts at the page border, not the page edge.

DIN A4 is 21 cm wide, standard margins are 2.5 cm each => 16 cm for text => 3 cm left indent to center a 10 cm table.

Author:  csalernocsit [ Mon Aug 27, 2012 12:51 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

Thanks Thomas,
the LeftIndent worked fine for the positioning of the table.
However I still have a challenge with getting the tables to follow a paragraph. Since removing the TextFrame I get the three paragraphs printed followed by a single table at the end after the third paragraph. Trying to achieve:


Paragraph
Table

Paragraph
Table

Paragraph
Table

But instead getting:

Paragraph
Paragraph
Paragraph

Table

Author:  csalernocsit [ Mon Aug 27, 2012 1:04 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

Is there a command that I need to use to end the table after each paragragh?

Author:  Thomas Hoevel [ Mon Aug 27, 2012 2:15 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

Each call of AddTable() creates (and returns) a new table. There is only one AddTable() in your code snippet.
The table consists of the rows and columns you add to it.

Author:  csalernocsit [ Mon Aug 27, 2012 2:20 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

I should have better explained the code.
The above code is called for each of the three paragraphs

Code:
DataParagraph.Append(CurrentBlock.BlockHeader, true, false);
            DataParagraph.Append(CurrentBlock.CoveragesText);
            DataParagraph.Append(CurrentBlock.Limits.Any() ? "Limits of not less than:" : " ");


That's why I don't understand why I'm getting the results I am.

Author:  csalernocsit [ Mon Aug 27, 2012 2:34 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

Here is the actuall calling method.

Code:
DeficientLimitsAndCoverages
                .Where(LoopDeficientLimitsAndCoverages => LoopDeficientLimitsAndCoverages.Display)
                .ToList()
                .ForEach(LoopDeficientLimitsAndCoverages => [b]WriteDeficientLimitsAndCoveragesBlock(LoopDeficientLimitsAndCoverages)[/b]);

Author:  csalernocsit [ Mon Aug 27, 2012 2:37 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

Correction on the codeing

Here is the calling method
Code:
           List<ExpiredLimitsAndCoveragesBlock> blocks = ExpiredLimitsAndCoverages
                 .Where(LoopExpiredLimitsAndCoverages => LoopExpiredLimitsAndCoverages.Display)
                 .ToList();
            blocks.ForEach(LoopExpiredLimitsAndCoverages => WriteExpiredLimitsAndCoveragesBlock(LoopExpiredLimitsAndCoverages));


Here is the method writing the prargraph and table
Code:
        public void WriteExpiredLimitsAndCoveragesBlock(ExpiredLimitsAndCoveragesBlock CurrentBlock)
        {

            //MigraDoc.DocumentObjectModel.Tables.Table DTable = new MigraDoc.DocumentObjectModel.Tables.Table();

            DataParagraph.Append(CurrentBlock.BlockHeader, true, false);
            DataParagraph.Append(CurrentBlock.CoveragesText);
            DataParagraph.Append(CurrentBlock.Limits.Any() ? "Limits of not less than:" : " ");
           
            //DataParagraph.Append(CurrentBlock.LimitsText, CurrentBlock.Limits.Any());

            // Table Test Below
            MigraDoc.DocumentObjectModel.Tables.Table DTable = new MigraDoc.DocumentObjectModel.Tables.Table();
            DTable = DataParagraph.Section.AddTable();
            DTable.Style = "Table";
            DTable.Borders.Visible = false;
            DTable.Rows.LeftIndent = "3cm";

            MigraDoc.DocumentObjectModel.Tables.Cell cell = new MigraDoc.DocumentObjectModel.Tables.Cell();
            MigraDoc.DocumentObjectModel.Tables.Row row = new MigraDoc.DocumentObjectModel.Tables.Row();
         
            // Before you can add a row, you must define the columns
            //MigraDoc.DocumentObjectModel.Tables.Column column = DTable.AddColumn(MigraDoc.DocumentObjectModel.Unit.FromCentimeter(2));
            MigraDoc.DocumentObjectModel.Tables.Column column = new MigraDoc.DocumentObjectModel.Tables.Column();

            column = DTable.AddColumn();
            column.Borders.Visible = false;
            column.Width = "5cm";
            column.Format.Alignment = MigraDoc.DocumentObjectModel.ParagraphAlignment.Left;

            column = DTable.AddColumn();
            column.Borders.Visible = false;
            column.Width = "5cm";
            column.Format.Alignment = MigraDoc.DocumentObjectModel.ParagraphAlignment.Right;

            foreach (TableStructure str in CurrentBlock.LimitList)
            {
                row = DTable.AddRow();
                row.Borders.Visible = false;
                cell = row.Cells[0];
                cell.Borders.Visible = false;
                cell.AddParagraph(str.TableCol1);
                cell = row.Cells[1];
                cell.Borders.Visible = false;
                cell.AddParagraph(str.TableCol2);
            }
        }

Author:  Thomas Hoevel [ Mon Aug 27, 2012 2:49 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

And where are the calls to DataParagraph.Section.AddParagraph()?

Author:  csalernocsit [ Mon Aug 27, 2012 6:47 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

They are done here:


Code:
       public static void Append(this Paragraph CurrentParagraph, string Text, bool MakeBold, bool MakeUnderline, bool AppendCarriageReturn)
        {
            CurrentParagraph.AddFormattedText(string.Concat(Text, AppendCarriageReturn ? "\r\n" : ""), CertLetter.LetterFont(MakeBold, MakeUnderline));
        }
        public static void Append(this Paragraph CurrentParagraph, string Text, bool AppendCarriageReturn)
        {
            CurrentParagraph.Append(Text, false, false, AppendCarriageReturn);
        }
        public static void Append(this Paragraph CurrentParagraph, string Text, bool MakeBold, bool MakeUnderline)
        {
            CurrentParagraph.Append(Text, MakeBold, MakeUnderline, true);
        }
        public static void Append(this Paragraph CurrentParagraph, string Text)
        {
            CurrentParagraph.Append(Text, !string.IsNullOrEmpty(Text));
        }
        public static Paragraph AppendParagraph(this Section CurrentSection)
        {
            Paragraph NewParagraph = CurrentSection.AddParagraph();
            return NewParagraph;
        }

Author:  Thomas Hoevel [ Tue Aug 28, 2012 8:35 am ]
Post subject:  Re: Help with Mixing Paragraph text and tables

Thomas Hoevel wrote:
And where are the calls to DataParagraph.Section.AddParagraph()?
AppendParagraph is never called in the snippets you show here.

Set breakpoints on AddTable and AddParagraph and you will see when (and how often) they are called.

Author:  csalernocsit [ Tue Aug 28, 2012 1:46 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

So you are saying that the order should be

Addparagraph
AddTable

Addparagraph
AddTable

Addparagraph
AddTable

to get the results I'm looking for?

Author:  Thomas Hoevel [ Tue Aug 28, 2012 2:05 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

Yes.
It's all dynamic. You can add a paragraph, then add a table, then add text to the paragraph - the text appears above the table because it doesn't matter when you add something, it only matters where you add it ("where" refers to the element of the MirgaDoc document, not your source code).

Sometimes it helps to save the MigraDoc document in an MDDDL file if you don't get what you expect.
See here:
http://www.pdfsharp.net/wiki/MigraDocDDL.ashx

Author:  csalernocsit [ Tue Aug 28, 2012 3:55 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

So for each of my paragraphs, I need to create a new paragraph and not just append to the same paragraph?

Author:  csalernocsit [ Tue Aug 28, 2012 8:07 pm ]
Post subject:  Re: Help with Mixing Paragraph text and tables

Thanks Thomas for your help.

That was it. I added a AddParagraph() call before each paragraph and now getting expected results.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/