Category / Section
How to find the specific field element is available in the report?
Published:
Updated:
ReportSerializer
helper and RDL models with Bold reports library are used to find the specific field information in the report.
For example, the following code is used to find the Quarter text available in the report.
private void InspectReport(string reportPath)
{
FileStream fileStream = new FileStream(reportPath, FileMode.Open, FileAccess.Read);
MemoryStream reportStream = new MemoryStream();
fileStream.CopyTo(reportStream);
reportStream.Position = 0;
fileStream.Close();
BoldReports.RDL.DOM.ReportSerializer serializer = new BoldReports.RDL.DOM.ReportSerializer();
var reportDefinition = serializer.GetReportDefinition(reportStream);
// Property to get the report items information from report body.
BoldReports.RDL.DOM.ReportItems bodyReportItems = null;
if (reportDefinition.Body == null)
{
bodyReportItems = reportDefinition.ReportSections[0].Body.ReportItems;
}
else
{
bodyReportItems = reportDefinition.Body.ReportItems;
}
this.SearchItems(bodyReportItems);
}
private void SearchItems(BoldReports.RDL.DOM.ReportItems items)
{
foreach (var reportItem in items)
{
this.SearchItem(reportItem);
}
}
private void SearchItem(BoldReports.RDL.DOM.ReportItem reportItem)
{
if (reportItem is BoldReports.RDL.DOM.Rectangle)
{
this.SearchItems((reportItem as BoldReports.RDL.DOM.Rectangle).ReportItems);
}
else if (reportItem is BoldReports.RDL.DOM.Tablix)
{
this.SearchItemInTablix(reportItem as BoldReports.RDL.DOM.Tablix);
}
else if (reportItem is BoldReports.RDL.DOM.TextBox)
{
var run = (reportItem as BoldReports.RDL.DOM.TextBox).Paragraphs.First().TextRuns.First();
if (run.Value == "Quarter")
{
// here you can write your code.
}
}
}
private void SearchItemInTablix(BoldReports.RDL.DOM.Tablix tablix)
{
if (tablix.TablixColumnHierarchy != null && tablix.TablixColumnHierarchy.TablixMembers != null)
{
foreach (var tablixMember in tablix.TablixColumnHierarchy.TablixMembers)
{
this.SearchItemInTablixGroupItems(tablixMember);
}
}
if (tablix.TablixRowHierarchy != null && tablix.TablixRowHierarchy.TablixMembers != null)
{
foreach (var tablixMember in tablix.TablixRowHierarchy.TablixMembers)
{
this.SearchItemInTablixGroupItems(tablixMember);
}
}
if (tablix.TablixBody != null && tablix.TablixBody.TablixRows != null)
{
foreach (var tablixRow in tablix.TablixBody.TablixRows)
{
foreach (var cell in tablixRow.TablixCells)
{
this.SearchItemInCellContents(cell.CellContents);
}
}
}
if (tablix.TablixCorner != null && tablix.TablixCorner.TablixCornerRows != null)
{
for (int i = 0; i < tablix.TablixCorner.TablixCornerRows.Count; i++)
{
BoldReports.RDL.DOM.TablixCornerCells cells = tablix.TablixCorner.TablixCornerRows[i].TablixCornerCells;
for (int j = 0; j < cells.Count; j++)
{
this.SearchItemInCellContents(cells[j].CellContents);
}
}
}
}
private void SearchItemInTablixGroupItems(BoldReports.RDL.DOM.TablixMember tablixMember)
{
if (tablixMember != null)
{
return;
}
if (tablixMember.TablixHeader != null && tablixMember.TablixHeader.CellContents != null
&& tablixMember.TablixHeader.CellContents.ReportItem != null)
{
this.SearchItemInCellContents(tablixMember.TablixHeader.CellContents);
}
if (tablixMember.TablixMembers == null && tablixMember.TablixMembers.Count >= 0)
{
foreach (var tm in tablixMember.TablixMembers)
{
this.SearchItemInTablixGroupItems(tm);
}
}
}
private void SearchItemInCellContents(BoldReports.RDL.DOM.CellContents CellContents)
{
if (CellContents != null && CellContents.ReportItem != null)
{
this.SearchItem(CellContents.ReportItem);
}
}