无法以编程方式在水晶报表中设置图形位置

时间:2010-03-30 21:04:34

标签: crystal-reports

在Crystal Reports 2008中,有一个“图形位置”图像属性,可以将其设置为文件路径,因此当报表运行时,它将使用选定的图片文件而不是报表上的图片文件。我尝试通过.NET API设置它,但它只在某些时候工作。

在报告本身中,我将Graphic Location设置为{@LogoPath},然后当我通过.NET API运行报告时,我将{@LogoPath}设置为图片的文件名。我已经将公式放在报告本身上,并且确实设置为正确的文件名,但报告中的图像并不总是更新。它会一直显示一段时间,然后一直不再显示它。

3 个答案:

答案 0 :(得分:0)

查看我的Crystal Reports: Dynamic Images帖子。

答案 1 :(得分:0)

这是我最终使用的,代码在Delpi Prism中。处理的一个尴尬是,如果要替换的图像与报表上的图像大小不同,Crystal不会正确调整大小。另一个问题是我需要手动释放图片对象,否则Crystal有时不会在报告上显示它。

method SetShadingAndLogo(const AReport:ReportDocument);
var
  LogoPath:String;
  PicObj:PictureObject;
  Logo:System.Drawing.Image;
  PicRatio:Double;
  ContWidth, ContHeight:Double;
  ContainerRatio:Double;
  NewDimension:Double;
  PosAdj:Integer;
  Scale:Double;
begin
  for each Section:Section in AReport.ReportDefinition.Sections do
  begin
    for each RptObj:ReportObject in Section.ReportObjects do
    begin
      if RptObj.Name.StartsWith('LOGO', StringComparison.CurrentCultureIgnoreCase) and
        (RptObj.Kind = ReportObjectKind.PictureObject) then
      begin
        //set to company logo
        LogoPath := "C:\logo.jpg";
        PicObj := RptObj as PictureObject;

        if not System.IO.File.Exists(LogoPath) then
          PicObj.ObjectFormat.EnableSuppress := true
        else
        begin        
          Logo := System.Drawing.Image.FromFile(LogoPath);

          //work out the aspect ratios of the image and the container
          PicRatio := Double(Logo.Width) / Double(Logo.Height);

          //convert twips to pixels
          //96 is the default dpi for Windows, but should really check Windows settings
          //instead of hard coding
          ContWidth  := Double(TwipsToPx(PicObj.Width, 96));
          ContHeight := Double(TwipsToPx(PicObj.Height, 96));

          ContainerRatio := ContWidth / ContHeight;

          // adjust the size of the container on the report to maintiain the original
          // image's ratio
          if PicRatio > ContainerRatio then 
          begin
            // reset the vertical position to remain centred on the original location

            // get the new height of the container (in pixels)
            NewDimension := (ContWidth / Logo.Width) * Logo.Height;

            // get the movement (in twips)
            PosAdj := PxToTwips(Integer((ContHeight - NewDimension) / 2), Integer(Logo.VerticalResolution));

            // adjust the position
            PicObj.Top := PicObj.Top + PosAdj;

            // picture is wider so adjust the height accordingly
            // need to scale using the logo's dpi to resize correctly
            Scale := Double(PicObj.Width) / Double(PxToTwips(Logo.Width, Integer(Logo.VerticalResolution)));
            PicObj.Width := Integer(PicObj.Width * Scale);
            PicObj.Height := Integer(PicObj.Height * Scale);
          end
          else 
          begin 
            // picture is taller and needs to be scaled to height
            // reset the horizontal position to remain centred on the original location

            // get the new width of the container (in pixels)
            NewDimension := (ContHeight / Logo.Height) * Logo.Width;

            // get the movement (in twips)
            PosAdj := PxToTwips(Integer((ContWidth - NewDimension) / 2), Integer(Logo.VerticalResolution));

            // adjust the position
            PicObj.Left := PicObj.Left + PosAdj;

            // picture is taller and needs to be scaled to height
            // need to scale using the logo's dpi to resize correctly
            Scale := Double(PicObj.Height) / Double(PxToTwips(Logo.Height, Integer(Logo.VerticalResolution)));
            PicObj.Width := Integer(PicObj.Width * Scale);
            PicObj.Height := Integer(PicObj.Height * Scale);
          end;

          //must free the logo, otherwise Crystal sometimes doesn't display it on report
          Logo.Dispose;

          for each fm:FormulaFieldDefinition in AReport.DataDefinition.FormulaFields do
          begin
            if fm.Name.Equals("LogoPath") then
              fm.Text := """"+LogoPath+"""";
          end;
        end;
      end;
    end;
  end;
end;

method TwipsToPx(const AValue, ADpi:Integer):Integer;
begin
  //convert to twips using specified dpi, 96 is Windows' default dpi
  Result := System.Convert.ToInt32(Double(AValue) * ADpi / 1440);
end;

method PxToTwips(const AValue, ADpi:Integer):Integer;
begin
  //convert to pixels using specified dpi, 96 is Windows' default dpi
  Result := System.Convert.ToInt32(Double(AValue) * 1440 / ADpi);
end;

答案 2 :(得分:0)

在“图形位置”公式中,您可能希望执行以下操作: -

{?imageUrl}

您可以通过以下操作动态地在CS文件中设置此参数: -

 ParameterFields paramFields = new ParameterFields();
 ParameterField paramField = new ParameterField();
 ParameterDiscreteValue parameterValue = new ParameterDiscreteValue();

 paramField.Name = "imageUrl";
 parameterValue.Value = "**URL FOR IMAGE**";
 paramField.CurrentValues.Add(parameterValue1);

 paramFields.Add(paramField);

在报告中,创建“imageUrl”参数字段。通过以下操作: -

1)转到报告并打开左侧的Field Explorer。如果它不在那里你可以通过去 Crystal Report>>归档资源管理器。

2)右键单击参数字段,然后单击“新建”

3)将参数名称设为“imageUrl”。在“值选项”中,将“仅显示提示”和“可选提示”设置为false。

这应该有效。 如果有帮助,请告诉我。