Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions knowledge-base/creating-tablecells-with-gotoaction-and-uriaction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
title: Creating TableCells with GoToAction and UriAction
description: This article demonstrates how can create table cells with GoTo action and UriAction in PDF document with Telerik Document Processing.
type: how-to
page_title: Creating TableCells with GoToAction and UriAction
slug: kb-create-table-cells
position: 0
tags: pdf, datatable, cells, widget
res_type: kb
---

|Product Version|Product|Author|
|----|----|----|
|N/A|RadPdfProcessing|Maria Terzieva|

## Description

With the PDF format, each content element has a specific position, and in some cases, each character is positioned separately. PDF standard does not have any information about tables as well, once exported the tables in the document are represented by lines and text fragments.

Although the editors do not allow you to insert the widgets it is possible to insert a place holder and then use its postion to insert the widget.

## Solution

First we need to create a table in the document. Next we have to iterate throught the elements of the page and to find the text fragment which we want. After that for creating Links to URLs or Locations within the document, we use Actions - UriAction or GoToAction.

## Create TableCells with GoToAction and UriAction

````C#
{{region kb-create-table-cells}}

RadFixedDocument document = new RadFixedDocument();
using (RadFixedDocumentEditor editor = new RadFixedDocumentEditor(document))
{
Table table = new Table();
Border border = new Border(BorderStyle.Single);
table.Borders = new TableBorders(left: border, top: border, right: border, bottom: border);
TableRow firstRow = table.Rows.AddTableRow();
TableCell firstRowFirstCell = firstRow.Cells.AddTableCell();
firstRowFirstCell.Borders = new TableCellBorders(left: border, top: border, right: border, bottom: border);
Block block1 = firstRowFirstCell.Blocks.AddBlock();
block1.InsertText("Text in first cell.");

TableRow secondRow = table.Rows.AddTableRow();
TableCell secondRowFirstCell = secondRow.Cells.AddTableCell();
secondRowFirstCell.Borders = new TableCellBorders(left: border, top: border, right: border, bottom: border);
Block block2 = secondRowFirstCell.Blocks.AddBlock();
block2.InsertText("Text in second cell.");

editor.InsertTable(table);
}

RadFixedPage secondPage = document.Pages.AddPage();

foreach (RadFixedPage page in document.Pages)
{
foreach (ContentElementBase element in page.Content)
{
if (element is TextFragment textFragment)
{
if (textFragment.Text == "Text in first cell.")
{
Location location = new Location
{
Left = 225,
Top = 500,
Zoom = 1,
Page = secondPage
};

GoToAction goToAction = new GoToAction
{
Destination = location
};

Link goToLink = document.Pages[0].Annotations.AddLink(goToAction);
Rect clippingBounds = textFragment.Clipping.Clip.Bounds;
goToLink.Rect = new Rect(clippingBounds.X, clippingBounds.Y, clippingBounds.Width, clippingBounds.Height);
}
else if (textFragment.Text == "Text in second cell.")
{
UriAction uriAction = new UriAction
{
Uri = new Uri(@"http://www.telerik.com"),
};

Link uriLink = document.Pages[0].Annotations.AddLink(uriAction);
Rect clippingBounds = textFragment.Clipping.Clip.Bounds;
uriLink.Rect = new Rect(clippingBounds.X, clippingBounds.Y, clippingBounds.Width, clippingBounds.Height);
}
}
}
}

string outputPath = "Exported.pdf";
if (File.Exists(outputPath))
{
File.Delete(outputPath);
}

using (Stream output = File.OpenWrite(outputPath))
{
PdfFormatProvider provider = new PdfFormatProvider();
provider.Export(document, output);
}

Process.Start(new ProcessStartInfo() { FileName = outputPath, UseShellExecute = true });

{{endregion}}

````