Exemplo n.º 1
0
        private static void AddTextObjectTest()
        {
            var rpt = new ReportDocument();

            rpt.Load(@"Reports\SimpleReport.rpt");

            //access the ReportClientDocument in the ReportDocument (EROM bridge)
            var doc = rpt.ReportClientDocument;

            //first determine which area and section to add the text field to - in this case the ReportHeaderArea first's section
            var section = doc.ReportDefController.ReportDefinition.ReportHeaderArea.Sections[0];

            //create the textobject
            var textObject = new CrystalDecisions.ReportAppServer.ReportDefModel.TextObject();

            //create the paragraph elements for the textobject
            var paragraphs           = new CrystalDecisions.ReportAppServer.ReportDefModel.Paragraphs();
            var paragraph            = new CrystalDecisions.ReportAppServer.ReportDefModel.Paragraph();
            var paragraphElements    = new CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphElements();
            var paragraphTextElement = new CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphTextElement();

            //set the text value for the text field
            paragraphTextElement.Text = "This is the added text field in the reportheader";
            paragraphTextElement.Kind = CrystalDecisions.ReportAppServer.ReportDefModel.CrParagraphElementKindEnum.crParagraphElementKindText;

            //fix some wierd defaults and behavior by explicitly setting default font
            var fontColor = new CrystalDecisions.ReportAppServer.ReportDefModel.FontColor();
            var font      = new CrystalDecisions.ReportAppServer.ReportDefModel.Font();

            font.Bold      = true;
            font.Name      = "Arial";
            fontColor.Font = font;
            paragraphTextElement.FontColor = fontColor;

            paragraphElements.Add(paragraphTextElement);
            paragraph.ParagraphElements = paragraphElements;
            paragraphs.Add(paragraph);
            textObject.Paragraphs = paragraphs;

            //now add it to the section
            textObject.Left   = 2000;
            textObject.Top    = 1;
            textObject.Width  = 5000;
            textObject.Height = 226;

            doc.ReportDefController.ReportObjectController.Add(textObject, section);

            rpt.SaveAs(@"Reports\AddTextObjectReport.rpt");

            rpt.ExportToDisk(ExportFormatType.PortableDocFormat, "AddTextObjectReport.pdf");

            rpt.Close();

            rpt.Dispose();

            System.Diagnostics.Process.Start("AddTextObjectReport.pdf");
        }
Exemplo n.º 2
0
        private static void TransformReportTest()
        {
            var rpt = new ReportDocument();

            rpt.Load(@"Reports\SimpleReport.rpt");
            var doc = rpt.ReportClientDocument;

            // var fieldObjects = rpt.ReportDefinition.ReportObjects.OfType<FieldObject>().ToList();
            var fields = doc.ReportDefController.ReportObjectController.GetReportObjectsByKind(CrystalDecisions.ReportAppServer.ReportDefModel.CrReportObjectKindEnum.crReportObjectKindField);

            // var q = doc.ReportDefController.ReportDefinition.DetailArea.Sections.Count;

            var textObject = new CrystalDecisions.ReportAppServer.ReportDefModel.TextObject();

            var sectionStart = doc.ReportDefController.ReportDefinition.DetailArea.Sections[0];

            sectionStart.Format.BackgroundColor = 16;

            var fontColor = new CrystalDecisions.ReportAppServer.ReportDefModel.FontColor();
            var font      = new CrystalDecisions.ReportAppServer.ReportDefModel.Font();

            font.Bold       = true;
            font.Name       = "Arial";
            fontColor.Font  = font;
            fontColor.Color = 0;

            var header1 = new CrystalDecisions.ReportAppServer.ReportDefModel.FieldHeadingObject();

            header1.FieldObjectName = "headerPole";
            header1.FontColor       = fontColor;

            //  doc.ReportDefController.ReportObjectController.Add(textObject, sectionStart, -1);

            foreach (CrystalDecisions.ReportAppServer.ReportDefModel.ISCRReportObject fieldObject in fields)
            {
                // Utworzenie nowej sekcji
                CrystalDecisions.ReportAppServer.ReportDefModel.Section newSection = new CrystalDecisions.ReportAppServer.ReportDefModel.Section();
                newSection.Kind   = CrystalDecisions.ReportAppServer.ReportDefModel.CrAreaSectionKindEnum.crAreaSectionKindDetail;
                newSection.Name   = "Detail" + fieldObject.Name;
                newSection.Height = 300;
                newSection.Width  = 11520;

                //   var area = doc.ReportDefController.ReportDefinition.DetailArea;

                var index = fields.FindIndexOf(fieldObject);

                if (index % 2 == 0)
                {
                    newSection.Format.BackgroundColor = 14803425;
                }

                doc.ReportDefController.ReportSectionController.Add(newSection, doc.ReportDefController.ReportDefinition.DetailArea, -1);

                fieldObject.Format.HorizontalAlignment = CrystalDecisions.ReportAppServer.ReportDefModel.CrAlignmentEnum.crAlignmentLeft;
                fieldObject.Left  = 200;
                fieldObject.Top   = 0;
                fieldObject.Width = 6 * 1440;

                // Przesuwamy pole
                doc.ReportDefController.ReportObjectController.Remove(fieldObject);
                doc.ReportDefController.ReportObjectController.Add(fieldObject, newSection);
            }

            // Utworzenie grupy

            var group      = new CrystalDecisions.ReportAppServer.DataDefModel.Group();
            var tableIndex = doc.DatabaseController.Database.Tables.FindByAlias("Orzeczenia");
            var table      = (CrystalDecisions.ReportAppServer.DataDefModel.Table)doc.DatabaseController.Database.Tables[tableIndex];

            // Pobranie pola z raportu
            var field = (CrystalDecisions.ReportAppServer.DataDefModel.Field)table.DataFields.FindField("{Orzeczenia.OsobaId}",
                                                                                                        CrystalDecisions.ReportAppServer.DataDefModel.CrFieldDisplayNameTypeEnum.crFieldDisplayNameFormula,
                                                                                                        CrystalDecisions.ReportAppServer.DataDefModel.CeLocale.ceLocaleUserDefault);

            // Ustawienie pola do grupowania
            group.ConditionField = field;


            // Dodanie grupy do raportu
            doc.DataDefController.GroupController.Add(-1, group);

            // Dodanie pola z bazy danych
            var field2 = new CrystalDecisions.ReportAppServer.ReportDefModel.FieldObject();

            field2.DataSourceName             = "{Orzeczenia.OsobaId}";
            field2.FieldValueType             = CrystalDecisions.ReportAppServer.DataDefModel.CrFieldValueTypeEnum.crFieldValueTypeInt32sField;
            field2.Left                       = 4 * 1440; //1440 twips per inch
            field2.Width                      = 3 * 1440;
            field2.FontColor                  = new CrystalDecisions.ReportAppServer.ReportDefModel.FontColor();
            field2.FontColor.Font.Name        = "Arial";
            field2.FontColor.Font.Size        = 10;
            field2.Format.HorizontalAlignment = CrystalDecisions.ReportAppServer.ReportDefModel.CrAlignmentEnum.crAlignmentLeft;

            //   var sectionStart = doc.ReportDefController.ReportDefinition.DetailArea.Sections[0];

            var groupSection = doc.ReportDefController.ReportDefinition.GroupHeaderArea[0].Sections[0];

            //Add the object to the report
            doc.ReportDefController.ReportObjectController.Add(field2, groupSection, -1);



            rpt.SaveAs(@"Reports\TransformReport.rpt");

            rpt.ExportToDisk(ExportFormatType.PortableDocFormat, "TransformReport.pdf");

            rpt.Close();

            rpt.Dispose();

            System.Diagnostics.Process.Start("TransformReport.pdf");
        }