//Open a report project item in a new view.//A report project item may exist but it may not be open in a view. //Reference a report project item by nameReportProjectItemreportPrjItem= Project.Current.GetItems<ReportProjectItem>().FirstOrDefault(item => item.Name.Equals("MyReport"));//Get the report associated with the report project itemReportreportToOpen=await QueuedTask.Run(()=> reportPrjItem.GetReport());//Create the new paneIReportPaneiNewReporttPane=await ProApp.Panes.CreateReportPaneAsync(reportToOpen);//GUI thread
Activate an already open report view
Reportreport= Project.Current.GetItems<ReportProjectItem>().FirstOrDefault().GetReport();varreportPane= FrameworkApplication.Panes.FindReportPanes(report).Last();if(reportPane==null)return;//Activate the pane(reportPane as ArcGIS.Desktop.Framework.Contracts.Pane).Activate();//Get the "ReportView" associated with the Report Pane.ReportViewreportView= reportPane.ReportView;
Reference the active report view
//Confirm if the current, active view is a report view. If it is, do something.ReportViewactiveReportView= ReportView.Active;if(activeReportView!=null){// do something}
//On the QueuedTaskvardetailsSection= report.Elements.OfType<ReportSection>().FirstOrDefault().Elements.OfType<ReportDetails>().FirstOrDefault();varbounds= detailsSection.GetBounds();
ReportView.Active.ZoomTo(bounds);
Zoom to page width
//Process on worker thread
QueuedTask.Run(()=> reportView.ZoomToPageWidth());
Create Report
Create report
//Note: Call within QueuedTask.Run()//The fields in the datasource used for the report//This uses a US Cities datasetvarlistFields=newList<CIMReportField>{//Grouping should be the first fieldnew CIMReportField{Name="STATE_NAME",FieldOrder=0,Group=true,SortInfo= FieldSortInfo.Desc},//Group cities using STATESnew CIMReportField{Name="CITY_NAME",FieldOrder=1},new CIMReportField{Name="POP1990",FieldOrder=2,},};//Definition query to use for the data sourcevardefQuery="STATE_NAME LIKE 'C%'";//Define the DatasourcevarreportDataSource=new ReportDataSource(featureLayer, defQuery, listFields);//The CIMPage defintion - page size, units, etcvarcimReportPage=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 templatevarreportTemplates=await ReportTemplateManager.GetTemplatesAsync();varreportTemplate= reportTemplates.Where(r => r.Name =="Attribute List with Grouping").First();//Report StylingvarreportStyles=await ReportStylingManager.GetStylingsAsync();varreportStyle= reportStyles.Where(s =>s=="Cool Tones").First();//Field StatisticsvarfieldStatisticsList=newList<ReportFieldStatistic>{new ReportFieldStatistic{Field="POP1990",Statistic= FieldStatisticsFlag.Sum}//Note: NoStatistics option for FieldStatisticsFlag is not supported.};varreport= ReportFactory.Instance.CreateReport("USAReport", reportDataSource, cimReportPage, fieldStatisticsList, reportTemplate, reportStyle);
Export report to pdf
//Note: Call within QueuedTask.Run()//Define Export OptionsvarexportOptions=new ReportExportOptions
{ExportPageOption= ExportPageOptions.ExportAllPages,TotalPageNumberOverride=0};//Create PDF format with appropriate settingsPDFFormatpdfFormat=new PDFFormat();
pdfFormat.Resolution =300;
pdfFormat.OutputFileName =path;
report.ExportToPDF($"{report.Name}", pdfFormat, exportOptions, useSelection);
Import a report file
//Note: Call within QueuedTask.Run()ItemreportToImport= 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 nameReportProjectItemreportItem= Project.Current.GetItems<ReportProjectItem>().FirstOrDefault(item => item.Name.Equals(reportName));//Check for report itemif(reportItem==null)return Task.FromResult<bool>(false);//Delete the report from the projectreturn Task.FromResult<bool>(Project.Current.RemoveItem(reportItem));
Modify Reports
Rename Report
//Note: Call within QueuedTask.Run()ReportProjectItemreportProjItem= Project.Current.GetItems<ReportProjectItem>().FirstOrDefault(item => item.Name.Equals(reportName));
reportProjItem.GetReport().SetName("RenamedReport");
Modify the Report DefinitionQuery
//Note: Call within QueuedTask.Run()//Remove Groups// The fields in the datasource used for the reportvarlistFields=newList<string>{"STATE_NAME"};
report.RemoveGroups(listFields);//Add Group
report.AddGroup("STATE_NAME",true,true,"");//Modify the Definition QueryvardefQuery="STATE_NAME LIKE 'C%'";
report.SetDefinitionQuery(defQuery);
Modify the report Page
//Note: Call within QueuedTask.Run()varcimReportPage=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);
Add SubReport
//Note: Call within QueuedTask.Run()varmainReport= Project.Current.GetItems<ReportProjectItem>().FirstOrDefault(r => r.Name =="USAReports")?.GetReport();if(mainReport==null)return;//Add sub reportvarvermontReportItem= Project.Current.GetItems<ReportProjectItem>().FirstOrDefault(r => r.Name =="Vermont");if(vermontReportItem==null)return;ReportvermontReport= vermontReportItem.GetReport();
mainReport.AddSubReport(vermontReportItem,-1,true);// If -1, the subreport is added to the end of the report.
Report Design
Get a report template
//Report Template Styles://Attribute List//Attribute List with Grouping//Basic Summary//Basic Summary with Grouping//Page Per FeaturevarreportTemplates=await ReportTemplateManager.GetTemplatesAsync();varreportTemplate= reportTemplates.Where(r => r.Name ==reportTemplateName).First();
Get a report styling
//Report Styling://Black and White//Cool Tones//Warm TonesvarreportStyles=await ReportStylingManager.GetStylingsAsync();varreportStyle= reportStyles.Where(s =>s==reportStyleName).First();
Report Elements
Get various Report sections
//Get the "ReportSection element"//ReportSectionElement contains the ReportHeader, ReportPageHeader, ReportDetails. ReportPageFooter, ReportFooter sections.varmainReportSection= report.Elements.OfType<ReportSection>().FirstOrDefault();//Get the ReportHeadervarreportHeader= mainReportSection?.Elements.OfType<ReportHeader>().FirstOrDefault();//Get the ReportHeadervarreportPageHeader= mainReportSection?.Elements.OfType<ReportPageHeader>().FirstOrDefault();//Get the "ReportDetails" within the ReportSectionElement. ReportDetails is where "fields" are.varreportDetailsSection= mainReportSection?.Elements.OfType<ReportDetails>().FirstOrDefault();//Get the ReportPageFootervarreportPageFooter= mainReportSection?.Elements.OfType<ReportPageFooter>().FirstOrDefault();//Get the ReportFootervarreportFooter= mainReportSection?.Elements.OfType<ReportFooter>().FirstOrDefault();
Select elements
//ReportDetailsSection contains the "Fields"varelements= reportDetailsSection.Elements;
reportDetailsSection.SelectElements(elements);
Select all elements
//Select all elements in the Report Footer.ReportPageFooterpageFooterSection= report.Elements.OfType<ReportSection>().FirstOrDefault().Elements.OfType<ReportPageFooter>().FirstOrDefault();
pageFooterSection.SelectAllElements();
Get selected elements
IReadOnlyList<Element>selectedElements= report.GetSelectedElements();//Can also use the active ReportViewIReadOnlyList<Element>selectedElementsFromView= ReportView.Active.GetSelectedElements();
//This is the gap between two fieldsdoublefieldIncrement=0.9388875113593206276389;//On the QueuedTask//New field to add.varnewReportField=new CIMReportField
{Name="POP1990",FieldOrder=2,};//Get the "ReportSection element" varmainReportSection= report.Elements.OfType<ReportSection>().FirstOrDefault();if(mainReportSection==null)return;//Get the "ReportDetails" within the ReportSectionElement. ReportDetails is where "fields" are.varreportDetailsSection= mainReportSection?.Elements.OfType<ReportDetails>().FirstOrDefault();if(reportDetailsSection==null)return;//Within ReportDetails find the envelope that encloses a field.//We get the first CIMParagraphTextGraphic in the collection so that we can add the new field next to it. varlastFieldGraphic= reportDetailsSection.Elements.FirstOrDefault((r)=>{vargr= r as GraphicElement;if(gr==null)returnfalse;return(gr.GetGraphic()is CIMParagraphTextGraphic ?true:false);});//Get the Envelope of the last fieldvargraphicBounds= lastFieldGraphic.GetBounds();//Min and Max values of the envelopevarxMinOfFieldEnvelope= graphicBounds.XMin;varyMinOfFieldEnvelope= graphicBounds.YMin;varxMaxOfFieldEnvelope= graphicBounds.XMax;varYMaxOfFieldEnvelope= graphicBounds.YMax;//create the new Envelope to be offset from the existing field//At 2.x//MapPoint newMinPoint = MapPointBuilder.CreateMapPoint(xMinOfFieldEnvelope + fieldIncrement, yMinOfFieldEnvelope);//MapPoint newMaxPoint = MapPointBuilder.CreateMapPoint(xMaxOfFieldEnvelope + fieldIncrement, YMaxOfFieldEnvelope);//Envelope newFieldEnvelope = EnvelopeBuilder.CreateEnvelope(newMinPoint, newMaxPoint);MapPointnewMinPoint= MapPointBuilderEx.CreateMapPoint(xMinOfFieldEnvelope+fieldIncrement, yMinOfFieldEnvelope);MapPointnewMaxPoint= MapPointBuilderEx.CreateMapPoint(xMaxOfFieldEnvelope+fieldIncrement, YMaxOfFieldEnvelope);EnvelopenewFieldEnvelope= EnvelopeBuilderEx.CreateEnvelope(newMinPoint, newMaxPoint);//Create fieldGraphicElementfieldGraphic= ReportElementFactory.Instance.CreateFieldValueTextElement(reportDetailsSection, newFieldEnvelope, newReportField);