PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri May 10, 2024 2:31 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Tables question...
PostPosted: Fri Aug 15, 2008 8:23 am 
Offline

Joined: Wed Aug 13, 2008 7:45 am
Posts: 7
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Aug 15, 2008 8:41 am 
Offline

Joined: Wed Aug 13, 2008 7:45 am
Posts: 7
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?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Aug 15, 2008 9:05 am 
Offline

Joined: Wed Aug 13, 2008 7:45 am
Posts: 7
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
           


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Aug 18, 2008 8:15 am 
Offline
PDFsharp Guru
User avatar

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Aug 27, 2008 1:56 pm 
Offline

Joined: Wed Aug 13, 2008 7:45 am
Posts: 7
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??


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Aug 27, 2008 2:22 pm 
Offline
PDFsharp Guru
User avatar

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Aug 27, 2008 2:27 pm 
Offline

Joined: Wed Aug 13, 2008 7:45 am
Posts: 7
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]?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Aug 27, 2008 2:33 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
Cell cell = tbl.Rows[y].Cells[x];

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Aug 27, 2008 2:37 pm 
Offline

Joined: Wed Aug 13, 2008 7:45 am
Posts: 7
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...


Top
 Profile  
Reply with quote  
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: No registered users and 93 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