Draw a GraphicsPath in a PDF

前提是你 提交于 2020-01-16 00:25:27

问题


I am trying to create a PDF based on some vector art I have in my C# application. I have two major problems when I try to map the points and types from a GraphicsPath.

  1. Some paths are just plain missing.
  2. When a sub path is an internal boundary I need to indicate that somehow. ie, the circle in the letter d is filled in.

I'm referencing iTextSharp 5.5.2 on NuGet. I'm only using AddString here because I want an easy way to demonstrate creating a complex path in this example. For my need I won't be using a path to place text in the 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);
        }
    }
}

回答1:


I'm posting an answer to myself in case anyone else is in need of a simple function to plot out a GraphicsPath in iTextSharp. I had two problems with my sample code in the question:

  1. as mkl pointed out I was trying to fill too often
  2. I failed to notice that PathPointType.Line is a valid mask in PathPointType.Bezier so the code placing a line back to the origin after a curve.

Updatd Code:

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);
        }
    }
}


来源:https://stackoverflow.com/questions/25796669/draw-a-graphicspath-in-a-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!