I need add page link to cell
- Code: Select all
for (int i = 0; i < grid.Columns.Count; i++)
{
row1.Cells[i].Value = page link
row2.Cells[i].Value = String.Format("{0}", i + 1);
}
for (int i = 0; i < grid.Columns.Count; i++)
{
row1.Cells[i].Value = page link
row2.Cells[i].Value = String.Format("{0}", i + 1);
}
//Create a PDF file with the PdfGrid.
PdfDocument doc = new PdfDocument();
PdfPageBase pageone = doc.Pages.Add();
//Add a page
PdfPageBase pagetwo = doc.Pages.Add();
pagetwo.Canvas.DrawString("This is Page Two.",
new PdfFont(PdfFontFamily.Helvetica, 20f),
new PdfSolidBrush(Color.Black),
10, 10);
PdfPageBase pagethree = doc.Pages.Add();
pagethree.Canvas.DrawString("This is Page Three.",
new PdfFont(PdfFontFamily.Helvetica, 20f),
new PdfSolidBrush(Color.Black),
10, 10);
//Create the Pdf grid.
PdfGrid grid = new PdfGrid();
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
grid.Style.CellPadding.Top = 5f;
grid.Style.CellPadding.Bottom = 5f;
grid.Columns.Add(3);
//Set the columns’ width
grid.Columns[0].Width = 120f;
grid.Columns[1].Width = 120f;
grid.Columns[2].Width = 120f;
//Create a PDFStringFormat instance
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
//Assign values to the cells of the grid and set formatting
row1.Cells[0].Value = "toPage2";
row1.Cells[0].StringFormat = stringFormat;
row1.Cells[1].Value = "1028";
row1.Cells[1].StringFormat = stringFormat;
row1.Cells[2].Value = "";
row1.Cells[2].StringFormat = stringFormat;
row2.Cells[0].Value = "toPage3";
row2.Cells[0].StringFormat = stringFormat;
row2.Cells[1].Value = "1028";
row2.Cells[1].StringFormat = stringFormat;
row2.Cells[2].Value = "";
row2.Cells[2].StringFormat = stringFormat;
grid.Draw(pageone, new PointF(0f, 50f));
//Find the text that you want to add the annotation.
PdfTextFind[] result = null;
string[] test = { "toPage2", "toPage3" };
for (int i = 0; i < test.Length; i++)
{
result = pageone.FindText(test[i], TextFindParameter.None).Finds;
foreach (PdfTextFind find in result)
{
//Create a PDFGoToAction instance to jump to a page.
PdfDestination pageBottomDest = new PdfDestination(doc.Pages[i+1]);
PdfGoToAction action = new PdfGoToAction(pageBottomDest);
//There is the default margin 40 when creating a new PDF file with Spire.PDF, so we need to subtract the margin here. If you load an existing PDF file, there is no need to subtract 40.
RectangleF buttonBounds = new RectangleF(find.Position.X-40, find.Position.Y-40, find.Size.Width, find.Size.Height);
PdfActionAnnotation annotation = new PdfActionAnnotation(buttonBounds, action);
pageone.AnnotationsWidget.Add(annotation);
}
result = null;
}
doc.SaveToFile("result.pdf", FileFormat.PDF);