通过Java SDK将LineObject添加到Crystal Report

时间:2022-01-03 15:28:13

I have successfully connected a Crystal Report with Java in my desktop application. Now I want to programmatically add a line to the report.

我在桌面应用程序中成功连接了Crystal Report和Java。现在我想以编程方式在报表中添加一行。

I have tried this with the following code.

我用以下代码尝试了这个。

try {
    ReportDefController rdc = reportClientDoc.getReportDefController();
} catch (ReportSDKException ex) {
    Logger.getLogger(PurchaseReport.class.getName()).log(Level.SEVERE, null, ex);
}

Tables tables = null;
try {
    tables = reportClientDoc.getDatabase().getTables();
} catch (ReportSDKException ex) {
    Logger.getLogger(PurchaseReport.class.getName()).log(Level.SEVERE, null, ex);
}

LineObject lineObject = new LineObject();
lineObject.clone(false);
lineObject.setSectionCode(0);
lineObject.setLineThickness(10);
lineObject.setLeft(50);
lineObject.setWidth(50);
lineObject.setHeight(10);
lineObject.setRight(20);
lineObject.setTop(10);
lineObject.setLineColor(Color.BLUE);

ReportObjectController roc = null;
try {
    roc = reportClientDoc.getReportDefController().getReportObjectController();
} catch (ReportSDKException ex) {
    Logger.getLogger(PurchaseReport.class.getName()).log(Level.SEVERE, null, ex);
}

ReportDefController reportDefController = null;
try {
    reportDefController = reportClientDoc.getReportDefController();
    ISection section = reportDefController.getReportDefinition().getDetailArea().getSections().getSection(0);
    lineObject.setSectionName(section.getName());
    //roc.add(fieldObject, section, 0);
    if(roc.canAddReportObject(lineObject, section))
    {
        roc.add(lineObject, section, -1);
    }
} catch (ReportSDKException ex) {
    Logger.getLogger(PurchaseReport.class.getName()).log(Level.SEVERE, null, ex);
}

This throws an error at roc.add(lineObject, section, -1)

这会在roc.add(lineObject,section,-1)处抛出错误

How can I resolve this error and properly add the line?

如何解决此错误并正确添加该行?

1 个解决方案

#1


0  

Lines and boxes are added slightly differently than regular report objects with the SDK, because they can span sections/areas. You need to specify 'bottom' (or 'right') and 'endSection' properties, where 'bottom' is the offset from the top of the end section. Height and width properties have no effect on these drawing objects (use 'right' combined with 'left' to specify width)

使用SDK添加的行和框与常规报表对象的添加方式略有不同,因为它们可以跨越节/区域。您需要指定'bottom'(或'right')和'endSection'属性,其中'bottom'是距离结束部分顶部的偏移量。高度和宽度属性对这些绘图对象没有影响(使用'right'结合'left'指定宽度)

On to your problem: First, you'll want to remove the calls to setHeight & setWidth.

关于你的问题:首先,你要删除对setHeight和setWidth的调用。

Then, try something like the following code inside your last try block:

然后,在最后一个try块中尝试类似下面的代码:

reportDefController = reportClientDoc.getReportDefController();
ISection section = reportDefController.getReportDefinition().getDetailArea().getSections().getSection(0);
lineObject.setSectionName(section.getName());

// BEGIN ADDED CODE

// specify section you want line to end in
lineObject.setEndSectionName(section.getName()); 

// how far down in the end section you want your line object - if you want a horizontal line, use setRight instead
lineObject.setBottom(100);                       

// specify some style so it appears (I think default is noLine)
lineObject.setLineStyle(LineStyle.singleLine);


// END ADDED CODE

if(roc.canAddReportObject(lineObject, section)) {
    roc.add(lineObject, section, -1);
}

This will add a small vertical line that starts at 10 (based on the setTop call in your code) and ends at 100 in the first details section. You'll probably want to tweak the numbers and positioning to meet your needs.

这将添加一个从10开始的小垂直线(基于代码中的setTop调用),并在第一个详细信息部分中以100结束。您可能希望调整数字和定位以满足您的需求。

#1


0  

Lines and boxes are added slightly differently than regular report objects with the SDK, because they can span sections/areas. You need to specify 'bottom' (or 'right') and 'endSection' properties, where 'bottom' is the offset from the top of the end section. Height and width properties have no effect on these drawing objects (use 'right' combined with 'left' to specify width)

使用SDK添加的行和框与常规报表对象的添加方式略有不同,因为它们可以跨越节/区域。您需要指定'bottom'(或'right')和'endSection'属性,其中'bottom'是距离结束部分顶部的偏移量。高度和宽度属性对这些绘图对象没有影响(使用'right'结合'left'指定宽度)

On to your problem: First, you'll want to remove the calls to setHeight & setWidth.

关于你的问题:首先,你要删除对setHeight和setWidth的调用。

Then, try something like the following code inside your last try block:

然后,在最后一个try块中尝试类似下面的代码:

reportDefController = reportClientDoc.getReportDefController();
ISection section = reportDefController.getReportDefinition().getDetailArea().getSections().getSection(0);
lineObject.setSectionName(section.getName());

// BEGIN ADDED CODE

// specify section you want line to end in
lineObject.setEndSectionName(section.getName()); 

// how far down in the end section you want your line object - if you want a horizontal line, use setRight instead
lineObject.setBottom(100);                       

// specify some style so it appears (I think default is noLine)
lineObject.setLineStyle(LineStyle.singleLine);


// END ADDED CODE

if(roc.canAddReportObject(lineObject, section)) {
    roc.add(lineObject, section, -1);
}

This will add a small vertical line that starts at 10 (based on the setTop call in your code) and ends at 100 in the first details section. You'll probably want to tweak the numbers and positioning to meet your needs.

这将添加一个从10开始的小垂直线(基于代码中的setTop调用),并在第一个详细信息部分中以100结束。您可能希望调整数字和定位以满足您的需求。