java - 使用 Xfinium.Graphics 在 PDF 文档上渲染椭圆形
问题描述
我目前正在传递一个json 对象,并且希望能够在 PDF 文件中的json 对象的某些值周围绘制一个椭圆形,例如
{"Applicant":
{
"Insurance1":"Gold",
"Insurance2":"Platinum"
}
}
需要的是文本的高度和宽度应确定椭圆形的大小。我也在从远程服务读取 PDF 文件。我需要有关如何使用Xfinium.Graphics实现此目的的帮助
解决方案
我能够使用下面的代码在pdf 文档(使用Xfinium.Pdf )上绘制一个椭圆形
PdfFixedDocument document = new PdfFixedDocument();
PdfStandardFont font = new PdfStandardFont();
PdfPage page = document.Pages.Add();
double checkLeft = 50;
double checkTop = 50;
double checkWidth = 100;
double checkHeight = 20;
PdfStandardFont font = new PdfStandardFont();
PdfPage page = document.Pages.Add();
BuildCheckBox1("check1", "1", "Checked 1", "Unchecked 1", font, page, new PdfVisualRectangle(checkLeft, checkTop, checkWidth, checkHeight));
checkTop += 25;
BuildCheckBox1("check2", "2", "Checked 2", "Unchecked 2", font, page, new PdfVisualRectangle(checkLeft, checkTop, checkWidth, checkHeight));
checkTop += 25;
BuildCheckBox1("check3", "2", "Checked 3", "Unchecked 3", font, page, new PdfVisualRectangle(checkLeft, checkTop, checkWidth, checkHeight));
checkTop += 25;
PdfStringAppearanceOptions sao = new PdfStringAppearanceOptions();
sao.Brush = new PdfBrush(PdfRgbColor.Black);
sao.Font = font;
PdfStringLayoutOptions slo = new PdfStringLayoutOptions();
slo.X = checkLeft + checkWidth / 2;
slo.Y = checkTop + checkHeight / 2;
slo.HorizontalAlign = PdfStringHorizontalAlign.Center;
slo.VerticalAlign = PdfStringVerticalAlign.Middle;
page.Graphics.DrawString("Option", sao, slo);
BuildCheckBox2("check4", "4", page, new PdfVisualRectangle(checkLeft, checkTop, checkWidth, checkHeight));
document.Save("Sample_OvalCheckBox.pdf");
private void BuildCheckBox1(string name, string exportValue, string checkedText, string uncheckedText, PdfFont font, PdfPage page, PdfVisualRectangle location)
{
PdfCheckBoxField checkBoxField = new PdfCheckBoxField(name);
(checkBoxField.Widgets[0] as PdfCheckBoxWidget).ExportValue = exportValue;
page.Fields.Add(checkBoxField);
checkBoxField.Widgets[0].VisualRectangle = location;
(checkBoxField.Widgets[0] as PdfCheckWidget).CheckStyle = PdfCheckStyle.Check;
double lineWidth = 2;
PdfPen redPen = new PdfPen(PdfRgbColor.Red, lineWidth);
PdfPen grayPen = new PdfPen(PdfRgbColor.LightGray, lineWidth / 2);
grayPen.DashPattern = new double[] { 1, 1 };
PdfBrush pinkBrush = new PdfBrush(PdfRgbColor.LightPink);
PdfStringAppearanceOptions sao = new PdfStringAppearanceOptions();
sao.Brush = new PdfBrush(PdfRgbColor.Black);
sao.Font = font;
PdfStringLayoutOptions slo = new PdfStringLayoutOptions();
slo.X = location.Width / 2;
slo.Y = location.Height / 2;
slo.HorizontalAlign = PdfStringHorizontalAlign.Center;
slo.VerticalAlign = PdfStringVerticalAlign.Middle;
PdfAnnotationAppearance checkedAppearance = new PdfAnnotationAppearance(location.Width, location.Height);
checkedAppearance.Graphics.DrawRoundRectangle(redPen, pinkBrush,
lineWidth / 2, lineWidth / 2, location.Width - lineWidth, location.Height - lineWidth,
location.Height - lineWidth, location.Height - lineWidth);
checkedAppearance.Graphics.DrawString(checkedText, sao, slo);
(checkBoxField.Widgets[0] as PdfCheckWidget).CheckedStateNormalAppearance = checkedAppearance;
PdfAnnotationAppearance uncheckedAppearance = new PdfAnnotationAppearance(location.Width, location.Height);
uncheckedAppearance.Graphics.DrawRoundRectangle(grayPen, pinkBrush,
lineWidth / 4, lineWidth / 4, location.Width - lineWidth / 2, location.Height - lineWidth / 2,
location.Height - lineWidth / 2, location.Height - lineWidth / 2);
uncheckedAppearance.Graphics.DrawString(uncheckedText, sao, slo);
(checkBoxField.Widgets[0] as PdfCheckWidget).UncheckedStateNormalAppearance = uncheckedAppearance;
}
private void BuildCheckBox2(string name, string exportValue, PdfPage page, PdfVisualRectangle location)
{
PdfCheckBoxField checkBoxField = new PdfCheckBoxField(name);
(checkBoxField.Widgets[0] as PdfCheckBoxWidget).ExportValue = exportValue;
page.Fields.Add(checkBoxField);
checkBoxField.Widgets[0].VisualRectangle = location;
(checkBoxField.Widgets[0] as PdfCheckWidget).CheckStyle = PdfCheckStyle.Check;
double lineWidth = 2;
PdfPen redPen = new PdfPen(PdfRgbColor.Red, lineWidth);
PdfPen grayPen = new PdfPen(PdfRgbColor.LightGray, lineWidth / 2);
grayPen.DashPattern = new double[] { 1, 1 };
PdfAnnotationAppearance checkedAppearance = new PdfAnnotationAppearance(location.Width, location.Height);
checkedAppearance.Graphics.DrawRoundRectangle(redPen,
lineWidth / 2, lineWidth / 2, location.Width - lineWidth, location.Height - lineWidth,
location.Height - lineWidth, location.Height - lineWidth);
(checkBoxField.Widgets[0] as PdfCheckWidget).CheckedStateNormalAppearance = checkedAppearance;
PdfAnnotationAppearance uncheckedAppearance = new PdfAnnotationAppearance(location.Width, location.Height);
uncheckedAppearance.Graphics.DrawRoundRectangle(grayPen,
lineWidth / 4, lineWidth / 4, location.Width - lineWidth / 2, location.Height - lineWidth / 2,
location.Height - lineWidth / 2, location.Height - lineWidth / 2);
(checkBoxField.Widgets[0] as PdfCheckWidget).UncheckedStateNormalAppearance = uncheckedAppearance;
}
推荐阅读
- c# - App.config 将嵌套组添加到现有节点
- html - 如何使用引导程序 3 实现相对内容框?
- assembly - 尝试加载内核时重启循环
- android - 如何在片段中使用来自接口的数据?
- dart - Flutter ListView.Builder 焦点到最后一个图块
- css - bootstrap.css 主题在 ASP.NET Core MVC 中不起作用的原因是什么?
- php - 网络服务器阻止的 PHP file_get 内容
- android - 当recyclerview项目有另一个recyclerview时如何使用分页
- python - 我想在 Python 中为 CNN 自动创建权重
- ruby-on-rails - 使用 Devise 时发生 UrlGenerationError