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

Set table border on existing pdf template
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3177
Page 1 of 1

Author:  Zare [ Thu Aug 20, 2015 7:35 am ]
Post subject:  Set table border on existing pdf template

Hi, I have a pdf document template with predefined table and rows/columns. I use pdfsharp to fill the form fields with data (as demonstrated in your samples). I was able to set font of the content. Now I need to set a border visible, is this possible or do I have to use MigraDoc and draw the whole template from scratch?

Thanks in advance

Author:  Thomas Hoevel [ Thu Aug 20, 2015 8:28 am ]
Post subject:  Re: Set table border on existing pdf template

Hi!
Zare wrote:
Now I need to set a border visible, is this possible or do I have to use MigraDoc and draw the whole template from scratch?
I cannot answer that with the information I have. You do not show your template. Can you add the borders to the template?
viewtopic.php?f=2&t=832

Author:  Zare [ Thu Aug 20, 2015 8:39 am ]
Post subject:  Re: Set table border on existing pdf template

My mistake, I didn't specify that I want to add border only to those rows I add data to. In my template there is a table with predefined 10 rows, 3 columns per row. I fill some of those rows (usually not more than 2) programmatically based on the data I get from a web service. I didn't think that posting an image of the template would give any more information, as it contains a company specific data and a blank table (no borders). I am actually interseted only in bottom border of the table row.

Author:  Thomas Hoevel [ Thu Aug 20, 2015 8:59 am ]
Post subject:  Re: Set table border on existing pdf template

There are no tables in PDF.
You can easily draw some lines based on co-ordinates saved in your code. Disadvantage: Changes of the template will require changes of the code.

Getting co-ordinates from code would be better, but I think it is much more complicated.

Author:  Zare [ Thu Aug 20, 2015 10:47 am ]
Post subject:  Re: Set table border on existing pdf template

Thanks, do you have any example for this or know which of the downloaded samples includes simplest form to do this?

Author:  Thomas Hoevel [ Thu Aug 20, 2015 11:23 am ]
Post subject:  Re: Set table border on existing pdf template

Use gfx.DrawLine() as in this sample:
http://www.pdfsharp.net/wiki/Graphics-s ... le_lines_0

You can use XUnit.FromMillimeter or XUnit.FromInch and others for unit conversions.
Code:
gfx.DrawLine(XPens.DarkBlue, XUnit.FromMillimeter(0), XUnit.FromMillimeter(150), XUnit.FromMillimeter(210), XUnit.FromMillimeter(150));

Author:  Zare [ Thu Aug 20, 2015 2:09 pm ]
Post subject:  Re: Set table border on existing pdf template

Thanks. I also found that I can use Elements collection on the PdfTextField and a key "/Rect" to get coordinates. But there is something strange, my lines are drawn as if I got coordinates from the bottom of the page.

Author:  Thomas Hoevel [ Thu Aug 20, 2015 2:23 pm ]
Post subject:  Re: Set table border on existing pdf template

Maybe WorldToDefaultPage can help:
Code:
rect = gfx.Transformer.WorldToDefaultPage(New XRect(New XPoint(100, 400), New XSize(350, 150)))


If not then you may need a different transformation.

Author:  Zare [ Thu Aug 20, 2015 2:26 pm ]
Post subject:  Re: Set table border on existing pdf template

I will test some more, thanks for the help.

Author:  Zare [ Mon Aug 24, 2015 7:57 am ]
Post subject:  Re: Set table border on existing pdf template

Very strange, I use the following to get coordinates of text field:
Code:
PdfArray coordinates = (PdfArray)txtField.Elements["/Rect"];
double x = double.Parse(coordinates.Elements[0].ToString(), System.Globalization.CultureInfo.GetCultureInfo("en-US"));
double y = double.Parse(coordinates.Elements[1].ToString(), System.Globalization.CultureInfo.GetCultureInfo("en-US"));
double w = double.Parse(coordinates.Elements[2].ToString(), System.Globalization.CultureInfo.GetCultureInfo("en-US"));
double h = double.Parse(coordinates.Elements[3].ToString(), System.Globalization.CultureInfo.GetCultureInfo("en-US"));
xg.DrawLine(XPens.Black, x, y, w, y);


It seems as I get coordinates from the bottom of the page and when I call the DrawLine it draws from the top.

Author:  Thomas Hoevel [ Mon Aug 24, 2015 9:12 am ]
Post subject:  Re: Set table border on existing pdf template

Zare wrote:
It seems as I get coordinates from the bottom of the page and when I call the DrawLine it draws from the top.
So instead of "y" you have to use something like "842 - y".
842 is the height of a DIN A4 page. You have to use the height of your template page.

Author:  Zare [ Mon Aug 24, 2015 9:42 am ]
Post subject:  Re: Set table border on existing pdf template

Thanks, it worked!

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