如何使用PDFsharp

时间:2017-07-07 02:36:44

标签: c# pdf pdfsharp

我使用PDFsharp制作PDF文件,并成功将图像放在我的页面上。

        byte[] imgBytes = interview.Application.CandidateImage.ImageBinary.ToArray();
        Stream stream = new MemoryStream(imgBytes);
        MemoryStream strm = new MemoryStream();
        System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
        img.Save(strm, System.Drawing.Imaging.ImageFormat.Png);

        XImage xfoto = XImage.FromGdiPlusImage(img);

        gfx.DrawImage(xfoto, 30, 130, 300, 300);

这将获取二进制图像数据,DrawImage将绘制从流中检索到的此图像。

问题是,我想让图像变得圆润,就像我在HTML上使用img-circle类一样。 PDFsharp有这个功能吗?如果没有,我该怎么做?

编辑:

    void DrawClipPath(XGraphics gfx, PdfPage page)
    {


        XGraphicsPath path = new XGraphicsPath();
        path.AddEllipse((page.Width / 2) - 150, (page.Height / 2) - 120, 300, 300);
        gfx.Save();
        gfx.IntersectClip(path);

        // Draw a beam of dotted lines
        XPen pen = XPens.DarkRed.Clone();
        pen.DashStyle = XDashStyle.Dot;
        for (double r = 0; r <= 90; r += 0.5)
            gfx.DrawLine(pen, 0, 0, 250 * Math.Cos(r / 90 * Math.PI), 250 * Math.Sin(r / 90 * Math.PI));

        gfx.Restore();


    }

2 个答案:

答案 0 :(得分:1)

您可以使用具有透明度的图像,以便只能看到中心的圆形部分。

或者使用hack:首先绘制图像,然后在图像上绘制一个白色的面具,只留下中心的圆形部分。

答案 1 :(得分:1)

您可以将圆圈设置为剪辑路径,然后绘制图像。您必须在设置剪辑路径之前保存图形状态(XGraphics.Save),然后在绘制完所有需要剪切的对象后将其恢复(XGraphics.Restore)。

编辑:我不熟悉PDFSharp API,但代码看起来像这样:

gfx.Save();
XGraphicsPath clipPath = new XGraphicsPath();
clipPath.AddEllipse(30, 130, 300, 300);
gfx.IntersectClip(clipPath);
gfx.DrawImage(xfoto, 30, 130, 300, 300);
gfx.Restore();