This post is an addition to our previously posted article showing how to create grids, and describes how to use PDF links for navigation between grid rows.
Imaging that you have a grid with
many data rows and it spans across, say, five pages. You’d like to provide the
viewer with a set or links quickly navigating to each 10th, 20th,
etc. row. Here the Link property of ContentElement comes into play. Let’s use a
slightly modified sample from section 2.5 Multipage Grid.
C# code:
public void CreateMultipageGridWithLinks()
{
int rowCount = 45;
//
create resource manager and document object
ResourceManager resourceManager = new ResourceManager();
FlowDocument doc = new FlowDocument() { Margin = new Thickness(5) };
// register a style for links
doc.StyleManager.RegisterStyle(".link",
new Style(){ Color = RgbColors.Blue, Margin = new Thickness(5)});
//
create grid with 4 autosized columns taking 100% of the available space
Grid grid = new Grid(Length.Auto, Length.Auto, Length.Auto, Length.Auto);
// add
col headers
grid.Add(new GridRow(new TextBlock("Order"), new TextBlock("Date"),
new TextBlock("Customer"), new TextBlock("Paid")));
// add
data records
for (int rowIndex = 1; rowIndex <= rowCount; rowIndex++)
{
string rowId = rowIndex.ToString();
/*
setting an id for the entire gridrow won't work because
gridrow objects can't be matched via styles.
They are
special
elements which don't actually exist on page.
So, we assign the id to the first textbox in
this row. */
grid.Add(new GridRow(new TextBlock(rowId){Id=rowId}, new TextBlock("01/02/2015"),
new TextBlock("Jon Doe"), new TextBlock("Yes")));
// check
if the row should get a link to it
if (rowIndex % 10 == 0)
{
// create
textblock serving as link,
// and reference a row using id set to the first cell
TextBlock link = new TextBlock(string.Format("Row {0} ↓", rowIndex));
link.Link = new CrossReference(rowId);
link.Class = "link";
doc.Add(link);
}
}
// add
grid to document
doc.Add(grid);
// save
document
using (Stream stream = File.Create("multipage_grid_with_links.pdf"))
{
doc.Write(stream, resourceManager, new PageBoundary(Boundaries.A6));
}
}
The code from above produces a
set of links which, when clicked, navigate the viewer to the specific row. See
the image below:
Pic. 9 Grid navigation
|
Note that the textblock links placed
on top of the page, are connected to elements in grid using their Link property and a CrossReference object, describing the link. A cross reference
is a link within the same document created using either element’s Id or its
instance.
In order to reference a GridRow,
you’d have to add a link to one of its cells because GridRow objects are
special content elements. They group other objects together logically, but are
not getting created on PDF page; thus, they can’t be linked to.
It’s also possible to create
outbound links to other resources using LinkUri
object, or bookmark page elements using the Bookmark
property. Read more about Apitron PDF Kit for .NET component on its product page.
Complete downloadable version of this article can be found by the following link [PDF].
Complete downloadable version of this article can be found by the following link [PDF].
No comments:
Post a Comment