I have been using Migradoc for a few weeks now but i'm stuck..
I have to build a small application for my job in which I have to generate a simple documentation pdf from a database. Everything is working fine except for the index page.
The setup of the program is as follows:
First of, I create a dataGridView with all the rows from the database I have to export. Then I loop through this dataGridView and for each row, I add a row to a table in the pdf.
Code:
foreach (DataGridViewRow dataRow in dataGridView.Rows)
{
newFolder = dataRow.Cells[4].Value.ToString();
Row row = t.AddRow();
row.Cells[0].AddParagraph(dataRow.Cells[0].Value.ToString());
row.Cells[0].VerticalAlignment = VerticalAlignment.Center;
Paragraph para = row.Cells[1].AddParagraph();
//adding image to this cell. long code, don't think it's necessary.
row.Cells[2].AddParagraph(dataRow.Cells[2].Value.ToString());
row.Cells[2].VerticalAlignment = VerticalAlignment.Center;
row.Cells[3].AddParagraph(dataRow.Cells[3].Value.ToString());
row.Cells[3].VerticalAlignment = VerticalAlignment.Center;
Now here comes the problem. The last cell of each row contains the name of the folder in which the data is stored. When the current folder of the row that is looped is different from the last row, then a bookmark needs to be added and a reference on the index page with a page number.
Code:
if (!newFolder.Equals(oldFolder) && newFolder != "")
{
Paragraph p = row.Cells[4].AddParagraph(dataRow.Cells[4].Value.ToString());
p.AddBookmark(dataRow.Cells[4].Value.ToString() + "bm");
p.Format.OutlineLevel = OutlineLevel.Level2;
Hyperlink hLink = indexSection.AddParagraph().AddHyperlink(dataRow.Cells[4].Value.ToString() + "bm", HyperlinkType.Bookmark);
hLink.AddText(" - " + dataRow.Cells[4].Value.ToString() + "\t_______________________ ");
hLink.AddPageRefField(dataRow.Cells[4].Value.ToString() + "bm");
}
else
{
row.Cells[4].AddParagraph(dataRow.Cells[4].Value.ToString());
}
When the pdf is made, there are bookmarks. On the left side in Adobe Reader I can see all the bookmarks and they point to exactly the right page. These are probably added by the paragraph itself because I'm using
Code:
p.Format.OutlineLevel = OutlineLevel.Level2;
But when i look at the index page, I can see all the bookmarks, but the page numbers all direct to the beginning of the table and so do the hyperlinks.
I assume i'm doing something wrong, but I can't figure out what.
I'd really appreciate some help!