1

我正在尝试根据我在 C# 应用程序中的一些矢量图创建 PDF。当我尝试从 GraphicsPath 映射点和类型时,我遇到了两个主要问题。

  1. 有些路径完全丢失了。
  2. 当子路径是内部边界时,我需要以某种方式指出这一点。即,字母d中的圆圈被填满。

我在 NuGet 上引用 iTextSharp 5.5.2。我在这里只使用 AddString 是因为我想要一种简单的方法来演示在此示例中创建复杂路径。出于我的需要,我不会使用在 PDF 中放置文本的路径。

using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PdfGen
{
    class StackQuestion
    {
        public static void Main()
        {
            string filename = @"d:\itext.pdf";
            using (var doc = new Document())
            using (var fs = new FileStream(filename, FileMode.Create))
            {
                var writer = PdfWriter.GetInstance(doc, fs);
                doc.Open();
                writer.DirectContent.SetColorFill(BaseColor.BLACK);
                var path = new GraphicsPath(FillMode.Winding);
                path.AddString("Hello World", FontFamily.GenericSerif, 
                    (int)FontStyle.Regular, 25f, RectangleF.Empty, 
                    StringFormat.GenericDefault);
                AddPath(path, writer.DirectContent);
                doc.Close();
            }
            System.Diagnostics.Process.Start(filename);
        }

        static void AddPath(GraphicsPath path, PdfContentByte to)
        {
            var view = to.PdfDocument.PageSize;
            path.FillMode = System.Drawing.Drawing2D.FillMode.Winding;
            var d = path.PathData;
            for (int i = 0; i < d.Points.Length && i < d.Types.Length; i++)
            {
                var t = (PathPointType)d.Types[i];
                var p = Fix(d.Points[i], view);
                if (Match(t, PathPointType.Bezier))
                {
                    var p2 = Fix(d.Points[++i], view);
                    if (d.Types.Length > i + 1 && 
                        Match((PathPointType)d.Types[i + 1], 
                            PathPointType.Bezier3))
                    {
                        var p3 = Fix(d.Points[++i], view);
                        to.CurveTo(p.X, p.Y, p2.X, p2.Y, p3.X, p3.Y);
                    }
                    else
                    {
                        to.CurveTo(p.X, p.Y, p2.X, p2.Y);
                    }
                }
                if (Match(t, PathPointType.Line))
                {
                    to.LineTo(p.X, p.Y);
                }
                if (Match(t, PathPointType.CloseSubpath))
                {
                    to.ClosePath();
                    to.EoFill();
                }
                if (t == PathPointType.Start)
                {
                    to.NewPath();
                    to.MoveTo(p.X, p.Y);
                }
            }
        }

        static bool Match(PathPointType type, PathPointType match)
        {
            return (type & match) == match;
        }

        static System.Drawing.PointF Fix(System.Drawing.PointF pt, 
            iTextSharp.text.Rectangle view)
        {
            return new System.Drawing.PointF(pt.X, view.Height - pt.Y);
        }
    }
}
4

1 回答 1

1

我正在向自己发布一个答案,以防其他人需要一个简单的函数来在 iTextSharp 中绘制 GraphicsPath。我在问题中的示例代码有两个问题:

  1. 正如 mkl 指出的那样,我试图经常填充
  2. 我没有注意到 PathPointType.Line 是 PathPointType.Bezier 中的一个有效掩码,因此代码在曲线后将一条线放回原点。

更新代码:

using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

namespace PdfGen
{
    class StackQuestion
    {
        public static void Main()
        {
            string filename = @"d:\itext.pdf";
            using (var doc = new Document())
            using (var fs = new FileStream(filename, FileMode.Create))
            {
                var writer = PdfWriter.GetInstance(doc, fs);
                doc.Open();
                writer.DirectContent.SetColorFill(BaseColor.BLACK);
                var path = new GraphicsPath(FillMode.Winding);
                path.AddString("Hello World", FontFamily.GenericSansSerif,
                    (int)FontStyle.Regular, 90f, PointF.Empty,
                    StringFormat.GenericDefault);
                AddPath(path, writer.DirectContent);
                writer.DirectContent.EoFill();
                doc.Close();
            }
            System.Diagnostics.Process.Start(filename);
        }

        static void AddPath(GraphicsPath path, PdfContentByte to)
        {
            var view = to.PdfDocument.PageSize;
            var d = path.PathData;
            to.NewPath();
            PointF? start = null;
            for (int i = 0; i < d.Points.Length && i < d.Types.Length; i++)
            {
                var t = (PathPointType)d.Types[i];
                var p = Fix(d.Points[i], view);
                if (Match(t, PathPointType.Bezier))
                {
                    var p2 = Fix(d.Points[++i], view);
                    if (d.Types.Length > i + 1 &&
                        Match((PathPointType)d.Types[i + 1],
                            PathPointType.Bezier))
                    {
                        var p3 = Fix(d.Points[++i], view);
                        to.CurveTo(p.X, p.Y, p2.X, p2.Y, p3.X, p3.Y);
                    }
                    else
                    {
                        to.CurveTo(p.X, p.Y, p2.X, p2.Y);
                    }
                }
                else if (Match(t, PathPointType.Line))
                {
                    to.LineTo(p.X, p.Y);
                }
                if (Match(t, PathPointType.CloseSubpath))
                {
                    if (start != null)
                        to.LineTo(start.Value.X, start.Value.Y);
                    start = null;
                    to.ClosePath();
                }
                if (t == PathPointType.Start)
                {
                    if (start != null)
                        to.LineTo(start.Value.X, start.Value.Y);
                    start = p;
                    to.MoveTo(p.X, p.Y);
                }
            }
        }

        static bool Match(PathPointType type, PathPointType match)
        {
            return (type & match) == match;
        }

        static System.Drawing.PointF Fix(System.Drawing.PointF pt,
            iTextSharp.text.Rectangle view)
        {
            return new System.Drawing.PointF(pt.X, view.Height - pt.Y);
        }
    }
}
于 2014-09-12T14:26:18.690 回答