PDF for UWP | ComponentOne
Using PDF for UWP / Adding Graphics
In This Topic
    Adding Graphics
    In This Topic

    The C1PdfDocument class exposes several methods that allow you to add graphical elements to your documents, including lines, rectangles, ellipses, pies, arcs, rounded rectangles, polygons, Bezier curves, and so on.

    The methods are a subset of those found in the .NET Graphics class, and use the same Brush and Pen classes to control the color and style of the lines and filled areas.

    It is important to remember that PDF for UWP uses a coordinate system based on points, with the origin located at the top left of the page. (The default coordinate system for the .NET Graphics class is pixel-based.)

    The example below illustrates how similar the graphics methods are between PDF for UWP and the .NET Graphics class. The sample declares a C1PdfDocument class called 'pdf' and calls methods to draw pies, Bezier curve, and other graphical elements.

    The point of the sample is that if you replaced the C1PdfDocument class with a regular .NET Graphics object, you would be able to compile the code and get the same results:

    Copy Code
    ' set up to draw
    Dim rc As New Rect(0, 0, 300, 200)
    Dim text As String = "Hello world of .NET Graphics and PDF." & vbCr & vbLf & "Nice to meet you."
    Dim font As New Font("Times New Roman", 12, PdfFontStyle.Italic Or PdfFontStyle.Underline)
    ' draw to pdf document
    Dim penWidth As Integer = 0
    Dim penRGB As Byte = 0
    pdf.FillPie(Colors.Red, rc, 0, 20F)
    pdf.FillPie(Colors.Green, rc, 20F, 30F)
    pdf.FillPie(Colors.Blue, rc, 60F, 12F)
    pdf.FillPie(Colors.Orange, rc, -80F, -20F)
    For startAngle As Single = 0 To 359 Step 40
        Dim penColor As Color = Color.FromArgb(&Hff, penRGB, penRGB, penRGB)
        Dim pen As New Pen(penColor, System.Math.Max(System.Threading.Interlocked.Increment(penWidth),penWidth - 1))
        penRGB = CByte(penRGB + 20)
        pdf.DrawArc(pen, rc, startAngle, 40F)
    pdf.DrawRectangle(Colors.Red, rc)
    pdf.DrawString(text, font, Colors.Black, rc)
    ' show a Bezier curve
    Dim pts = New Point() {New Point(400, 100), New Point(420, 30), New Point(500, 140), New Point(530, 20)}
    ' draw Bezier
    pdf.DrawBezier(New Pen(Colors.Blue, 4), pts(0), pts(1), pts(2), pts(3))
    ' show Bezier control points
    pdf.DrawLines(Colors.Gray, pts)
    For Each pt As Point In pts
        pdf.FillRectangle(Colors.Red, pt.X - 2, pt.Y - 2, 4, 4)
    Copy Code
    // set up to draw
    Rect rc = new Rect(0, 0, 300, 200);
    string text = "Hello world of .NET Graphics and PDF.\r\nNice to meet you.";
    Font font = new Font("Times New Roman", 12, PdfFontStyle.Italic | PdfFontStyle.Underline);
    // draw to pdf document
    int penWidth = 0;
    byte penRGB = 0;
    pdf.FillPie(Colors.Red, rc, 0, 20f);
    pdf.FillPie(Colors.Green, rc, 20f, 30f);
    pdf.FillPie(Colors.Blue, rc, 60f, 12f);
    pdf.FillPie(Colors.Orange, rc, -80f, -20f);
    for (float startAngle = 0; startAngle < 360; startAngle += 40)
        Color penColor = Color.FromArgb(0xff, penRGB, penRGB, penRGB);
        Pen pen = new Pen(penColor, penWidth++);
        penRGB = (byte)(penRGB + 20);
        pdf.DrawArc(pen, rc, startAngle, 40f);
    pdf.DrawRectangle(Colors.Red, rc);
    pdf.DrawString(text, font, Colors.Black, rc);
    // show a Bezier curve
    var pts = new Point[]
        new Point(400, 100), new Point(420,  30),
        new Point(500, 140), new Point(530,  20),
    // draw Bezier 
    pdf.DrawBezier(new Pen(Colors.Blue, 4), pts[0], pts[1], pts[2], pts[3]);
    // show Bezier control points
    pdf.DrawLines(Colors.Gray, pts);
    foreach (Point pt in pts)
        pdf.FillRectangle(Colors.Red, pt.X - 2, pt.Y - 2, 4, 4);