Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Sat Oct 26, 2019 12:49 am

How to add page link to PdfGrid table?
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);
}

yingwx2018
 
Posts: 1
Joined: Sat Oct 26, 2019 12:43 am

Mon Oct 28, 2019 10:18 am

Hi,

Thanks for your inquiry.
Please refer to the code below to add an annotation to jump to specified page in PDF file.

Code: Select all
            //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);


If the code couldn't solve your issue. Please offer us your input Pdf file and your desired result file for further investigation. You could upload them here or send us([email protected]) via email.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.PDF