The smart way to insert a text box in existing Crystal Reports using .Net
Insert a Textbox in existing Crystal Reports : The smart way
· 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 continuation of the story of Jason, a meager trainee in a middle level organization, who was generally given boring and lengthy manual work. But he always tried to make his job fast, intelligent and interesting.
This week Mr. Murphy told him that he needed to update all the 700+ Crystal reports used by their organization. There was a change in the calculation of service tax as per the new law just passed. All customers needed to be informed that the service tax was being calculated as per the latest law. So the task given to Jason was:-
1) Open each report.
2) Add a disclaimer in the form of a text box at the bottom of the report footer containing the text ”Service Taxes are calculated as per Service Tax Act: 2011”
As usual, Jason planned to achieve this task through .Net code. The first thing he needed was a textbox object that he could insert in to his ReportDocument object. But to his dismay he found that the “Text” property of TextObject was either inaccessible or read-only. So he had to create quite a few objects( Paragraphs collection, Paragraph, ParagraphElements collection, ParagraphElement) for the simple task of getting a TextBox containing the necessary Text in it. He created a Function GetFooterText that would return the necessary TextBox.
Function GetFooterText() As CrystalDecisions.ReportAppServer.ReportDefModel.TextObject
Dim myFooterText As CrystalDecisions.ReportAppServer.ReportDefModel.TextObject
Dim myParagraphs As CrystalDecisions.ReportAppServer.ReportDefModel.Paragraphs
Dim myPara As CrystalDecisions.ReportAppServer.ReportDefModel.Paragraph
Dim myParaElements As CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphElements
Dim myParaTextElement As CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphTextElement
Dim myFont As CrystalDecisions.ReportAppServer.ReportDefModel.Font
myFooterText = New CrystalDecisions.ReportAppServer.ReportDefModel.TextObject()
myParagraphs = New CrystalDecisions.ReportAppServer.ReportDefModel.Paragraphs()
myPara = New CrystalDecisions.ReportAppServer.ReportDefModel.Paragraph()
myParaElements = New CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphElements()
myParaTextElement = New CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphTextElement()
myFont = New CrystalDecisions.ReportAppServer.ReportDefModel.Font()
myParaTextElement.Text = “Service Taxes are calculated as per Service Tax Act: 2011”
myFont.Name = “Arial”
myParaTextElement.FontColor.Font = myFont
myParaTextElement.FontColor.Font.Size = 8
myPara.ParagraphElements = myParaElements
myFooterText.Paragraphs = myParagraphs
myFooterText.Name = “txt_Disclaimer”
Next Jason proceeded to write the code for the simple VB.Net application with a single button. The following functions were to be performed on the click of the button.
1. The “disclaimer” object is retrieved by calling the function GetFooterText.
2. The Crystal Report file is loaded to rdoc and then accessed via rasClientDoc so that it can be modified.
3. The last report footer section is selected (for cases where the report might contain multiple report sections).
4. All the objects in the report footer section are examined to find which is the lowest. This will help ensure that the disclaimer appears below the lowest object.
5. The size and position properties of the textbox are set.
6. The “disclaimer” is added to the report footer section.
7. The report is saved.
Private Sub Add_Disclaimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add_Disclaimer.Click
Dim rdoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim rasClientDoc As CrystalDecisions.ReportAppServer.ClientDoc.ISCDClientDocument
Dim disclaimer As CrystalDecisions.ReportAppServer.ReportDefModel.TextObject
Dim oldReportFooter As CrystalDecisions.ReportAppServer.ReportDefModel.Section
Dim i, j, k As Integer
‘get the prepared text box from a seperate function
disclaimer = GetFooterText()
‘Start report file processing
rdoc = New CrystalDecisions.CrystalReports.Engine.ReportDocument
‘Load single report- can run this within a loop to load and process all reports in a folder
rasClientDoc = rdoc.ReportClientDocument
‘Find the last Report footer section number
i = rasClientDoc.ReportDefController.ReportDefinition.ReportFooterArea.Sections.Count() – 1
oldReportFooter = rasClientDoc.ReportDefController.ReportDefinition.ReportFooterArea.Sections(i)
‘The disclaimer needs to be placed below other objects in report footer section
For j = 0 To rasClientDoc.ReportDefController.ReportDefinition.ReportFooterArea.Sections(i).ReportObjects.Count – 1
If k<rasClientDoc.ReportDefController.ReportDefinition.ReportFooterArea.Sections(i).ReportObjects(j).Top Then
k = rasClientDoc.ReportDefController.ReportDefinition.ReportFooterArea.Sections(i).ReportObjects(j).Top +
‘Modify some properties of the text Box Object
disclaimer.Top = k + 50
disclaimer.Width = 5000
disclaimer.Height = 200
rasClientDoc.ReportDefController.ReportObjectController.Add(disclaimer, oldReportFooter, -1)
1. This code snippet deals with a single report. Multiple reports can be called in a loop and updated at one go. This logic is dealt in my previous blog.
2. The above automation shall be very useful when similar changes are to be made to a very large number of reports.
3. The above automation can be used in cases of logo modification, Report Title modification with slight change in code.