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

positioning PDFSharp graphics in a table
https://forum.pdfsharp.net/viewtopic.php?f=2&t=780
Page 1 of 1

Author:  mikesowerbutts [ Wed Jul 08, 2009 1:29 pm ]
Post subject:  positioning PDFSharp graphics in a table

Hi,

I have a page with multiple tables on, all of which position nicely in migradoc, I have however created a graphic using the PDFSharp functions like drawRectangle etc. I would like to be able to position this under a table, I can do this by specifically stating coordinates which will put it under the table, but when the table size changes, the graphic doesnt move up/down accordingly.

Is there a way to create a graphic using PDFSharp commands, but then add it to a a MigraDoc section so it will layout correctly?

Author:  Thomas Hoevel [ Mon Jul 13, 2009 9:30 am ]
Post subject:  Re: positioning PDFSharp graphics in a table

Hi!

It should be possible: just add a MigraDoc object of appropriate size as a placeholder and add a Tag to this object (a user-defined object class that contains information what to draw).

Create the PDF document page by page and draw the PDFsharp objects over the placeholder.

Author:  mikesowerbutts [ Tue Jul 14, 2009 2:08 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

Hi Thomas,

I have basically got thsi working - using a TextFrame as the placeholder, but it always returns its Left and Top properties as 0. I am the textframe the the appropriate cell, but I cant seem to get the co-ordinates out which i need for positioning the drawing.

any ideas?

Mike

Author:  Thomas Hoevel [ Tue Jul 14, 2009 2:42 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

mikesowerbutts wrote:
Hi Thomas,

I have basically got thsi working - using a TextFrame as the placeholder, but it always returns its Left and Top properties as 0.

Have you called Left and Top after executing "docRenderer.PrepareDocument()"?

Here's a code snippet:
Code:
this.docRenderer = new DocumentRenderer(this.doc);
this.docRenderer.PrepareDocument();
int pageCount = this.docRenderer.FormattedDocument.PageCount;
for (int idx = 0; idx < pageCount; idx++)
{
  DocumentObject[] docObjects = this.docRenderer.GetDocumentObjectsFromPage(idx + 1);
  if (docObjects != null && docObjects.Length > 0)
  {
     [...] // indentify your textframe here (add an appropriate Tag to it when you add it)
  }
}

Author:  mikesowerbutts [ Thu Jul 16, 2009 1:39 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

Hi Thomas,

I am trying to detect the cell I have added the placeholder to using the following code:
Code:
public List<DocumentObject> getDocumentObjectsWithTag()
        {
            List<DocumentObject> _objs = new List<DocumentObject>();
            DocumentRenderer _renderer = new DocumentRenderer(document);
            _renderer.PrepareDocument();
            int pageCount = _renderer.FormattedDocument.PageCount;
            for (int idx = 1; idx <= pageCount; idx++)
            {
              DocumentObject[] docObjects = _renderer.GetDocumentObjectsFromPage(idx);
              if (docObjects != null && docObjects.Length > 0)
              {
                  for (int g = 0; g < docObjects.Length; g++)
                  {
                      if (docObjects[idx].GetType() == typeof(MigraDoc.DocumentObjectModel.Tables.Table))
                      {
                          MigraDoc.DocumentObjectModel.Tables.Table _tbl = (MigraDoc.DocumentObjectModel.Tables.Table)docObjects[idx];
                          if (_tbl.Comment != "")
                          {

                          }
                          for (int r = 0; r < _tbl.Rows.Count; r++)
                          {
                              for (int c = 0; c < _tbl.Columns.Count; c++)
                              {
                                  try
                                  {
                                      Cell _c = _tbl[r, c];
                                      if (_c.Tag != null)
                                      {
                                          _objs.Add(_c.Elements[0]);
                                      }
                                  }
                                  catch
                                  {

                                  }
                              }
                          }
                      }
                  }
              }
            }
            return _objs;
        }

I can detect the appropriate cell before the document is rendered, but since incorporating the prepare document etc. you mentioned in your last post, all of the .Tag and .Comment properties for every DocumentObject (and cells in the tables) seems to be set to either null or "".

This then means that I cant find the appropriate cell, to then get the placeholder etc. etc.

I am so close with this, but so far still!

please help!

Mike

Author:  Thomas Hoevel [ Thu Jul 16, 2009 2:24 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

I see
Code:
for (int g = 0; g < docObjects.Length; g++)

followed by
Code:
if (docObjects[idx].GetType()


"docObjects[g]" might be worth a try ...

Author:  mikesowerbutts [ Thu Jul 16, 2009 3:09 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

good point!

that part of the code is now working! I cant belive that it was such a simple mistake that I literally wasted about 2 hours on!

however, I am back to the same problem as I was a few posts ago, the TextFrame I am using as the placeholder is still returning its Left.Position.Centimeter property (and the same for Top) as 0?

I dont ever specifically set these properties, but i presumed that adding all the PrepareDocument() stuff, would update these properties?

Mike

Author:  Thomas Hoevel [ Thu Jul 16, 2009 4:13 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

Shame on me - the information you need is "internal".
I should have checked that before writing here ...

You could add a new method
Code:
/// <summary>
/// Gets the render information for document objects that get rendered on the specified page.
/// </summary>
internal RenderInfo[] GetRenderInfoFromPage(int page)
{
  return this.formattedDocument.GetRenderInfos(page);
}

right below "GetDocumentObjectsFromPage" in DocumentRenderer.cs.

In the AssemblyInfo.cs of MigraDoc.Rendering you can add a "[assembly: InternalsVisibleTo(" line for your assembly (or change "internal abstract class RenderInfo" to public and also make GetRenderInfoFromPage public).

What you need is RenderInfo.LayoutInfo.ContentArea.
RenderInfo.DocumentObject will take you to your object.

I can't say whether or not this function will be included in the forthcoming release of PDFsharp.

Author:  mikesowerbutts [ Tue Jul 21, 2009 2:19 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

Hi Thomas,

I have added the new stuff into the MigraDoc.Rendering.dll - BUT I cant get the assembly change to work correctly - I have
Code:
[assembly: InternalsVisibleTo("MigraDoc.Rendering")]

But I keep getting an error saying strongly named assemblies need a public key or something similar.

Also I changed the RenderInfo class to public anstract, rather than internal abstract and changed the new function I added to DocumentRenderer.cs to public, but I still cant access RenderInfo.LayoutInfo as it is internal? (maybe this will be abailable when the assembly InternalsVisibleTo is fixed?

To be honest im not really sure what I am doing here. Please help!

Author:  Thomas Hoevel [ Tue Jul 21, 2009 3:12 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

"InternalsVisibleTo" does what the name suggests.
You insert it into the AssemblyInfo.cs of the assembly that contains the Internals (i. e. MigraDoc.Rendering) providing assembly name (and public key if your assembly uses a SNK) of the assembly that is to see the internals (i. e. your assembly).

To get the public key, use SN.EXE (applicable only if your assembly uses a strong name key):
sn -p friend_assemblies.snk key.publickey // Extract public key from key.snk into key.publickey
sn -tp key.publickey // Display public key stored in file'key.publickey

Author:  mikesowerbutts [ Wed Jul 22, 2009 2:58 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

hi Thomas,

got the assembly code working finally!

Now, when I put:
Code:
RenderInfo.

I only see the static method "GetTotalHeight" and not any of the other methods, normally I would instansiate an RenderInfo document to get round this, but as it's an abstract class i cant!

do you think this is actually a workable solution? it just seems to be problem after problem!

Mike

Author:  Thomas Hoevel [ Wed Jul 22, 2009 3:17 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

"GetRenderInfoFromPage" returns the RenderInfo objects you need (see code above, not yet included).

You only see the static methods if you type the classname.
You see all methods if you use an object.

Author:  mikesowerbutts [ Wed Jul 22, 2009 3:31 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

It wont let me point to the method GetRenderInfoFromPage though!

could you please provide some sample code?

im sure I will get there in the end!

Author:  Thomas Hoevel [ Wed Jul 22, 2009 3:41 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

"GetRenderInfoFromPage" is the new one you have to add yourself (code given in this thread).

Author:  mikesowerbutts [ Wed Jul 22, 2009 4:10 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

Hi Thomas,

I have this part now working - i stupidly thought the GetRenderInfoFrom page method had to be called through RenderInfo.

the textframe is still returning 0 as its left/top position, even when I set the left/top props of the textframe to 20 when I add it to the table cell!

Mike

Author:  Thomas Hoevel [ Thu Jul 23, 2009 2:27 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

What you need is RenderInfo.LayoutInfo.ContentArea.

RenderInfo contains LayoutInfo.ContentArea and DocumentObject.
Iterate through the RenderInfos to find your tagged DocumentObject and then use the information from LayoutInfo.ContentArea.

Author:  mikesowerbutts [ Fri Jul 24, 2009 8:41 am ]
Post subject:  Re: positioning PDFSharp graphics in a table

Hi Thomas,

OK so i can get the top/left of the entire table, but i need to top/left of the textframe contained within one of my table's cells.

In the RenderInfo[] I have all of the 4 TableRenderInfo objects, I can then access the DocumentObject property of each of these and loop through its Cols/Rows to detect the correct cell using the Tag property, but I cant get LayoutInfo for the TextFrame (as i can only seem to access it through the DocumentObject property)

Is there a function like "GetRenderInfoFromDocumentObject" or something which will allow me to retrieve the render info for a nested element such as the TextFrame?

Author:  C.Bienek [ Tue Nov 22, 2011 3:28 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

Hi, there.

any success in findong out the exact cell position?
I am as far as you where 2 years ago, so I hope you or anyone remember.

Finding out the tables position is nice but may be not enough. as I want to create a graphic exactly fitting into a specific cell.

Author:  wasers [ Wed Aug 27, 2014 12:57 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

Hi,

I'm facing now the same problem.

I got a TextFrame inside a ParagraphElement inside my Primary footer with a special setter Tag property.
After the document is rendered, I have to find the rendered position of the text frame to append some text.
But all i got is two ParagraphRenderInfo objects if asking the GetRenderInfoFromPage().
Can't find a way to get the document objects tree downwards until I reached the text frame that I has tagged before.

Any suggestion how i can find my tagged textFrame?

Author:  centravanti [ Fri Oct 31, 2014 9:18 am ]
Post subject:  Re: positioning PDFSharp graphics in a table

To get the RenderInfo we will need to render/prepare the document first.
I'm using DocumentRenderer.PrepareDocument() and then use the suggested way to get the content height. This works.
However, it takes an extremely long time when you have lots of row and you have to check the height after you added a new row.

Any suggestion? The reason I need to check the height every time is , if the rows are too many to fit for one page we will have to move it to the next page and generate a header based on the information of the rows.

Author:  () => true [ Sun Nov 02, 2014 7:49 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

centravanti wrote:
However, it takes an extremely long time when you have lots of row and you have to check the height after you added a new row.
So you start a new table with every new page? No need to render more then just the final table to see whether it fits a single page IMHO.

Author:  centravanti [ Mon Nov 03, 2014 1:29 am ]
Post subject:  Re: positioning PDFSharp graphics in a table

() => true wrote:
centravanti wrote:
However, it takes an extremely long time when you have lots of row and you have to check the height after you added a new row.
So you start a new table with every new page? No need to render more then just the final table to see whether it fits a single page IMHO.


yep, i need to start with a table on every page. Hmm i am not sure that will solve this, because the number of rows of the tables could be different.
If only i could determine the table height every time i add a new row before rendering that would be great

Author:  jsteward [ Tue Jan 08, 2019 5:40 pm ]
Post subject:  Re: positioning PDFSharp graphics in a table

I created an account just to answer this:

If you need a row to repeat at the top of new pages. No rendering or height measurements are required.

with the row you are working with, just set

Code:
row.HeadingFormat = true;


This is a built-in feature that builds the header row(s) in, no measurement required.

It can be applied to multiple rows, and those rows will all repeat.

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