您的图像显示您想要编辑所有路径中的线帽和线连接以使其成为圆形。
很遗憾,您没有分享具有代表性的示例文件,所以我不得不自己构建一个混合了不同的 cap 和 join 样式以及提醒您的路径形式的文件:
我建议您的任务使用来自this answer 的通用PdfContentStreamEditor,因为它可以完成所有繁重的工作,我们可以专注于手头的任务。
因此,我们的流编辑器实现必须做什么?它必须将 cap 和 join 样式设置为“round”并防止这些设置被覆盖。查看 PDF 规范,我们看到 cap 和 join 样式是当前图形状态的参数,可以分别使用 J 和 j 指令直接设置,也可以通过 LC 和 LJ 条目。
因此,我们可以简单地通过首先初始化 cap 和 join 样式来实现我们的流编辑器,然后删除所有 J 和 j 指令并重新初始化 cap 和 join每个图形状态 gs 指令之后的样式。
class PathMakeCapAndJoinRound : PdfContentStreamEditor
{
protected override void Write(PdfContentStreamProcessor processor, PdfLiteral operatorLit, List<PdfObject> operands)
{
if (start)
{
initializeCapAndJoin(processor);
start = false;
}
if (CAP_AND_JOIN_OPERATORS.Contains(operatorLit.ToString()))
{
return;
}
base.Write(processor, operatorLit, operands);
if (GSTATE_OPERATOR == operatorLit.ToString())
{
initializeCapAndJoin(processor);
}
}
void initializeCapAndJoin(PdfContentStreamProcessor processor)
{
PdfLiteral operatorLit = new PdfLiteral("J");
List<PdfObject> operands = new List<PdfObject> { new PdfNumber(PdfContentByte.LINE_CAP_ROUND), operatorLit };
base.Write(processor, operatorLit, operands);
operatorLit = new PdfLiteral("j");
operands = new List<PdfObject> { new PdfNumber(PdfContentByte.LINE_JOIN_ROUND), operatorLit };
base.Write(processor, operatorLit, operands);
}
List<string> CAP_AND_JOIN_OPERATORS = new List<string> { "j", "J" };
string GSTATE_OPERATOR = "gs";
bool start = true;
}
像这样将它应用到上面的 PDF 中
using (PdfReader pdfReader = new PdfReader(testDocument))
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(@"Paths-Rounded.pdf", FileMode.Create, FileAccess.Write), (char)0, true))
{
pdfStamper.RotateContents = false;
PdfContentStreamEditor editor = new PathMakeCapAndJoinRound();
for (int i = 1; i <= pdfReader.NumberOfPages; i++)
{
editor.EditPage(pdfStamper, i);
}
}
我们得到结果:
请注意,引用答案的限制仍然存在。特别是这个编辑器只编辑页面内容流。要获得完整的解决方案,您还必须编辑所有表单 XObject 和 Pattern 流,并处理注释。
为了允许复制,这是我创建测试文档的方式:
byte[] createMixedPathsPdf()
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (Document document = new Document())
{
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
var canvas = writer.DirectContent;
canvas.SetLineWidth(10);
canvas.MoveTo(100, 700);
canvas.CurveTo(180, 720, 180, 720, 200, 800);
canvas.CurveTo(220, 720, 220, 720, 350, 700);
canvas.MoveTo(350, 700);
canvas.CurveTo(220, 680, 220, 680, 210, 650);
canvas.Stroke();
canvas.SetLineCap(PdfContentByte.LINE_CAP_BUTT);
canvas.SetLineJoin(PdfContentByte.LINE_JOIN_BEVEL);
canvas.SetGState(createGState(PdfContentByte.LINE_CAP_BUTT, PdfContentByte.LINE_JOIN_BEVEL));
canvas.MoveTo(100, 500);
canvas.CurveTo(180, 520, 180, 520, 200, 600);
canvas.CurveTo(220, 520, 220, 520, 350, 500);
canvas.MoveTo(350, 500);
canvas.CurveTo(220, 480, 220, 480, 210, 450);
canvas.Stroke();
canvas.SetLineCap(PdfContentByte.LINE_CAP_PROJECTING_SQUARE);
canvas.SetLineJoin(PdfContentByte.LINE_JOIN_MITER);
canvas.SetGState(createGState(PdfContentByte.LINE_CAP_PROJECTING_SQUARE, PdfContentByte.LINE_JOIN_MITER));
canvas.MoveTo(100, 300);
canvas.CurveTo(180, 320, 180, 320, 200, 400);
canvas.CurveTo(220, 320, 220, 320, 350, 300);
canvas.MoveTo(350, 300);
canvas.CurveTo(220, 280, 220, 280, 210, 250);
canvas.Stroke();
canvas.SetLineCap(PdfContentByte.LINE_CAP_ROUND);
canvas.SetLineJoin(PdfContentByte.LINE_JOIN_ROUND);
canvas.SetGState(createGState(PdfContentByte.LINE_CAP_ROUND, PdfContentByte.LINE_JOIN_ROUND));
canvas.MoveTo(100, 100);
canvas.CurveTo(180, 120, 180, 120, 200, 200);
canvas.CurveTo(220, 120, 220, 120, 350, 100);
canvas.MoveTo(350, 100);
canvas.CurveTo(220, 080, 220, 080, 210, 050);
canvas.Stroke();
}
return memoryStream.ToArray();
}
}
PdfGState createGState(int lineCap, int lineJoin)
{
PdfGState pdfGState = new PdfGState();
pdfGState.Put(new PdfName("LC"), new PdfNumber(lineCap));
pdfGState.Put(new PdfName("LJ"), new PdfNumber(lineJoin));
return pdfGState;
}