1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Bezier Curve Bounding Boxes

18/12/2019

One of the challenges for generating accurate character sizes in a PDF document I encountered while building PdfPig was working out the bounding box for a cubic Bezier curve.

A cubic Bezier curve is defined by 4 points; the start, end and 2 control points.

We can number the points for use in the formulae in this post:

  • Start: P0
  • Control 1: P1
  • Control 2: P2
  • End: P3

This gives the formula for the Bezier curve:

formula from Wikipedia

...

Open and Create PNG Images in C#

18/12/2019

I wanted a platform independent way to open and create PNG images in C#. BigGustave is a new library which provides a .NET Standard 2.0 compatible way of opening and creating PNG images.

To open a png image you can pass either the bytes or the stream of the image to Png.Open and then retrieve the values for pixels at any location in the image:

Png png = Png.Open(File.ReadAllBytes(@"C:\pictures\example.png"));
Pixel first = png.GetPixel(0, 0);
Console.WriteLine($"R: {first.R}, G: {first.G}, B: {first.B}");

To create a .png image in C# use the PngBuilder to define pixel values before saving to an output stream:

var builder = PngBuilder.Create(2, 2, false);

var red = new Pixel(255, 0, 0);

builder.SetPixel(red, 0, 0);
builder.SetPixel(red, 1, 1);

using (var memory = new MemoryStream())
{
    builder.Save(memory);

    return memory.ToArray();
}

BigGustave is completely open source and is available on NuGet now so if you need very basic PNG manipulation tools for platform independent .NET code why not check it out?

...

PdfPig Version 0.0.5

30/12/2018

Today version 0.0.5 of PdfPig was released. This is the first version which includes the ability to create PDF documents in C#.

There aren't many fully open source options around for both reading and writing PDF documents so the addition of PDF document creation to PdfPig is an exciting next step for the API.

The actual design of document creation isn't finished yet and there's more work to be done around the currently unsupported use cases such as splitting, merging and editing existing documents as well as adding non-ASCII text, working with forms and adding images to new documents but the functionality in 0.0.5 should provide enough for simple use cases and the open source Apache 2.0 license means that it can be used in commercial software.

You can create a new document using a document builder:

PdfDocumentBuilder builder = new PdfDocumentBuilder();

This creates a completely empty document. To add the first page we use the imaginatively named add page method.

PdfPageBuilder page = builder.AddPage(PageSize.A4);

This supports various page sizes defined by the PageSize enum, such as the North American standard PageSize.Letter. It also allows the choice of portrait (default) or landscape pages.

Once a page builder has been created text, lines and rectangles can be added to it.

Fonts

In order to draw text a font must be chosen. Version 0.0.5 supports TrueType fonts as well as the 14 default fonts detailed in the PDF Specification. These are called the Standard 14 fonts and while their use is beginning to be phased out, all PDF readers should still support them.

...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18