Thursday, July 3, 2014

PDF4NET: Setting Transparent Background for a Barcode

PDF4NET is my favourite PDF manipulation library for .NET. It is very versatile and has many useful and unique features but unfortunately lacks good documentation. So often in order to achieve something it requires some digging and researching to do.

Here is a small trick that allows setting a transparent background for a bar code inside a PDF document.
PDF4NET renders bar codes inside PDF as images. Because of that there is a background colour of a bar code area. By default it's a White colour. If a bar code are is not overlapping with any other coloured content in a PDF document it does not matter. But if a bar code is drawn on top of another coloured area it will look like a white rectangle with a bar code inside it which may not be your exact intention.

PDFBarCode class has a BackColor property of PDFColor type that allows setting a background colour for a bar code area but there is no way to define neither RGB or CMYK colour that is transparent because neither colour model support transparency in PDF. But assigning explicitly BackColor a null value will remove any colour from the background and make a bar code area transparent so only the bar code data will be visible.

PDFBarCode barCode;
barCode.BackColor = null; // set transparent background

1 comment: