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

Tables question...
https://forum.pdfsharp.net/viewtopic.php?f=2&t=451
Page 1 of 1

Author:  garethterrace [ Fri Aug 15, 2008 8:23 am ]
Post subject:  Tables question...

Hello PDfSharp people!

I'm using PDFSharp to make a report from an SQL database, but I'm having trouble getting PDFsharp to insert a table.

I think I should use MigraDoc for the table, but i'm not sure how to integrate it into PDFsharp just for the table.

Any help would be appreciated!

Gareth

Author:  garethterrace [ Fri Aug 15, 2008 8:41 am ]
Post subject: 

okay, I've found the tables sample in the Migradoc samples. and I think I've created a table, but I cant get it to display in the PDF I generate with PDFsharp.

anyone have any ideas?

Author:  garethterrace [ Fri Aug 15, 2008 9:05 am ]
Post subject: 

figured it out, code for anyone that wants it:

Code:
private void ConsumptionReport(string PageTitle, string SubTitle, PdfPage pg, XGraphics Xgfx)
        {

            //table?
            //got to make the table in migradoc and then merge it into PDFsharp
            //for no apparent reason....

            //MigraDoc make:
         
            // HACKĀ²
            Xgfx.MUH = PdfFontEncoding.Unicode;
            Xgfx.MFEH = PdfFontEmbedding.Default;
            //  MigraDoc document for rendering.
            Document doc = new Document();
            Section sec = doc.AddSection();

            //add table to document:

            Table tbl = new Table();
            tbl.Borders.Width = 0.75;

            Column col = tbl.AddColumn(Unit.FromCentimeter(2));
           
            col.Format.Alignment = ParagraphAlignment.Center;
            col = tbl.AddColumn(Unit.FromCentimeter(5));
            Cell cell;
            Row row = tbl.AddRow();
            row.Shading.Color = Colors.PaleGreen;
            cell = row.Cells[0];
            cell.AddParagraph("Savings");
            cell = row.Cells[1];
            cell.AddParagraph("Manager");

            row = tbl.AddRow();
            row.Shading.Color = Colors.White;
            cell = row.Cells[0];


            tbl.SetEdge(0, 0, 1, 1, Edge.Box, MigraDoc.DocumentObjectModel.BorderStyle.Single, 1.5, Colors.Black);
            doc.LastSection.Add(tbl);

            // Create a renderer and prepare (=layout) the document
            MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(doc);
            docRenderer.PrepareDocument();

            // Render the paragraph. You can render tables or shapes the same way.
            docRenderer.RenderObject(Xgfx, XUnit.FromCentimeter(5), XUnit.FromCentimeter(10), XUnit.FromCentimeter(12), tbl);
           


            //end table

            //END MigraDoc
           

Author:  Thomas Hoevel [ Mon Aug 18, 2008 8:15 am ]
Post subject: 

Hi!
garethterrace wrote:
//for no apparent reason....

At first, there was MigraDoc (using a different PDF library).
Then we developed PDFsharp as the new base of MigraDoc - merging PDFsharp and MigraDoc was not possible at that time.

Now we include samples that show how to merge MigraDoc and PDFsharp.

MigraDoc creates documents - creating as many pages as required, as PDF, RTF, or printout.
PDFsharp deals with pages and PDF objects.

So be happy that there is some integration.
If we had time for a complete redesign, there would be seamless integration ...

Author:  garethterrace [ Wed Aug 27, 2008 1:56 pm ]
Post subject: 

thanks for clarifying Tom,

I'm having real trouble creating a table at run time, my code looks as follows - but it isnt working correctly (not all the cells are filled.



Code:
List<Row> lstRows = new List<Row>();
            List<Column> lstCols = new List<Column>();
// HACKĀ²
            Xgfx.MUH = PdfFontEncoding.Unicode;
            Xgfx.MFEH = PdfFontEmbedding.Default;
            //  MigraDoc document for rendering.
            Document doc = new Document();
            Section sec = doc.AddSection();

            //add table to document:

            Table tbl = new Table();
            tbl.Borders.Width = 0.75;

            for (int i = 0; i < cols; i++)
            {
                //draw columns first
                Column col = tbl.AddColumn(Unit.FromCentimeter(3));
                 col.Format.Alignment = ParagraphAlignment.Center;
                 col = tbl.AddColumn(Unit.FromCentimeter(5));
                 col.Format.Alignment = ParagraphAlignment.Center;
                lstCols.Add(col);
            }
            for (int i = 0; i < rows; i++)
            {
               //draw rows
                Row row = tbl.AddRow();
            row.VerticalAlignment = VerticalAlignment.Center;
            row.Height = Unit.FromMillimeter(7);
            row.Shading.Color = Colors.LightGray;
            lstRows.Add(row);
            }
            //now fill with cells
            for (int i = 0; i < cols; i++)
            {
                    //found a column

                for (int j = 0; j < rows -1; j++)
                {
                    Cell cell = new Cell();
                        cell = lstRows[i].Cells[j];
                        cell.AddParagraph("Data");
                  } 
             }

tbl.SetEdge(0, 0, cols, rows, Edge.Box, MigraDoc.DocumentObjectModel.BorderStyle.Single, 1.0, Colors.Black);
            doc.LastSection.Add(tbl);

            // Create a renderer and prepare (=layout) the document
            MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(doc);
            docRenderer.PrepareDocument();

            // Render the paragraph. You can render tables or shapes the same way.
            docRenderer.RenderObject(Xgfx, XUnit.FromMillimeter(x), XUnit.FromMillimeter(y), XUnit.FromCentimeter(wdth), tbl);
           



Am I right in thinking that should work??

Author:  Thomas Hoevel [ Wed Aug 27, 2008 2:22 pm ]
Post subject: 

Look here: "lstRows[i].Cells[j];"
I think you swapped i and j here ...
Why "j < rows -1", not "j < rows"?

You don't need lstRow and lstCol. You don't need "new Cell()".

I'd start with:
Code:
Table table = document.LastSection.AddTable();


You can use table.Rows[x] and table.Columns[x] and row.Cells[x].

MigraDoc documents can persist as text files.
If my documents don't look as they should, I look at the persisted document.

Here's how to get it (after composing; I'd call it immediately before rendering):
Code:
#if DEBUG
      string strDirectory = Path.GetDirectoryName(pdfFilename);
      DdlWriter dw = new DdlWriter(Path.Combine(strDirectory, @"samplepdf.mdddl"));
      dw.WriteDocument(document);
      dw.Close();
#endif

Author:  garethterrace [ Wed Aug 27, 2008 2:27 pm ]
Post subject: 

Thanks!

Just realised I'd been chopping and changing code all day and had loads of rubbish in there which didnt need to be there.

the only major problem i'm having is populating the cells, how do I access row[x].cell[y]?

Author:  Thomas Hoevel [ Wed Aug 27, 2008 2:33 pm ]
Post subject: 

Cell cell = tbl.Rows[y].Cells[x];

Author:  garethterrace [ Wed Aug 27, 2008 2:37 pm ]
Post subject: 

diamond!

this is my first time with C# and also my first time with an external library, thanks for your help, I'm getting used to it slowly...

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