使用Xfinium.Graphics在PDF文档上渲染椭圆形

时间:2019-06-18 05:32:14

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

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

import numpy as np

state = np.random.RandomState(1)
X = state.rand(40,100)
A = np.cov(X[:20])
B = np.cov(X)[:20,:20]
print(np.array_equal(A, B))
diff = A - B
print(np.max(diff))

Renderd on PDF like this 要求文本的高度宽度应该确定椭圆形的大小。我也在从远程服务中读取PDF文件。 我需要有关如何使用 Xfinium.Graphics

来实现此目的的帮助

1 个答案:

答案 0 :(得分:0)

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

    }

矢量图像如下所示:Oval Shape