PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Apr 28, 2024 12:59 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Thu May 28, 2009 8:14 pm 
Offline

Joined: Thu May 28, 2009 8:11 pm
Posts: 14
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)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri May 29, 2009 8:38 am 
Offline

Joined: Tue May 26, 2009 3:32 pm
Posts: 6
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri May 29, 2009 8:43 am 
Offline

Joined: Thu May 28, 2009 8:11 pm
Posts: 14
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? :)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri May 29, 2009 8:57 am 
Offline

Joined: Tue May 26, 2009 3:32 pm
Posts: 6
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...


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri May 29, 2009 10:52 am 
Offline

Joined: Thu May 28, 2009 8:11 pm
Posts: 14
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)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri May 29, 2009 11:29 am 
Offline

Joined: Tue May 26, 2009 3:32 pm
Posts: 6
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()


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri May 29, 2009 5:03 pm 
Offline

Joined: Thu May 28, 2009 8:11 pm
Posts: 14
Yeah, that did the trick! Thanks! :)


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

All times are UTC


Who is online

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