Controlling Report column width using SDK

135
5
10-02-2019 11:43 AM
RebeccaPenman
New Contributor

I want to create and export a report on a single button click, but I can’t figure out how to control the report's column widths using the SDK. The values in my fields are always truncated because the default width is too narrow. I’ve tried setting the field Width property, but that didn't seem to do anything. 

Reply
0 Kudos
5 Replies
UmaHarano
Esri Regular Contributor

Hi Rebecca

At present, there is no way to modify the report field width using the public API. Report enhancements will happen with the Pro 2.6 release. In the meantime, the report field can be lengthened by using the CIM.  A code snippet to accomplish this is provided below.

Thanks!

Uma

        protected async override void OnClick()
{
ReportProjectItem reportProjItem = Project.Current.GetItems<ReportProjectItem>().FirstOrDefault(item => item.Name.Equals("Test"));
Report report = null;
await QueuedTask.Run(() => {
report = reportProjItem?.GetReport();
var reportDfn = report.GetDefinition();
var reportSection = reportDfn.Elements[0] as CIMReportSection;
var reportSectionElements = reportSection.Elements;
//we need CIMReportDetails element
CIMReportDetails reportDetails = null;
foreach (var reportSectionElement in reportSectionElements)
{
if (reportSectionElement is CIMReportDetails)
reportDetails = reportSectionElement as CIMReportDetails;
}
CIMParagraphTextGraphic cimParagraphTextGraphicElement = null;
foreach (var reportDetail in reportDetails.Elements)
{
CIMGraphicElement cimGraphicElement = null;
//Check if the "Graphic" in this CIMElement is of type ParagraphicTextGraphic
//And check if the "Text" for this is the field we want.
if (reportDetail is CIMGraphicElement)
{
cimGraphicElement = reportDetail as CIMGraphicElement;
if (cimGraphicElement.Graphic is CIMParagraphTextGraphic) //This is a field
{
//Now check if it is the field we want
cimParagraphTextGraphicElement = cimGraphicElement.Graphic as CIMParagraphTextGraphic;
if (cimParagraphTextGraphicElement.Text.Contains("field-value") && cimParagraphTextGraphicElement.Text.Contains("FieldName"))
{
var textPolygonFrame = cimParagraphTextGraphicElement.Shape as ArcGIS.Core.Geometry.Polygon;
//This where are making the text frame for the field wider by adding 5 units.
cimParagraphTextGraphicElement.Shape = MakeNewTextFrame(textPolygonFrame, 5.0);
}
}
}

}
report.SetDefinition(reportDfn); //Set the modified report cim.
});
}

//Helper function that takes the existing text frame of the field and makes it wider.
private ArcGIS.Core.Geometry.Polygon MakeNewTextFrame(ArcGIS.Core.Geometry.Polygon existingFrame, double additionalWidth)
{
List<MapPoint> listCornerPoints = new List<MapPoint>();
int cornerIndex = 1;
foreach (var cornerPt in existingFrame.Points)
{
//We want to make the frame wider so we have to add additional width to some of the X values of the frame corners.
//The corner points start with the top right and travel counter clockwise.
//Therefore to change the width, we have to modify corners 1, 4 and 5. 5th and 1st corner are coincident.
switch (cornerIndex)
{
case 1:
case 4:
case 5:
//For these we have to add the width
listCornerPoints.Add(MapPointBuilder.CreateMapPoint(cornerPt.X + additionalWidth, cornerPt.Y));
break;
default:
listCornerPoints.Add(MapPointBuilder.CreateMapPoint(cornerPt.X, cornerPt.Y));
break;

}
cornerIndex++;
}

ArcGIS.Core.Geometry.Polygon newWiderTextFrame = PolygonBuilder.CreatePolygon(listCornerPoints, existingFrame.SpatialReference);
return newWiderTextFrame;
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Reply
0 Kudos
RebeccaPenman
New Contributor

Uma,

Great, I thought I’d need to use the CIM, but couldn’t figure out the right variables. This should really help.

Thanks,

Becky

Reply
0 Kudos
RebeccaPenman
New Contributor

Uma,

When I implement your code to widen a text frame, I lose any data in that frame. Is there a step I'm missing? Does the data source need to be reloaded? 

Reply
0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Rebecca,

 Can you explain the workflow you implemented to in order to change the report's layout and run it?  Also how many fields do you have and which field's frame are you widening (left, center, right most)?    As for the code above we created the report (with one column only), next we ran the snippet above, finally we exported the report.  The text widening worked fine (from 1 inch to 5 in our case).  To make sure that your text frames are not overlapping or going outside the report area, you can iterate through all cimParagraphTextGraphicElement items with a text containing "field-value" (these are the field values in your detail report section only) and output the textPolygonFrame's corners.

Reply
0 Kudos
RebeccaPenman
New Contributor

Hi Wolf,

 

I have only one field in my test report (and no groupings). I added a button to run your code so I could replicate all of your steps. All I changed in your code was "FieldName". When I click on “Details Field Value Text” before and after running your code, I can see that the column polygon in my design does increase in size when I run your code. I am adding just one unit to ensure it doesn't fall outside the report area and it looks like it can fit easily on the line.  When I export to PDF, however, I get a report with empty values. It is multi-pages, so it is filling with something.  The field name in the design changes to a red ellipsis after running the column fix--that can't be good. The new polygon must be overlapping, but I don’t see where to fix it.

 

When I export my report to PDF without your fix, I get values, but they are truncated. If I manually increase the column width, my report exports fine. I just can’t get the column fix to work using the SDK.

 

Any ideas?

 

Thanks,

Rebecca

Reply
0 Kudos