Do you have an example of using ElementBuilder.CreatePath ?

29 views
Skip to first unread message

Ryan

unread,
Dec 15, 2020, 1:08:28 PM12/15/20
to PDFTron SDK
Question:
I am trying to create a polygon and was looking for an example of how to use create path.

Answer:
The following C# code shows how to draw a vector path using ElementBuilder.CreatePath

using (PDFDoc doc = new PDFDoc())
using (ElementBuilder eb = new ElementBuilder())
using (ElementWriter writer = new ElementWriter())
{
Page page = doc.PageCreate();
doc.PagePushBack(page);
writer.Begin(page);

double[] points = new double[] { 86.068, 0, 61.466, 0, 56.851, 35.041, 70.691, 35.041, 84.529, 35.041, 110.671,
0, 86.068, 0, 45.217, 30.699, 52.586, 31.149, 60.671, 2.577, 46.821, 4.374, 32.976, 6.171, 37.845, 30.249,
45.217, 30.699, 11.445, 48.453, 16.686, 46.146, 12.12, 23.581, 3.208, 29.735, -5.7, 35.89, 6.204, 50.759, 11.445, 48.453,
26.212,36.642, 32.451,35.37, 32.793,9.778, 21.667,14.369, 10.539,18.961, 19.978,37.916, 26.212,36.642, 26.212,36.642,
58.791,93.913,59.898,102.367, 52.589,106.542, 45.431,101.092, 22.644,83.743, 83.16,75.088, 79.171,51.386, 75.86,31.712,
15.495,37.769, 8.621,68.553, 3.968,89.374, 27.774,118.26, 52.614,118.26, 64.834,118.26, 78.929,107.226, 81.566,93.248,
83.58,82.589, 57.867,86.86, 58.791,93.913, 58.791,93.913};

// Map enums to bytes for CreatePath API
byte moveTo = (byte)PathData.PathSegmentType.e_moveto;
byte lineTo = (byte)PathData.PathSegmentType.e_lineto;
byte curveTo = (byte)PathData.PathSegmentType.e_cubicto;
byte closePath = (byte)PathData.PathSegmentType.e_closepath;
byte[] seg_types = new byte[] { moveTo, curveTo, curveTo, closePath, moveTo, curveTo, curveTo, closePath, moveTo, curveTo, curveTo, closePath, moveTo, curveTo, curveTo, lineTo, closePath, moveTo, curveTo, curveTo, curveTo, curveTo, curveTo, curveTo, lineTo, closePath};

Element element = eb.CreatePath(points, points.Length, seg_types, seg_types.Length);

element.SetPathFill(true);
writer.WriteElement(element);
writer.End();  // save changes to the current page
doc.Save("gnome_foot_logo.pdf", SDFDoc.SaveOptions.e_linearized);
}
Reply all
Reply to author
Forward
0 new messages