利用FastReport传递图片参数,在报表上展示签名信息

利用,fastreport,传递,图片,参数,报表,展示,签名,信息 · 浏览次数 : 1262

小编点评

## Summary of the Report Script's Image Processing This document explains the image processing implementation within the FastReport report generation process for signing display. **Design Process:** * The report layout includes a signature display location. * Instead of text, the signature is represented through an image. * To achieve this, the script retrieves the image based on a parameter name ("signName"). * The obtained image is then assigned to the relevant control. **Image Handling:** * The script uses a method called `GetImage` to retrieve the image based on the signName. * The method checks for the image existence and converts it to a Base64 string for transmission. **Data Passing:** * The script generates a dictionary with parameters and data values. * These parameters are then set as report parameters for further use. * The generated PDF report supports both PDF and image output formats. **Output Images:** * Depending on the output format, the script generates the report and exports the following: * PDF report with multiple pages. * Images embedded within the PDF report. **Key Points:** * The script uses Base64 encoding to ensure proper image transmission. * Images are placed in a dedicated directory relative to the report. * The script sets appropriate parameters and data values for signature display. * The output format can be either PDF or image based on the report settings. **Overall, this approach allows for flexible and customizable signature display in the FastReport report.**

正文

在一个项目中,客户要求对报表中的签名进行仿手写的签名处理,因此我们原先只是显示相关人员的姓名的地方,需要采用手写方式签名,我们的报表是利用FastReport处理的,在利用楷体处理的时候,开发展示倒是正常效果,不过实际上在服务器运行的时候,出来的确实正规的宋体格式,相应的字体都已经安装,不过还是没有生效。因此采用变通的方式,在对应签名的地方采用图片的格式显示,实际效果达到要求。本篇随笔介绍这个过程,利用FastReport传递图片参数,在报表上展示签名信息的处理。

1、报表上的设计处理

例如我们要在报表落款的附近记录相关人员的名字,因此采用签名的显示方式会比较合理。

 

因此设计相关的报表,本来想是采用文本的方式,变化字体的方式来快捷实现的

 

 不过在报表是在服务器上生成图片的方式,导致正常出来的图片,却没有变化字体,导致出来的是正常的宋体格式。 

  

 因此考虑使用图片的格式方式来处理。在其中放置Picture控件,如下所示。

 

调整好Picture控件的高度和宽度,让它在设计的空白上合适的展示即可。 

为了实现图片格式的显示,我们需要在报表的图片控件的BeforePrint事件中解析数据(来自传递参数),数据格式为Base64字符串(从Byte数据转换),如下代码所示。

namespace FastReport
{
  public class ReportScript
  {

    private void shopDoctorImg_BeforePrint(object sender, EventArgs e)
    {
      var img = GetImage("ShopDoctor");
      if(img != null){
        shopDoctorImg.Image=img;  
      }
    }   
    private void tiaopeiImg_BeforePrint(object sender, EventArgs e)
    {    
      var img = GetImage("Tiaopei");
      if(img != null){
        tiaopeiImg.Image=img;  
      }
    }       

    private void CheckDoctorImg_BeforePrint(object sender, EventArgs e)
    {
      var img = GetImage("CheckDoctor");
      if(img != null){
        CheckDoctorImg.Image=img;  
      }
    }

    private void CheckPharmacistImg_BeforePrint(object sender, EventArgs e)
    {       
      var img = GetImage("CheckPharmacist");
      if(img != null){
        CheckPharmacistImg.Image=img;  
      }
    }
    private Image GetImage(string signName)
    {         
      Image img = null;
      string imgStr = (string)Report.GetParameterValue(signName);
      if(!string.IsNullOrEmpty(imgStr))
      {
        byte[] imgData= Convert.FromBase64String(imgStr);
        using(MemoryStream ms = new MemoryStream(imgData))
        {
          img = System.Drawing.Image.FromStream(ms);            
        }
      }
      return img;
    }
        
  }
}

其中主要注意的是,我们传递的图片数据需要采用Base64String的格式才能正常传递和展示。 

2、报表传递图片数据

完成了报表的设计处理,我们剩下的就是在实际的报表中传递对应的参数数据了。

我们把签名图片,放在相对的目录上,如下所示。

 

 然后编写一个公用的读取图片为Base64String的函数处理,如下所示。

        //通过姓名获取签名图片的Base64
        private string GetSignImage(string signName)
        {
            var result = "";
            string imagePath = Path.Combine(baseDir, $"Report/signs/{signName}.png");
            if (File.Exists(imagePath))
            {
                var stream = FileUtil.FileToStream(imagePath);
                var image = FileUtil.StreamToBytes(stream);
                if (image != null)
                {
                    result = Convert.ToBase64String(image);
                }
            }
            return result;
        }

接着就是根据对应的报表进行加载,并设置相关的参数进行传递给报表即可,如下测试代码所示。

    //生成PDF报表文档到具体文件
    Report report = new Report();
    report.Load(reportFile);

    //定义参数和数据格式
    var dict = new Dictionary<string, object>();
    #region 测试数据源
    dict.Add("Name", "张三");
    dict.Add("Gender", "");
    dict.Add("Age", 32);
    dict.Add("Telephone", "18620292076");
    dict.Add("CreateTime", "2019-10-13 22:30:15");
    dict.Add("CheckDoctor", GetSignImage("张医生"));//"张医生"
    dict.Add("CheckPharmacist", GetSignImage("张医生")); //"李药师"
    dict.Add("SendUser", "王小姐");
    dict.Add("QrCode", "http://www.iqidi.com");
    dict.Add("BarCode", "1234567890");

    //图片文件
    dict.Add("ShopDoctor", GetSignImage("张医生"));
    dict.Add("Tiaopei", GetSignImage("张医生"));
    dict.Add("Fayao", GetSignImage("王小姐"));    
    #endregion
    
    report.RegisterData(dt, "Detail");
    foreach (string key in dict.Keys)
    {
        report.SetParameterValue(key, dict[key]);
    }

    //运行报表
    report.Prepare();    

由于我们的报表,最终是生成PDF或者图片的方式,方便客户进行在线查询的,因此可以选择PDF或者图片的格式生成。

    //运行报表
    report.Prepare();    
    
    //导出PDF报表
    //PDFExport export = new PDFExport();

    //多个图片导出
    int count = 1;
    string firstFileName = exportImgPath.Replace(".png", "");
    foreach (PageBase item in report.Pages)
    {
        string fileName = string.Format("{0}_{1}.png", firstFileName,  count);
        exportImgPath = fileName;
        //Resolution= 300可以提高分辨率
        report.Export(new ImageExport() { PageRange = PageRange.Current, CurPage = count, Resolution= 300 }, fileName);
        count++;
    }

最后生成的图片格式如下所示,顺利吧签名的图片贴在对应的单元格中即可。

 

与利用FastReport传递图片参数,在报表上展示签名信息相似的内容:

利用FastReport传递图片参数,在报表上展示签名信息

在一个项目中,客户要求对报表中的签名进行仿手写的签名处理,因此我们原先只是显示相关人员的姓名的地方,需要采用手写方式签名,我们的报表是利用FastReport处理的,在利用楷体处理的时候,开发展示倒是正常效果,不过实际上在服务器运行的时候,出来的确实正规的宋体格式,相应的字体都已经安装,不过还是没有生效。因此采用变通的方式,在对应签名的地方采用图片的格式显示,实际效果达到要求。本篇随笔介绍这个过程

利用Wireshark抓包分析DNS域名解析过程

一、DNS协议概述 DNS协议也可以称为DNS服务,全称是Domain Name System,即域名系统,和HTTP协议一样,也是一个位于应用层的协议(服务),它是基于运输层的UDP协议的。从DNS的名字我们就可以知道,它提供域名映射到IP地址的服务。 二、实验目的 掌握DNS域名解析过程 熟悉D

利用大型语言模型轻松打造浪漫时刻

在这篇文章中,我们介绍了如何利用大型语言模型为情人节营造难忘的氛围。通过上传图片并进行风格转化,我们可以为对方呈现一幅独特的作品,增添浪漫的色彩。同时,借助搜索功能,我们能够轻松获取与情人节相关的信息,为策划活动提供更多灵感和建议。

利用英特尔 Gaudi 2 和至强 CPU 构建经济高效的企业级 RAG 应用

检索增强生成 (Retrieval Augmented Generation,RAG) 可将存储在外部数据库中的新鲜领域知识纳入大语言模型以增强其文本生成能力。其提供了一种将公司数据与训练期间语言模型学到的知识分开的方式,有助于我们在性能、准确性及安全隐私之间进行有效折衷。 通过本文,你将了解到英特

利用深度循环神经网络对心电图降噪

具体的软硬件实现点击 http://mcu-ai.com/ MCU-AI技术网页_MCU-AI 我们提出了一种利用由长短期记忆 (LSTM) 单元构建的深度循环神经网络来降 噪心电图信号 (ECG) 的新方法。该网络使 用动态模型 ECG 生成的合成数据进行预训 练,并使用来自 Physionet

利用pearcmd实现裸文件包含

title: 利用pearcmd实现裸文件包含 tags: [web,文件包含] categories: [CTF,web] 利用pearcmd实现裸文件包含 在 ctf 中,常常有这样一类题: 题目很简单,一般围绕一个 include 函数展开。 例: ctfshow 元旦水友赛 easy_inc

利用pip/conda安装库时,出现requires XXX, which is not installed/incompatible

博客地址:https://www.cnblogs.com/zylyehuo/ 出现以下提示警告时 step1 step2 step3 总结 利用pip/conda安装库时,出现requires XXX, which is not installed/incompatible 依次执行安装所缺的库即可

利用队列的内置模块(deque)模拟 Linux 下的 tail 命令(输出文件中最后几行的内容)

博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- from collections import deque def tail(n): # n:指定输出文件中最后几行 with open('test.txt', 'r') a

利用Git+GitHub进行团队协作开发

自己之前写过两篇关于Git和GItHub使用的文章,分别是 浅谈使用git 进行版本控制博客链接:https://www.cnblogs.com/wj-1314/p/7992543.html 使用GitHub的点点滴滴的博客链接:https://www.cnblogs.com/wj-1314/p/9

利用SpringBoot项目做一个Mock挡板;基于事件发布动态自定义URL和响应报文

# 导入SpringbootWEb依赖 ```xml org.springframework.boot spring-boot-starter-web ${spring-boot-start-version} org.springframework.boot spring-boot-starter-