Prerequisites
To successfully complete this task, you will need an On-Premise version of VertiGIS Studio Reporting, version 5.1.0 or later. You will also need access to a Feature Service containing features with image attachments. Finally, you will need a basic understanding of the C# programming language.
Instructions
First, you will need to log in to your VertiGIS Studio Reporting On-Premise edition and add your desired feature service as an ArcGIS Data Source to the application.
Then you will need to create a report that targets that data source. This can be done through a layer report wizard or other methods. Instructions for creating a layer report can be found on the Doc Center (https://docs.geocortex.com/reporting/latest/help/Default.htm#rpt5/help/get-started.htm).
Once the basic components of your report are in place, you should add a Picture Box element to the design area and resize it to the desired dimensions. Make a note of the ID of your chosen Picture Box (e.g. pictureBox1), you’ll need to reference this later. It is also a good idea to edit the Sizing property of your Picture Box and change it from Normal to Zoom Image. This way, if your image does not match your element size exactly, it will be resized to fit within the boundaries of the element. More detailed instructions on the Picture Box element can be found on the Doc Center (https://docs.geocortex.com/reporting/latest/help/Default.htm#rpt5/help/images.htm).
Next, we can navigate to the Scripts section of the designer and paste the code in the script template. Looking at lines 2, 31, and 32, we’re making a reference to pictureBox1. If this is not the ID of your element, you should change it on those three lines now.
Looking again at line 2, we are making a reference to the URL of an image to show, in the scenario where the selected feature has no image attachments. You should modify that reference to point to a placeholder image of your choice.
Line 3 holds the URL to our feature layer. You must modify this to point to your data source. Note, you must make sure the URL ends with a “/”.
On line 4, we use OBJECTID as the field name for our feature’s object id. If your service’s object id field name is different, replace OBJECTID with the field name you're using.
As a final step in the Scripts section of the designer, we can check the code by using the Validate button in the toolbar. When validation is complete, you can navigate back to the main section of the designer and select your Picture Box element. Under its Properties, expand the Behavior section, and then the Scripts section within it. Use the dropdown menu next to Before Print to select Detail1_BeforePrint, which is the name of the function we’ve just written.
You may now save your report, test it in the designer by using the Object IDs of features that include image attachments, and deploy it to your Geocortex Viewer for HTML5 or Web AppBuilder for ArcGIS applications. Instructions for configuring host applications are also found on the Doc Center (https://docs.geocortex.com/reporting/latest/help/Default.htm#rpt5/help/hosting.htm).
Script Template
private void Detail1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) { pictureBox1.ImageUrl = "http://YourNoImageAvailableURLGoesHere.jpg"; var serviceUrl = "https://YourMapServiceURLGoesHere/MapServer/0/"; var fid = GetCurrentColumnValue("OBJECTID"); var attachmentsUrl = serviceUrl + fid + "/attachments?f=json"; using (var wc = new System.Net.WebClient()) { using (var stream = wc.OpenRead(attachmentsUrl)) { var serializer = new Newtonsoft.Json.JsonSerializer(); using (var sr = new System.IO.StreamReader(stream)) using (var jsonTextReader = new Newtonsoft.Json.JsonTextReader(sr)) { var response = serializer.Deserialize<Newtonsoft.Json.Linq.JObject>(jsonTextReader); var infos = response["attachmentInfos"] as Newtonsoft.Json.Linq.JArray; if (infos != null) { var info = System.Linq.Enumerable.FirstOrDefault(infos, (x) => { var contentType = (string)x["contentType"]; if (!string.IsNullOrEmpty(contentType) && contentType.StartsWith("image/")) { return true; } return false; }); if (info != null) { var imageUrl = serviceUrl + fid + "/attachments/" + info["id"].ToString(); pictureBox1.ImageUrl = imageUrl; pictureBox1.Visible = true; } } } } } }
Multiple Attachments Template
This sample shows how to handle features that have multiple attachments. Note that pictureBox declarations are literal, so you should ensure that the references here match up with your report.
private void Detail1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) { var serviceUrl = "https://YourMapServiceURLGoesHere/FeatureServer/0/"; var fid = GetCurrentColumnValue("OBJECTID"); var pictureBoxes = new System.Collections.Generic.List() { pictureBox1, pictureBox2, pictureBox3, }; foreach (var pictureBox in pictureBoxes) { pictureBox.ImageUrl = null; pictureBox.Visible = false; } var attachmentsUrl = serviceUrl + fid + "/attachments?f=json"; using (var wc = new System.Net.WebClient()) { using (var stream = wc.OpenRead(attachmentsUrl)) { var serializer = new Newtonsoft.Json.JsonSerializer(); using (var sr = new System.IO.StreamReader(stream)) using (var jsonTextReader = new Newtonsoft.Json.JsonTextReader(sr)) { var response = serializer.Deserialize(jsonTextReader); var infos = response["attachmentInfos"] as Newtonsoft.Json.Linq.JArray; if (infos != null) { foreach (var info in infos) { var contentType = (string)info["contentType"]; if (string.IsNullOrEmpty(contentType) || !contentType.StartsWith("image/")) { continue; } if (pictureBoxes.Count > 0) { var pictureBox = pictureBoxes[0]; pictureBoxes.Remove(pictureBox); var imageUrl = serviceUrl + fid + "/attachments/" + info["id"].ToString(); pictureBox.ImageUrl = imageUrl; pictureBox.Visible = true; } else { break; } } } } } } }
Comments
0 comments
Article is closed for comments.