一、FastReport 效果展示
(1)fastReport报表中已设置仿宋加粗 如下图1:
图1
(2)导出本地PDF 之后,显示效果。如下图2:
图2
二、DevExpress.xtraReport 效果展示
(1)dev报表中已设置仿宋加粗 如下图3:
图3
(2)导出本地PDF 之后,显示效果。如下图4:
图4
三、 解决方案
通过长时间的研究,发现最佳的方式是,通过在报表中使用图片元素(一定要使用报表工具中中自带图片元素,如果使用winfrom 的pictureBox 存在图片失真的问题) ,代替原来的文本元素。 在报表转PDF 的时候,把图片绘制到图片元素中。
(1)fastReport 代码如下:
using FastReport;
using FastReport.Export.Pdf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 创建报表对象
Report report = new Report();
// 加载报表模板
report.Load(@"C:\Users\wangjunjie\Desktop\test.frx");
// 找到需要加粗显示的文本对象
TextObject textObject = report.FindObject("test") as TextObject;
PictureObject PicObject = report.FindObject("Picture1") as PictureObject;
// 设置字体样式为仿宋加粗
textObject.Font = new Font("仿宋", 20, FontStyle.Regular);
//创建一个新的图片对象
using (Bitmap bmp = new Bitmap(1000, 100))
{
// 使用 Graphics 对象在图片上绘制文字
using (Graphics graphics = Graphics.FromImage(bmp))
{
//设置背景色为白色
graphics.Clear(Color.White);
//设置文字样式
Font font = new Font("仿宋", 50, FontStyle.Bold);
Brush brush = new SolidBrush(Color.Black);
// 绘制文字
graphics.DrawString("白日依山尽,黄河入海流", font, brush, new PointF(0, 0));
// 释放资源
font.Dispose();
brush.Dispose();
}
//保存图片到文件
bmp.Save("text_image.png", ImageFormat.Png);
}
// 把生成的图片添加到元素中
PicObject.Image = Image.FromFile("text_image.png");
// 准备报表
report.Prepare();
PDFExport pdfExport = new PDFExport();
// 导出报表为 PDF
report.Export(pdfExport, "OutputFileName.pdf");
// 释放资源
report.Dispose();
}
}
}
效果图 ,如下图5:
图5
(2)DevExpress.xtraReport代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Data;
using System.Collections.Generic;
using System.Drawing.Imaging;
namespace JHEMR.JHDeadCertificate.Report
{
public partial class Three : ThreeBase
{
public Three()
{
InitializeComponent();
// 创建一个新的图片对象
using (Bitmap bmp = new Bitmap(1000, 100))
{
// 使用 Graphics 对象在图片上绘制文字
using (Graphics graphics = Graphics.FromImage(bmp))
{
// 设置背景色为白色
graphics.Clear(Color.White);
// 设置文字样式
Font font = new Font("仿宋", 53, FontStyle.Bold);
Brush brush = new SolidBrush(Color.Black);
// 绘制文字
graphics.DrawString("白日依山尽,黄河入海流", font, brush, new PointF(0, 0));
// 释放资源
font.Dispose();
brush.Dispose();
}
// 保存图片到文件
bmp.Save(DateTime.Now.ToString("YYYYMMddHHMMSS") + "text_image.png", ImageFormat.Png);
}
this.xRpictureBox1.Image = Image.FromFile(DateTime.Now.ToString("YYYYMMddHHMMSS") + "text_image.png");
}
}
}
效果图 如下 图6
图六