-
Notifications
You must be signed in to change notification settings - Fork 117
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
var projectReports = Project.Current.GetItems<ReportProjectItem>();
foreach (var reportItem in projectReports)
{
//Do Something with the report
}
ReportProjectItem reportProjItem = Project.Current.GetItems<ReportProjectItem>().FirstOrDefault(item => item.Name.Equals(reportName));
return reportProjItem?.GetReport();
//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);
//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);
//Note: Call within QueuedTask.Run()
Item reportToImport = ItemFactory.Instance.Create(reportFile);
Project.Current.AddItem(reportToImport as IProjectItem);
//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));
//Note: Call within QueuedTask.Run()
ReportProjectItem reportProjItem = Project.Current.GetItems<ReportProjectItem>().FirstOrDefault(item => item.Name.Equals(reportName));
reportProjItem.GetReport().SetName("RenamedReport");
//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);
//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 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();
//Report Styling:
//Black and White
//Cool Tones
//Warm Tones
var reportStyles = await ReportStylingManager.GetStylingsAsync();
var reportStyle = reportStyles.Where(s => s == reportStyleName).First();
Report report = Project.Current.GetItems<ReportProjectItem>().FirstOrDefault().GetReport();
var reportPane = FrameworkApplication.Panes.FindReportPanes(report).Last();
if (reportPane == null)
return;
ReportView reportView = reportPane.ReportView;
if (reportView == null)
return;
QueuedTask.Run(() =>
{
reportView.Refresh();
});
var pageFooterSection = report.Elements.Where(elm => elm is ReportPageFooter).FirstOrDefault() as ReportPageFooter;
pageFooterSection.SelectAllElements();
//Process on worker thread
QueuedTask.Run(() =>
{
reportView.ZoomToSelectedElements();
});
reportView.ClearElementSelection();
//Process on worker thread
QueuedTask.Run(() =>
{
reportView.ZoomToWholePage();
});
// 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);
IReadOnlyList<Element> selectedElements = reportFooterSection.GetSelectedElements();
//Process on worker thread
QueuedTask.Run(() =>
{
reportView.Refresh();
});
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);
});
//Process on worker thread
QueuedTask.Run(() =>
{
reportView.ZoomToPageWidth();
});
Home | API Reference | Requirements | Download | Samples
-
Gets all the reports in the current project
-
Get a specific report
-
Open a Report project item in a new view
-
Activate an already open report view
-
Reference the active report view
-
Refresh the report view
-
Zoom to whole page
-
Zoom to specific location on Report view
-
Zoom to page width