C#在水晶报表中以编程方式创建页眉部分

时间:2013-12-17 06:29:22

标签: sql-server-2008 c#-4.0 crystal-reports-2010 crystal-reports-viewer

每当我预览报告时,我想在水晶报告中显示水印文字。 为此,我创建了新的段落对象,新的字段(文本框)对象,然后使用C#以编程方式插入到水晶报表中,
下面是代码

CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rptClientDoc1 = cryRpt.ReportClientDocument;

        ISCRParagraphTextElement paragraphTextElement = new ParagraphTextElementClass();
        paragraphTextElement.Text = "TESTING";

        paragraphTextElement.Kind = CrParagraphElementKindEnum.crParagraphElementKindText;

        ParagraphElements paragraphElements = new ParagraphElementsClass();
        paragraphElements.Add(paragraphTextElement);

        Paragraph paragraph = new ParagraphClass();
        paragraph.Alignment = CrAlignmentEnum.crAlignmentHorizontalCenter;
        paragraph.ParagraphElements = paragraphElements;

        Paragraphs paragraphs = new ParagraphsClass();
        paragraphs.Add(paragraph);

        ISCRTextObject textObject = new TextObjectClass();
        textObject.Paragraphs = paragraphs;
        textObject.FontColor.Font.Bold = true;
        textObject.FontColor.Font.Size = 48;
        textObject.FontColor.Font.Italic = true;
        textObject.FontColor.Color = uint.Parse(ColorTranslator.ToOle(Color.Gray).ToString());
        textObject.FontColor.Font.Charset = 30;
        textObject.Height = 8000;
        textObject.Top = this.Height;
        textObject.Left = 5000;
        textObject.Width = 1500;

        textObject.Format.TextRotationAngle = CrTextRotationAngleEnum.crRotationAngleRotate90;

        ReportDefController2 reportDef = rptClientDoc1.ReportDefController;

        ReportSectionController reportSectionController = rptClientDoc1.ReportDefController.ReportSectionController;

        CrystalDecisions.ReportAppServer.ReportDefModel.Section newsec;

        int index = reportDef.ReportDefinition.PageHeaderArea.Sections.Count;
        if (index > 0)
        {               
            newsec = reportDef.ReportDefinition.PageHeaderArea.Sections[index - 1];
        }
        else
        {
            index = 0;
            newsec = reportDef.ReportDefinition.PageHeaderArea.Sections[index];
        }
        reportDef.ReportDefinition.PageHeaderArea.Sections.Insert(index, newsec);

        CrystalDecisions.ReportAppServer.ReportDefModel.Section sectionToAddTo = reportDef.ReportDefinition.PageHeaderArea.Sections[index];

        CrystalDecisions.ReportAppServer.ReportDefModel.SectionFormat newSectionFormat = sectionToAddTo.Format;
        newSectionFormat.EnableKeepTogether = false;
        newSectionFormat.EnableSuppress = false;
        newSectionFormat.EnableUnderlaySection = true;
        reportSectionController.SetProperty(sectionToAddTo, CrReportSectionPropertyEnum.crReportSectionPropertyFormat, newSectionFormat);

        reportDef.ReportObjectController.Add(textObject, sectionToAddTo, 0);


此问题是 PageHeader中的新部分是PageHeader 中上一部分的副本,如果我在PageHeader属性中更改新部分,它也会更改 PageHeader上一节,请帮我解决一下这个问题?

1 个答案:

答案 0 :(得分:1)

    int index = reportDef.ReportDefinition.PageHeaderArea.Sections.Count;
    if (index > 0)
    {               
        newsec = reportDef.ReportDefinition.PageHeaderArea.Sections[index - 1].Clone();
    }
    else
    {
        index = 0;
        newsec = reportDef.ReportDefinition.PageHeaderArea.Sections[index].Clone();
    }

这将创建“上一部分”的新副本,对newsec的所有更改都不会影响到prev。部分。作为替代方案,您还可以检查 CopyTo()方法。