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

Adding table to document (migradocument and pdfdocument?)
https://forum.pdfsharp.net/viewtopic.php?f=2&t=742
Page 1 of 1

Author:  petersmith [ Thu May 28, 2009 8:14 pm ]
Post subject:  Adding table to document (migradocument and pdfdocument?)

I want to add a table to a document.
I read something that I need to add it to migradocument and then merge with regular document, but I have no idea how :)
I get the error: 'LastSection' is not a member of 'PdfSharp.Pdf.PdfDocument'.

Here's my current code

Dim document As PdfDocument = New PdfDocument

'ADD TABLE

Dim tbl As MigraDoc.DocumentObjectModel.Tables.Table = New MigraDoc.DocumentObjectModel.Tables.Table
tbl.Borders.Width = 0.75
Dim col As Column = tbl.AddColumn(MigraDoc.DocumentObjectModel.Unit.FromCentimeter(2))
col.Format.Alignment = MigraDoc.DocumentObjectModel.ParagraphAlignment.Center
col = tbl.AddColumn(MigraDoc.DocumentObjectModel.Unit.FromCentimeter(5))
Dim cell As Cell
Dim row As Row = tbl.AddRow
row.Shading.Color = MigraDoc.DocumentObjectModel.Colors.PaleGreen
cell = row.Cells(0)
cell.AddParagraph("Savings")
cell = row.Cells(1)
cell.AddParagraph("Manager")
row = tbl.AddRow
row.Shading.Color = MigraDoc.DocumentObjectModel.Colors.White
cell = row.Cells(0)
tbl.SetEdge(0, 0, 1, 1, Edge.Box, MigraDoc.DocumentObjectModel.BorderStyle.Single, 1.5, MigraDoc.DocumentObjectModel.Colors.Black)

'LastSection' is not a member of 'PdfSharp.Pdf.PdfDocument'.
document.LastSection.Add(tbl)

Author:  trnilse [ Fri May 29, 2009 8:38 am ]
Post subject: 

You cannot add MigraDocs and MigraDoc elements directly to PDFSharp docs, you need to render them with a DocumentRenderer.

See example here http://pdfsharp.net/PDFsharp/index.php?option=com_content&task=view&id=55&Itemid=63

In this example a paragraph is rendered, but you can render a table too in the same way.

Author:  petersmith [ Fri May 29, 2009 8:43 am ]
Post subject: 

Yes, I saw some more of these posts, but I cant figure out which lines I need for the rendering of the migradocs objects, I dont see the relation from the rendering to the PDF doc....could you just copy the lines I need here? :)

Author:  trnilse [ Fri May 29, 2009 8:57 am ]
Post subject: 

Code:
// 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(gfx, XUnit.FromCentimeter(5), XUnit.FromCentimeter(10), "12cm", para);


doc = Your MigraDocument
para = Your table (in the example it is a paragraph, but it is same as with tables)
gfx = a graphics object from the PDFdocument you are creating

RenderObject places (renders) the migradoc object on the PDFdocument graphics object at the given x,y coordinates.

Hope it is more clear now...

Author:  petersmith [ Fri May 29, 2009 10:52 am ]
Post subject: 

Not entirely, it seems to go better but now I receive the error: Value of '0' is not valid for 'emSize'. 'emSize' should be greater than 0 and less than or equal to System.Single.MaxValue.
Parameter name: emSize

On step through I noticed that this line is causing the crash:
Dim row As MigraDoc.DocumentObjectModel.Tables.Row = tbl.AddRow


This is my code
Dim document As PdfDocument = New PdfDocument

Dim page As PdfPage
For i = 0 To 4

page = document.AddPage
page.Width = XUnit.FromCentimeter(A4Width)
page.Height = XUnit.FromCentimeter(A4Height)
Next i


'ADD TABLE
Dim tbl As MigraDoc.DocumentObjectModel.Tables.Table = New MigraDoc.DocumentObjectModel.Tables.Table
tbl.Borders.Width = 0.75
Dim col As Column = tbl.AddColumn(MigraDoc.DocumentObjectModel.Unit.FromCentimeter(2))
col.Format.Alignment = MigraDoc.DocumentObjectModel.ParagraphAlignment.Center
col = tbl.AddColumn(MigraDoc.DocumentObjectModel.Unit.FromCentimeter(5))
Dim cell As MigraDoc.DocumentObjectModel.Tables.Cell
Dim row As MigraDoc.DocumentObjectModel.Tables.Row = tbl.AddRow
'row.Shading.Color = MigraDoc.DocumentObjectModel.Colors.PaleGreen
'cell = row.Cells(0)
'cell.AddParagraph("Savings")
'cell = row.Cells(1)
'cell.AddParagraph("Manager")
'row = tbl.AddRow
'row.Shading.Color = MigraDoc.DocumentObjectModel.Colors.White
'cell = row.Cells(0)
'tbl.SetEdge(0, 0, 1, 1, Edge.Box, MigraDoc.DocumentObjectModel.BorderStyle.Single, 1.5, MigraDoc.DocumentObjectModel.Colors.Black)



Dim doc As New MigraDoc.DocumentObjectModel.Document
Dim docRenderer As MigraDoc.Rendering.DocumentRenderer = New MigraDoc.Rendering.DocumentRenderer(doc)
docRenderer.PrepareDocument()

page = document.Pages(0)
Dim gfx As XGraphics = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend)

' Render the paragraph. You can render tables or shapes the same way.
docRenderer.RenderObject(gfx, XUnit.FromCentimeter(5), XUnit.FromCentimeter(10), "12cm", tbl)

Author:  trnilse [ Fri May 29, 2009 11:29 am ]
Post subject: 

Hmm, I can't see any reason that you should get an error there. But you're not adding the table to the migra doc before rendering. Hopefully this will fix it.

Try something like this:

Code:
Dim doc As New MigraDoc.DocumentObjectModel.Document

' new:
Dim section As MigraDoc.DocumentObjectModel.Section = doc.AddSection()
doc.LastSection.Add(tbl)

Dim docRenderer As MigraDoc.Rendering.DocumentRenderer = New MigraDoc.Rendering.DocumentRenderer(doc)
docRenderer.PrepareDocument()

Author:  petersmith [ Fri May 29, 2009 5:03 pm ]
Post subject: 

Yeah, that did the trick! Thanks! :)

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