Search code examples
c#visual-studio-2010reportdocument

add TextObject to ReportDocument in C#


I use Visual Studio 2010 SAP Crystal Reports.
I setup a Form with a Report Viewer and Load a custom designed rpt file to the Viewer's source.
It works fine.
I can even change my custom designed fields of the rpt file from code behind like this:

ReportDocument reportDocument = new ReportDocument();

string filePath = AppDomain.CurrentDomain.BaseDirectory; 

filePath = filePath.Replace("bin\\Debug\\", "MyReportClass.rpt");

reportDocument.Load(filePath);

CrystalDecisions.CrystalReports.Engine.TextObject MyText1= ((CrystalDecisions.CrystalReports.Engine.TextObject)reportDocument.ReportDefinition.Sections[3].ReportObjects["MyText1"]);

MyText1.Text = "Test Work";

MyReportViewer.ReportSource = reportDocument;

Question: How do I add a new TextObject to the ReportDocument in code behind?

I have tried:

reportDocument.Sections(2).AddTextObject(..

but that method does not exist


Solution

  • The Crystal Reports TextObject isn't that simple. You have to first create a ParagraphTextElement and place that in a ParagraphElements object, in a Paragraph object in a Paragraphs object and assign that object to the TextObject.Paragraphs property.

    Then you need to find the section of the report in the ReportDefinition to add to and then add that object to the section via the ReportDefinition.

    Enough explaining though, here's my using statements:

    #region Using
    
    using System;
    using System.Windows;
    
    using CrystalDecisions.ReportAppServer.Controllers;
    using CrystalDecisions.ReportAppServer.ReportDefModel;
    
    #endregion
    

    And here's the code that I got it working with:

    var reportDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
    
    var filePath = AppDomain.CurrentDomain.BaseDirectory;
    filePath = filePath.Replace("bin\\Debug\\", "MyReport.rpt");
    reportDocument.Load(filePath);
    
    ReportDefController2 reportDefinitionController = reportDocument.ReportClientDocument.ReportDefController;
    
    ISCRParagraphTextElement paraTextElementClass = new ParagraphTextElement { Text = "Text Work", Kind = CrParagraphElementKindEnum.crParagraphElementKindText };
    
    ParagraphElements paragraphElements = new ParagraphElements();
    paragraphElements.Add(paraTextElementClass);
    
    Paragraph paragraph = new Paragraph();
    paragraph.ParagraphElements = paragraphElements;
    
    Paragraphs paragraphs = new Paragraphs();
    paragraphs.Add(paragraph);
    
    TextObject newTextObject = new TextObject();
    newTextObject.Paragraphs = paragraphs;
    
    var detailSection = reportDefinitionController.ReportDefinition.DetailArea.Sections[0];
    reportDefinitionController.ReportObjectController.Add(newTextObject, detailSection, 0);
    
    // MyReportViewer.ReportSource = reportDocument;
    this.crystalReportsViewer.ViewerCore.ReportSource = reportDocument;
    

    The last line is different because I'm using a WPF app to do my testing in. I hope it helps!