The smart way to change the font in existing Crystal Reports using .Net
- SAP CRYSTAL REPORTS FOR VISUAL STUDIO 2010
- Visual Studio 2010
- Assembly Info: CrystalDecisions.CrystalReports.Engine.dll, CrystalDecisions.ReportSource.dll, CrystalDecisions.Shared.dll, CrystalDecisions.Windows.Forms.dll, CrystalDecisions.ReportAppServer.ReportDefModel.dll, CrystalDecisions.ReportAppServer.DataDefModel.dll, CrystalDecisions.ReportAppServer.ClientDoc.dll,
This is a continuation of the story of Jason who was a meager trainee in a middle level organization. He was generally given boring and lengthy manual work, but he always tried to make his job fast, intelligent and interesting.
Some time back, he had generated an excel sheet containing details of all the 762 crystal reports used by his organization. This week Mr. Murphy told him that the higher management had decided to deploy these reports for the far-east operations based on his analysis. So, a minor change needed to be made for all the reports. The font for all the database field objects in the detail section needed to be changed to “Arial Unicode MS” so that there was no problem displaying multi-language data. So Jason was required to right-click on each field object, go to fonts tab and change the font name to “Arial Unicode MS”.
The task was simple no doubt, but Jason was not interested in doing so much manual work, and again planned to make all the changes by a simple button click in his automated VB dot net application.
So the list of events that occurred when he clicked on the button would be as follows:
- First he needed to select the folder where the report files were placed. He used the FolderBrowserDialog Control for this.
- Once the proper directory was selected, only the “*.rpt” files needed to be processed.
- To read the report from the folder, he would use the CrystalDecisions.CrystalReports.Engine.ReportDocument object but when he proceeded with modifying the report he would need to use the rasClientDoc object which is an instance of CrystalDecisions.ReportAppServer.ClientDoc.ISCDClientDocument
- Each object in the 1st detail section is first copied to oldObject
- Then it is checked whether it is a FieldObject, and if true then its font is changed.
- After font change the modified object is saved to rasClientDoc object.
- When all objects are processed, then the rasClientDoc.Save() method saves the changes permanently to the Crystal Report file.
Dim folderPath As String
Dim fileNames As Object
Dim rdoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim rasClientDoc As CrystalDecisions.ReportAppServer.ClientDoc.ISCDClientDocument
Dim oldObject As CrystalDecisions.ReportAppServer.ReportDefModel.ReportObject
Dim changedObject As CrystalDecisions.ReportAppServer.ReportDefModel.ReportObject
Dim myFont As CrystalDecisions.ReportAppServer.ReportDefModel.Font
‘This is the Font to be set
myFont = New CrystalDecisions.ReportAppServer.ReportDefModel.Font
myFont.Name = “Arial Unicode MS”
FolderBrowserDialog1.SelectedPath = “c:\”
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
folderPath = FolderBrowserDialog1.SelectedPath
‘Only report files from selected directory are to be analysed
fileNames = My.Computer.FileSystem.GetFiles(
folderPath, FileIO.SearchOption.SearchTopLevelOnly, “*.rpt”)
For Each fileName As String In fileNames
rdoc = New CrystalDecisions.CrystalReports.Engine.ReportDocument
rasClientDoc = rdoc.ReportClientDocument
‘Iterate through all objects in the detail section
For i = 0 To rasClientDoc.ReportDefController.ReportDefinition.DetailArea.Sections(0).ReportObjects.Count() – 1
oldObject = CType(rasClientDoc.ReportDefController.ReportDefinition.DetailArea.Sections(0).ReportObjects(i), CrystalDecisions.ReportAppServer.ReportDefModel.ReportObject)
‘Check whether the reportobject is a database field, then change its font
If oldObject.Kind = CrystalDecisions.ReportAppServer.ReportDefModel.CrReportObjectKindEnum.crReportObjectKindField Then
changedObject = CType(rasClientDoc.ReportDefController.ReportDefinition.DetailArea.Sections(0).ReportObjects(i).Clone(True), CrystalDecisions.ReportAppServer.ReportDefModel.FieldObject)
changedObject.FontColor.Font.Name = myFont.Name
- This code snippet deals with a single detail section. Multiple detail sections can be dealt with by adding another For- Next loop ( eg: For j = 0 To rasClientDoc.ReportDefController.ReportDefinition.DetailArea.Sections.Count() – 1)
- The above code snippet may require to include other sections like report header, page header, group headers and footers when implementing this in practice.
- Sometimes it is required to do a change in font for a bulk of reports when deploying them for a different country/language. In such cases a simple automation will save a lot of manual activity.