首页 > 解决方案 > 使用 Xfinium.Graphics 在 PDF 文档上渲染椭圆形

问题描述

我目前正在传递一个json 对象,并且希望能够在 PDF 文件中的json 对象的某些值周围绘制一个椭圆形,例如

 {"Applicant": 
   {
     "Insurance1":"Gold",
     "Insurance2":"Platinum"
   }
 }

像这样在 PDF 上渲染 需要的是文本的高度宽度应确定椭圆形的大小。我也在从远程服务读取 PDF 文件。我需要有关如何使用Xfinium.Graphics实现此目的的帮助

标签: javac#.netasp.net-core-webapi

解决方案


我能够使用下面的代码在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;

    }

矢量图像看起来像这样:椭圆形


推荐阅读