【问题标题】:Rendering Oval Shape on a PDF document using Xfinium.Graphics使用 Xfinium.Graphics 在 PDF 文档上渲染椭圆形
【发布时间】:2019-06-20 20:45:58
【问题描述】:

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

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

需要的是文本的 heightwidth 应该确定椭圆形的大小。我也在从远程服务读取 PDF 文件。 我需要有关如何使用 Xfinium.Graphics

实现此目的的帮助

【问题讨论】:

    标签: java c# .net asp.net-core-webapi


    【解决方案1】:

    我可以使用下面的代码在 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;
    
        }
    

    矢量图像看起来像这样:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 2011-09-25
      相关资源
      最近更新 更多