Skip to content

ProSnippets Reports

uma2526 edited this page Dec 15, 2020 · 12 revisions
Language:              C#  
Subject:               Reports  
Contributor:           ArcGIS Pro SDK Team <arcgisprosdk@esri.com>  
Organization:          esri, http://www.esri.com  
Date:                  12/11/2020  
ArcGIS Pro:            2.7  
Visual Studio:         2017, 2019  
.NET Target Framework: 4.8  

Report Project Items

Gets all the reports in the current project

var projectReports = Project.Current.GetItems<ReportProjectItem>();
foreach (var reportItem in projectReports)
{
    //Do Something with the report
}

Get a specific report

ReportProjectItem reportProjItem = Project.Current.GetItems<ReportProjectItem>().FirstOrDefault(item => item.Name.Equals(reportName));
return reportProjItem?.GetReport();

Create Report

Create report

//Note: Call within QueuedTask.Run()
//The fields in the datasource used for the report
//This uses a US Cities dataset
var listFields = new List<CIMReportField> {
    //Grouping should be the first field
    new CIMReportField{Name = "STATE_NAME", FieldOrder = 0, Group = true, SortInfo = FieldSortInfo.Desc}, //Group cities using STATES
    new CIMReportField{Name = "CITY_NAME", FieldOrder = 1},
    new CIMReportField{Name = "POP1990", FieldOrder = 2, },
};
//Definition query to use for the data source
var defQuery = "STATE_NAME LIKE 'C%'";
//Define the Datasource
//pass true to use the selection set
var reportDataSource = new ReportDataSource(featureLayer, defQuery, false, listFields);
//The CIMPage defintion - page size, units, etc
var cimReportPage = new CIMPage
{
    Height = 11,
    StretchElements = false,
    Width = 6.5,
    ShowRulers = true,
    ShowGuides = true,
    Margin = new CIMMargin { Bottom = 1, Left = 1, Right = 1, Top = 1 },
    Units = LinearUnit.Inches
};

//Report template
var reportTemplates = await ReportTemplateManager.GetTemplatesAsync();
var reportTemplate = reportTemplates.Where(r => r.Name == "Attribute List with Grouping").First();

//Report Styling
var reportStyles = await ReportStylingManager.GetStylingsAsync();
var reportStyle = reportStyles.Where(s => s == "Cool Tones").First();

//Field Statistics
var fieldStatisticsList = new List<ReportFieldStatistic> {
    new ReportFieldStatistic{ Field = "POP1990", Statistic = FieldStatisticsFlag.Sum}
    //Note: NoStatistics option for FieldStatisticsFlag is not supported.
};
var report = ReportFactory.Instance.CreateReport("USAReport", reportDataSource, cimReportPage, fieldStatisticsList, reportTemplate, reportStyle);

Export report to pdf

//Note: Call within QueuedTask.Run()
//Define Export Options
var exportOptions = new ReportExportOptions
{
    ExportPageOption = ExportPageOptions.ExportAllPages,
    TotalPageNumberOverride = 0

};
//Create PDF format with appropriate settings
PDFFormat pdfFormat = new PDFFormat();
pdfFormat.Resolution = 300;
pdfFormat.OutputFileName = path;
report.ExportToPDF($"{report.Name}", pdfFormat, exportOptions, useSelection);

Import a report file

//Note: Call within QueuedTask.Run()
Item reportToImport = ItemFactory.Instance.Create(reportFile);
Project.Current.AddItem(reportToImport as IProjectItem);

Delete a report

//Note: Call within QueuedTask.Run()
//Reference a reportitem in a project by name
ReportProjectItem reportItem = Project.Current.GetItems<ReportProjectItem>().FirstOrDefault(item => item.Name.Equals(reportName));

//Check for report item
if (reportItem == null)
    return Task.FromResult<bool>(false);

//Delete the report from the project
return Task.FromResult<bool>(Project.Current.RemoveItem(reportItem));

Modify Reports

Rename Report

//Note: Call within QueuedTask.Run()
ReportProjectItem reportProjItem = Project.Current.GetItems<ReportProjectItem>().FirstOrDefault(item => item.Name.Equals(reportName));
reportProjItem.GetReport().SetName("RenamedReport");

Modify the Report datasource

//Note: Call within QueuedTask.Run()
//Remove Groups
// The fields in the datasource used for the report
var listFields = new List<string> {
 "STATE_NAME"
};
report.RemoveGroups(listFields);

//Add Group
report.AddGroup("STATE_NAME", true, true, "");

//Modify the Definition Query
var defQuery = "STATE_NAME LIKE 'C%'";
report.SetDefinitionQuery(defQuery);

Modify the report Page

//Note: Call within QueuedTask.Run()
var cimReportPage = new CIMPage
{
    Height = 12,
    StretchElements = false,
    Width = 6.5,
    ShowRulers = true,
    ShowGuides = true,
    Margin = new CIMMargin { Bottom = 1, Left = 1, Right = 1, Top = 1 },
    Units = LinearUnit.Inches
};
report.SetPage(cimReportPage);
//Change only the report's page height
report.SetPageHeight(12);

Report Design

Get a report template

//Report Template Styles:
//Attribute List
//Attribute List with Grouping
//Basic Summary
//Basic Summary with Grouping
//Page Per Feature
var reportTemplates = await ReportTemplateManager.GetTemplatesAsync();
var reportTemplate = reportTemplates.Where(r => r.Name == reportTemplateName).First();

Get a report styling

//Report Styling:
//Black and White
//Cool Tones
//Warm Tones
var reportStyles = await ReportStylingManager.GetStylingsAsync();
var reportStyle = reportStyles.Where(s => s == reportStyleName).First();

Element Factory

Find the active report view

Report report = Project.Current.GetItems<ReportProjectItem>().FirstOrDefault().GetReport();
var reportPane = FrameworkApplication.Panes.FindReportPanes(report).Last();
if (reportPane == null)
  return;

ReportView reportView = reportPane.ReportView;

Refresh the report view

if (reportView == null)
  return;

QueuedTask.Run(() =>
{
  reportView.Refresh();
});

Select all elements

var pageFooterSection = report.Elements.Where(elm => elm is ReportPageFooter).FirstOrDefault() as ReportPageFooter;

pageFooterSection.SelectAllElements();

Zoom to selected elements

//Process on worker thread
 QueuedTask.Run(() =>
{
  reportView.ZoomToSelectedElements();
});

Clear element selection

reportView.ClearElementSelection();

Zoom to whole page

//Process on worker thread
QueuedTask.Run(() =>
{
  reportView.ZoomToWholePage();
});

Select elements

// Select text elements from the report footer
var reportFooterSection = report.Elements.Where(elm => elm is ReportFooter).FirstOrDefault() as ReportFooter;
var elements = reportFooterSection.GetElementsAsFlattenedList();

reportFooterSection.SelectElements(elements);

Get selected elements

IReadOnlyList<Element> selectedElements = reportFooterSection.GetSelectedElements();

Refresh report view

//Process on worker thread
QueuedTask.Run(() =>
{
  reportView.Refresh();
});

Zoom to

var detailsSection = report.Elements.Where(elm => elm is ReportDetails).FirstOrDefault() as ReportDetails;
var bounds = detailsSection.GetBounds();

Coordinate2D ll = new Coordinate2D(bounds.XMin, bounds.YMin);
Coordinate2D ur = new Coordinate2D(bounds.XMax, bounds.YMax);
Envelope env = EnvelopeBuilder.CreateEnvelope(ll, ur);

//Process on worker thread
QueuedTask.Run(() =>
{
  reportView.ZoomTo(env);
});

Zoom to page width

//Process on worker thread
QueuedTask.Run(() =>
{
  reportView.ZoomToPageWidth();
});
Clone this wiki locally