Skip to main content

Creating Tables with Bytescout.PDF SDK

Back-end Development

If you're reading this is is probably because you're already familiar with the Bytescout.PDF SDK.  For those of you who aren't, here is the link http://bytescout.com/bytescoutpdf.html. Some PDF libraries for .NET already contain functions that create and maintain tables.  For instance the ITextSharp library contain a Table object, that facilitates creating a table very nicely.  Without something similar in the Bytescout library I had to come up with my own. The reason we went with Bytescout in the first place was due to the need for reading and manipulating PDF documents already created, as well as the need for the resulting PDF to be optimized and compressed. Every developer's needs are slightly different so flexibility is needed.  My solution isn't all that flexible, or full featured, but it will provide a good starting point.  I've shared my approach below, hopefully you will find it helpful. Here is the class I created for drawing tables....

using System.Collections.Generic;
using System.Data;
using Bytescout.PDF;

namespace BytescoutTable
{
public class BytescoutTable
{
private readonly Drawing _drawing;

public BytescoutTable(Drawing drawing)
{
_drawing = drawing;
}

public double RowHeight { get; set; }

/// 

/// Prints a table to the PDF using a DataTable ///

/// /// /// public void DrawTable(DataTable table, double x, double y) { if (RowHeight <= 0) RowHeight = 14f; double xcursor = 0f; double ycursor = 0f; List columnWidths = new List(); foreach (DataColumn column in table.Columns) { double columnWidth = _drawing.GetTextWidth(column.ColumnName) + 10f; DrawRectangle(x + xcursor, y, columnWidth, RowHeight, column.ColumnName); xcursor += columnWidth; columnWidths.Add(columnWidth); } foreach (DataRow row in table.Rows) { ycursor += RowHeight; xcursor = 0; for (int i = 0; i < table.Columns.Count; i++) { DrawRectangle(x + xcursor, y + ycursor, columnWidths[i], RowHeight, row[i].ToString()); xcursor += columnWidths[i]; } } } ///
/// Simply Draws a Rectange with the given information ///
/// /// /// /// /// /// private void DrawRectangle(double x, double y, double width, double height, string text, Color backgroundColor) { _drawing.SetLineWidth(1.0f); _drawing.rectangle(x, y, x + width, y - height); // UP-LEFT / DOWN-RIGHT _drawing.SetFillColor(backgroundColor); _drawing.FillAndStroke(); _drawing.SetFillColor(Drawing.RGBToColor(0, 0, 0)); _drawing.SetStrokeColor(Drawing.RGBToColor(0, 0, 0)); _drawing.PlaceText(x + 5f, y - 13f, 0f, text); _drawing.Stroke(); } ///
/// Simply Draws a Rectange with the given information /// 

/// /// /// /// /// private void DrawRectangle(double x, double y, double width, double height, string text) { DrawRectangle(x, y, width, height, text, Drawing.RGBToColor(255,255,255)); } } }


The following shows you how you can test this functionality.  A simple Console Application that uses the class above.


using System.Data;
using Bytescout.PDF;

namespace Test
{
class Program
{
static void Main(string[] args)
{
// Create main PDF Doc Engine
PDFDocEngine engine = new PDFDocEngine("demo", "demo");

// Add new document
Document document = engine.AddDocument();

// Append new page to the document
Page page = document.AddPage(PageSizeType.A3,
PageOrientationType.LandScape);

// Create new drawing
Drawing drawing = page.AddDrawing();

DataTable table = new DataTable("TestTable");
table.Columns.Add("Column One");
table.Columns.Add("Column Two");
DataRow row = table.NewRow();
row[0] = "test";
row[1] = "two";
table.Rows.Add(row);

// Add standard font
uint font = document.AddFontStandard(StandardFontType.Courier,
FontEncodingType.WinAnsi);

// Set Active Font
drawing.SetActiveFont(font, 10, false, false);

BytescoutTable.BytescoutTable testTable =
new BytescoutTable.BytescoutTable(drawing) {RowHeight = 20f};
testTable.DrawTable(table, 100, 100);

// Closing drawing on the page
drawing.Close();

// Save document
document.Save("HelloWorld.pdf");

}
}
}

 

Need a fresh perspective on a tough project?

Let’s talk about how RDG can help.

Contact Us