HiQPdf Documentation

PDF Graphics

Quickly Create High Quality PDFs

The graphic PDF objects in HiQPdf software are the lines, rectangles, circles, ellipses, ellipse arcs, ellipse slices, Bezier curves and polygons. The graphic objects can be laid out in any position in a canvas and they are not paginated.

The graphic PDF objects are all derived from the PdfDrawableObject class which is a specialization of the PdfObject generic class.

PDF Graphics Demo

In this demo you can learn how to layout graphics in a PDF document with various line styles and fill modes. There are examples for drawing lines, circles, ellipses, elippses arcs and slices, rectangles, Bezier curves and polygons.

Demo Source Code

C#
private void buttonCreatePdf_Click(object sender, EventArgs e)
{
    // create a PDF document
    PdfDocument document = new PdfDocument();

    // create a page in document
    PdfPage page1 = document.AddPage();

    // create the true type fonts that can be used in document text
    Font sysFont = new Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point);
    PdfFont pdfFont = document.CreateFont(sysFont);
    PdfFont pdfFontEmbed = document.CreateFont(sysFont, true);

    float crtYPos = 20;
    float crtXPos = 5;

    PdfLayoutInfo textLayoutInfo = null;
    PdfLayoutInfo graphicsLayoutInfo = null;

    #region Layout lines

    PdfText titleTextLines = new PdfText(crtXPos, crtYPos, "Lines:", pdfFontEmbed);
    titleTextLines.ForeColor = Color.Navy;
    textLayoutInfo = page1.Layout(titleTextLines);

    // advance the Y position in the PDF page
    crtYPos = textLayoutInfo.LastPageRectangle.Bottom + 10;

    // layout a simple line
    PdfLine pdfLine = new PdfLine(new PointF(crtXPos, crtYPos),  new PointF(crtXPos + 60, crtYPos));
    graphicsLayoutInfo = page1.Layout(pdfLine);

    // layout a thick line
    PdfLine pdfThickLine = new PdfLine(new PointF(crtXPos + 70, crtYPos), new PointF(crtXPos + 130, crtYPos));
    pdfThickLine.LineStyle.LineWidth = 3;
    graphicsLayoutInfo = page1.Layout(pdfThickLine);

    // layout a dotted colored line
    PdfLine pdfDottedLine = new PdfLine(new PointF(crtXPos + 140, crtYPos), new PointF(crtXPos + 200, crtYPos));
    pdfDottedLine.LineStyle.LineDashPattern = PdfLineDashPattern.Dot;
    pdfDottedLine.ForeColor = Color.Green;
    graphicsLayoutInfo = page1.Layout(pdfDottedLine);

    // layout a dashed colored line
    PdfLine pdfDashedLine = new PdfLine(new PointF(crtXPos + 210, crtYPos), new PointF(crtXPos + 270, crtYPos));
    pdfDashedLine.LineStyle.LineDashPattern = PdfLineDashPattern.Dash;
    pdfDashedLine.ForeColor = Color.Green;
    graphicsLayoutInfo = page1.Layout(pdfDashedLine);

    // layout a dash-dot-dot colored line
    PdfLine pdfDashDotDotLine = new PdfLine(new PointF(crtXPos + 280, crtYPos), new PointF(crtXPos + 340, crtYPos));
    pdfDashDotDotLine.LineStyle.LineDashPattern = PdfLineDashPattern.DashDotDot;
    pdfDashDotDotLine.ForeColor = Color.Green;
    graphicsLayoutInfo = page1.Layout(pdfDashDotDotLine);

    // layout a thick line with rounded cap style
    PdfLine pdfRoundedLine = new PdfLine(new PointF(crtXPos + 350, crtYPos), new PointF(crtXPos + 410, crtYPos));
    pdfRoundedLine.LineStyle.LineWidth = 5;
    pdfRoundedLine.LineStyle.LineCapStyle = PdfLineCapStyle.RoundCap;
    pdfRoundedLine.ForeColor = Color.Blue;
    graphicsLayoutInfo = page1.Layout(pdfRoundedLine);

    // advance the Y position in the PDF page
    crtYPos += 10;

    #endregion

    #region Layout circles

    PdfText titleTextCircles = new PdfText(crtXPos, crtYPos, "Circles:", pdfFontEmbed);
    titleTextCircles.ForeColor = Color.Navy;
    textLayoutInfo = page1.Layout(titleTextCircles);

    // advance the Y position in the PDF page
    crtYPos = textLayoutInfo.LastPageRectangle.Bottom + 10;

    // draw a simple circle with solid line
    PdfCircle pdfCircle = new PdfCircle(new PointF(crtXPos + 30, crtYPos + 30), 30);
    page1.Layout(pdfCircle);

    // draw a simple circle with dotted border line
    PdfCircle pdfDottedCircle = new PdfCircle(new PointF(crtXPos + 100, crtYPos + 30), 30);
    pdfDottedCircle.ForeColor = Color.Green;
    pdfDottedCircle.LineStyle.LineDashPattern = PdfLineDashPattern.Dot;
    graphicsLayoutInfo = page1.Layout(pdfDottedCircle);

    // draw a circle with colored border line and fill color
    PdfCircle pdfDisc = new PdfCircle(new PointF(crtXPos + 170, crtYPos + 30), 30);
    pdfDisc.ForeColor = Color.Navy;
    pdfDisc.BackColor = Color.WhiteSmoke;
    graphicsLayoutInfo = page1.Layout(pdfDisc);

    // draw a circle with thick border line
    PdfCircle pdfThickBorderDisc = new PdfCircle(new PointF(crtXPos + 240, crtYPos + 30), 30);
    pdfThickBorderDisc.LineStyle.LineWidth = 5;
    pdfThickBorderDisc.BackColor = Color.LightSalmon;
    pdfThickBorderDisc.ForeColor = Color.LightBlue;

    graphicsLayoutInfo = page1.Layout(pdfThickBorderDisc);

    crtYPos = graphicsLayoutInfo.LastPageRectangle.Bottom + 10;

    #endregion

    #region Layout ellipses and arcs

    PdfText titleTextEllipses = new PdfText(crtXPos, crtYPos, "Ellipses, arcs and slices:", pdfFontEmbed);
    titleTextEllipses.ForeColor = Color.Navy;
    textLayoutInfo = page1.Layout(titleTextEllipses);

    // advance the Y position in the PDF page
    crtYPos = textLayoutInfo.LastPageRectangle.Bottom + 10;

    // draw a simple ellipse with solid line
    PdfEllipse pdfEllipse = new PdfEllipse(new PointF(crtXPos + 50, crtYPos + 30), 50, 30);
    graphicsLayoutInfo = page1.Layout(pdfEllipse);

    // draw an ellipse with fill color and colored border line
    PdfEllipse pdfFilledEllipse = new PdfEllipse(new PointF(crtXPos + 160, crtYPos + 30), 50, 30);
    pdfFilledEllipse.BackColor = Color.WhiteSmoke;
    pdfFilledEllipse.ForeColor = Color.Green;
    graphicsLayoutInfo = page1.Layout(pdfFilledEllipse);

    // draw an ellipse arc with colored border line
    PdfEllipseArc pdfEllipseArc1 = new PdfEllipseArc(new RectangleF(crtXPos + 220, crtYPos, 100, 60), 0, 90);
    pdfEllipseArc1.ForeColor = Color.OrangeRed;
    pdfEllipseArc1.LineStyle.LineWidth = 3;
    graphicsLayoutInfo = page1.Layout(pdfEllipseArc1);

    // draw an ellipse arc with fill color and colored border line
    PdfEllipseArc pdfEllipseArc2 = new PdfEllipseArc(new RectangleF(crtXPos + 220, crtYPos, 100, 60), 90, 90);
    pdfEllipseArc2.ForeColor = Color.Navy;
    graphicsLayoutInfo = page1.Layout(pdfEllipseArc2);

    // draw an ellipse arc with fill color and colored border line
    PdfEllipseArc pdfEllipseArc3 = new PdfEllipseArc(new RectangleF(crtXPos + 220, crtYPos, 100, 60), 180, 90);
    pdfEllipseArc3.ForeColor = Color.Green;
    pdfEllipseArc3.LineStyle.LineWidth = 3;
    graphicsLayoutInfo = page1.Layout(pdfEllipseArc3);

    // draw an ellipse arc with fill color and colored border line
    PdfEllipseArc pdfEllipseArc4 = new PdfEllipseArc(new RectangleF(crtXPos + 220, crtYPos, 100, 60), 270, 90);
    pdfEllipseArc4.ForeColor = Color.Yellow;
    graphicsLayoutInfo = page1.Layout(pdfEllipseArc4);


    // draw an ellipse slice 
    PdfEllipseSlice pdfEllipseSlice1 = new PdfEllipseSlice(new RectangleF(crtXPos + 330, crtYPos, 100, 60), 0, 90);
    pdfEllipseSlice1.BackColor = Color.Coral;
    graphicsLayoutInfo = page1.Layout(pdfEllipseSlice1);

    // draw an ellipse slice 
    PdfEllipseSlice pdfEllipseSlice2 = new PdfEllipseSlice(new RectangleF(crtXPos + 330, crtYPos, 100, 60), 90, 90);
    pdfEllipseSlice2.BackColor = Color.LightBlue;
    graphicsLayoutInfo = page1.Layout(pdfEllipseSlice2);

    // draw an ellipse slice 
    PdfEllipseSlice pdfEllipseSlice3 = new PdfEllipseSlice(new RectangleF(crtXPos + 330, crtYPos, 100, 60), 180, 90);
    pdfEllipseSlice3.BackColor = Color.LightGreen;
    graphicsLayoutInfo = page1.Layout(pdfEllipseSlice3);

    // draw an ellipse slice 
    PdfEllipseSlice pdfEllipseSlice4 = new PdfEllipseSlice(new RectangleF(crtXPos + 330, crtYPos, 100, 60), 270, 90);
    pdfEllipseSlice4.BackColor = Color.Yellow;
    graphicsLayoutInfo = page1.Layout(pdfEllipseSlice4);

    crtYPos = graphicsLayoutInfo.LastPageRectangle.Bottom + 10;

    #endregion

    #region Layout rectangles

    PdfText titleTextRectangles = new PdfText(crtXPos, crtYPos, "Rectangles:", pdfFontEmbed);
    titleTextRectangles.ForeColor = Color.Navy;
    textLayoutInfo = page1.Layout(titleTextRectangles);

    // advance the Y position in the PDF page
    crtYPos = textLayoutInfo.LastPageRectangle.Bottom + 10;

    // draw a simple rectangle with solid line
    PdfRectangle pdfRectangle = new PdfRectangle(crtXPos, crtYPos, 100, 60);
    graphicsLayoutInfo = page1.Layout(pdfRectangle);

    // draw a rectangle with fill color and colored dotted border line
    PdfRectangle pdfFilledRectangle = new PdfRectangle(crtXPos + 110, crtYPos, 100, 60);
    pdfFilledRectangle.BackColor = Color.WhiteSmoke;
    pdfFilledRectangle.ForeColor = Color.Green;
    pdfFilledRectangle.LineStyle.LineDashPattern = PdfLineDashPattern.Dot;
    graphicsLayoutInfo = page1.Layout(pdfFilledRectangle);

    // draw a rectangle with fill color and without border line
    PdfRectangle pdfFilledNoBorderRectangle = new PdfRectangle(crtXPos + 220, crtYPos, 100, 60);
    pdfFilledNoBorderRectangle.BackColor = Color.LightGreen;
    graphicsLayoutInfo = page1.Layout(pdfFilledNoBorderRectangle);

    // draw a rectangle filled with a thick border line and reounded corners
    PdfRectangle pdfThickBorderRectangle = new PdfRectangle(crtXPos + 330, crtYPos, 100, 60);
    pdfThickBorderRectangle.BackColor = Color.LightSalmon;
    pdfThickBorderRectangle.ForeColor = Color.LightBlue;
    pdfThickBorderRectangle.LineStyle.LineWidth = 5;
    pdfThickBorderRectangle.LineStyle.LineJoinStyle = PdfLineJoinStyle.RoundJoin;
    graphicsLayoutInfo = page1.Layout(pdfThickBorderRectangle);

    crtYPos = graphicsLayoutInfo.LastPageRectangle.Bottom + 10;

    #endregion

    #region Layout Bezier curves

    PdfText titleTextBezier = new PdfText(crtXPos, crtYPos, "Bezier curves and polygons:", pdfFontEmbed);
    titleTextBezier.ForeColor = Color.Navy;
    textLayoutInfo = page1.Layout(titleTextBezier);

    // advance the Y position in the PDF page
    crtYPos = textLayoutInfo.LastPageRectangle.Bottom + 10;

    // the points controlling the Bezier curve
    PointF pt1 = new PointF(crtXPos, crtYPos + 50);
    PointF pt2 = new PointF(crtXPos + 50, crtYPos);
    PointF pt3 = new PointF(crtXPos + 100, crtYPos + 100);
    PointF pt4 = new PointF(crtXPos + 150, crtYPos + 50);

    // draw controlling points
    PdfCircle pt1Circle = new PdfCircle(pt1, 2);
    pt1Circle.BackColor = Color.LightSalmon;
    page1.Layout(pt1Circle);
    PdfCircle pt2Circle = new PdfCircle(pt2, 2);
    pt2Circle.BackColor = Color.LightSalmon;
    page1.Layout(pt2Circle);
    PdfCircle pt3Circle = new PdfCircle(pt3, 2);
    pt3Circle.BackColor = Color.LightSalmon;
    page1.Layout(pt3Circle);
    PdfCircle pt4Circle = new PdfCircle(pt4, 2);
    pt4Circle.BackColor = Color.LightSalmon;
    page1.Layout(pt4Circle);

    // draw the Bezier curve

    PdfBezierCurve bezierCurve = new PdfBezierCurve(pt1, pt2, pt3, pt4);
    bezierCurve.ForeColor = Color.LightBlue;
    bezierCurve.LineStyle.LineWidth = 2;
    graphicsLayoutInfo = page1.Layout(bezierCurve);

    #endregion

    #region Layout a polygons

    // layout a polygon without fill color

    // the polygon points
    PointF[] polyPoints = new PointF[]{
        new PointF(crtXPos + 200, crtYPos + 50),
        new PointF(crtXPos + 250, crtYPos),
        new PointF(crtXPos + 300, crtYPos + 50),
        new PointF(crtXPos + 250, crtYPos + 100)
    };

    // draw polygon points
    foreach (PointF polyPoint in polyPoints)
    {
        PdfCircle pointCircle = new PdfCircle(polyPoint, 2);
        pointCircle.BackColor = Color.LightSalmon;
        page1.Layout(pointCircle);
    }

    // draw the polygon line
    PdfPolygon pdfPolygon = new PdfPolygon(polyPoints);
    pdfPolygon.ForeColor = Color.LightBlue;
    pdfPolygon.LineStyle.LineWidth = 2;
    pdfPolygon.LineStyle.LineCapStyle = PdfLineCapStyle.ProjectingSquareCap;
    graphicsLayoutInfo = page1.Layout(pdfPolygon);

    // layout a polygon with fill color

    // the polygon points
    PointF[] polyFillPoints = new PointF[]{
        new PointF(crtXPos + 330, crtYPos + 50),
        new PointF(crtXPos + 380, crtYPos),
        new PointF(crtXPos + 430, crtYPos + 50),
        new PointF(crtXPos + 380, crtYPos + 100)
    };

    // draw a polygon with fill color and thick rounded border
    PdfPolygon pdfFillPolygon = new PdfPolygon(polyFillPoints);
    pdfFillPolygon.ForeColor = Color.LightBlue;
    pdfFillPolygon.BackColor = Color.LightSalmon;
    pdfFillPolygon.LineStyle.LineWidth = 5;
    pdfFillPolygon.LineStyle.LineCapStyle = PdfLineCapStyle.RoundCap;
    pdfFillPolygon.LineStyle.LineJoinStyle = PdfLineJoinStyle.RoundJoin;
    graphicsLayoutInfo = page1.Layout(pdfFillPolygon);

    #endregion

    Cursor = Cursors.WaitCursor;
    string pdfFile = Application.StartupPath + @"\DemoOutput\PdfGraphics.pdf";
    try
    {
        document.WriteToFile(pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Cannot create the PDF document. {0}", ex.Message));
        return;
    }
    finally
    {
        document.Close();
        Cursor = Cursors.Arrow;
    }

    // open the created PDF document
    try
    {
        System.Diagnostics.Process.Start(pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("The PDF document was created but cannot open '{0}'. {1}", pdfFile, ex.Message));
    }
}
See Also

Other Resources