PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Jul 15, 2024 10:23 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 17 posts ] 
Author Message
PostPosted: Sun Aug 26, 2012 2:43 pm 
Offline

Joined: Sun Aug 26, 2012 2:34 pm
Posts: 15
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);
            }


Top
 Profile  
Reply with quote  
PostPosted: Sun Aug 26, 2012 9:47 pm 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 343
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.

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
PostPosted: Sun Aug 26, 2012 10:04 pm 
Offline

Joined: Sun Aug 26, 2012 2:34 pm
Posts: 15
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?


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 27, 2012 7:53 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 27, 2012 12:51 pm 
Offline

Joined: Sun Aug 26, 2012 2:34 pm
Posts: 15
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 27, 2012 1:04 pm 
Offline

Joined: Sun Aug 26, 2012 2:34 pm
Posts: 15
Is there a command that I need to use to end the table after each paragragh?


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 27, 2012 2:15 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 27, 2012 2:20 pm 
Offline

Joined: Sun Aug 26, 2012 2:34 pm
Posts: 15
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.


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 27, 2012 2:34 pm 
Offline

Joined: Sun Aug 26, 2012 2:34 pm
Posts: 15
Here is the actuall calling method.

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 27, 2012 2:37 pm 
Offline

Joined: Sun Aug 26, 2012 2:34 pm
Posts: 15
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);
            }
        }


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 27, 2012 2:49 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
And where are the calls to DataParagraph.Section.AddParagraph()?

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 27, 2012 6:47 pm 
Offline

Joined: Sun Aug 26, 2012 2:34 pm
Posts: 15
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;
        }


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 28, 2012 8:35 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 28, 2012 1:46 pm 
Offline

Joined: Sun Aug 26, 2012 2:34 pm
Posts: 15
So you are saying that the order should be

Addparagraph
AddTable

Addparagraph
AddTable

Addparagraph
AddTable

to get the results I'm looking for?


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 28, 2012 2:05 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 28, 2012 3:55 pm 
Offline

Joined: Sun Aug 26, 2012 2:34 pm
Posts: 15
So for each of my paragraphs, I need to create a new paragraph and not just append to the same paragraph?


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 28, 2012 8:07 pm 
Offline

Joined: Sun Aug 26, 2012 2:34 pm
Posts: 15
Thanks Thomas for your help.

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


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 30 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