Dynamic table PDF generation using WPF

408 views
Skip to first unread message

Support

unread,
Jul 14, 2010, 4:09:24 PM7/14/10
to PDFTron PDFNet SDK
Q: I want to programatically create a PDF with a table (similar to
Excel output).

I saw that I can use PDFNet to create tables should use the library
using pdftron.Xaml (http://www.pdftron.com/pdfnet/
samplecode.html#Xaml2Pdf)? Is this recommended apprach to dynamic PDF
generation with PDFNet & .NET?

I just want to add columns and rows of data to an existing pdf.

--------------------------------

A: The simplest way to programmatically generate PDF with WPF is
using standard WPF/.NET API as shown in the following sample. In case
you do not want to use WPF you can generate document with
ElementBuilder/ElementWriter (e.g. as shown in ElementBuilder sample -
http://www.pdftron.com/pdfnet/samplecode.html#ElementBuilder), but
this is more work (although this gives more control over layout than
WPF).


// This sample shows how to dynamically generate a Table in existing
PDF document.
// The sample relies on pdftron.Xaml.Convert (from Xaml2PDF sample,
see Xaml2PDF for a
// full sample project; http://www.pdftron.com/pdfnet/samplecode.html#Xaml2Pdf)
// to append WPF FlowDocument to an existing PDF.

using System;
using System.IO;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

using pdftron;
using pdftron.Common;
using pdftron.SDF;
using pdftron.PDF;
using pdftron.Xaml;

namespace WPFTableTest
{
class Program
{
static void Main(string[] args)
{
PDFNet.Initialize();
try
{
Table table = CreateTable();

// For more info on FlowDocument see:
// http://msdn.microsoft.com/en-us/library/aa970909.aspx
FlowDocument flowDoc = new FlowDocument();
flowDoc.Blocks.Add(table);

// Now add the table to an exiting PDF document...
using (PDFDoc pdfdoc = new PDFDoc("../../../../TestFiles/
newsletter.pdf"))
{
pdfdoc.InitSecurityHandler();
pdftron.Xaml.ConverterOptions options = new ConverterOptions();
options.NumColumns = 1;
pdftron.Xaml.Convert.ToPdf(pdfdoc, flowDoc, options);
pdfdoc.Save("out.pdf", SDFDoc.SaveOptions.e_remove_unused);
pdfdoc.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}

/// <summary>
/// from MSDN SDK Sample TableCsharpSample
/// </summary>
static public Table CreateTable()
{
Table table1 = new Table();

// Set some global formatting properties for the table.
table1.CellSpacing = 10;
table1.Background = Brushes.White;

// Create 6 columns and add them to the table's Columns collection.
int numberOfColumns = 6;
for (int x = 0; x < numberOfColumns; x++) table1.Columns.Add(new
TableColumn());

// Set alternating background colors for the middle colums.
table1.Columns[1].Background =
table1.Columns[3].Background =
Brushes.LightSteelBlue;
table1.Columns[2].Background =
table1.Columns[4].Background =
Brushes.Beige;

// Create and add an empty TableRowGroup to hold the table's Rows.
table1.RowGroups.Add(new TableRowGroup());

// Add the first (title) row.
table1.RowGroups[0].Rows.Add(new TableRow());

// Alias the current working row for easy reference.
TableRow currentRow = table1.RowGroups[0].Rows[0];

// Global formatting for the title row.
currentRow.Background = Brushes.Silver;
currentRow.FontSize = 40;
currentRow.FontWeight = System.Windows.FontWeights.Bold;

// Add the header row with content,
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("2004
Sales Project"))));
// and set the row to span all 6 columns.
currentRow.Cells[0].ColumnSpan = 6;

// Add the second (header) row.
table1.RowGroups[0].Rows.Add(new TableRow());
currentRow = table1.RowGroups[0].Rows[1];

// Global formatting for the header row.
currentRow.FontSize = 18;
currentRow.FontWeight = FontWeights.Bold;

// Add cells with content to the second row.
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("Product"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Quarter
1"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Quarter
2"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Quarter
3"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Quarter
4"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("TOTAL"))));

table1.RowGroups[0].Rows.Add(new TableRow());
currentRow = table1.RowGroups[0].Rows[2];

// Global formatting for the row.
currentRow.FontSize = 12;
currentRow.FontWeight = FontWeights.Normal;

// Add cells with content to the third row.
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("Widgets"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("$50,000"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("$55,000"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("$60,000"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("$65,000"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("$230,000"))));

// Bold the first cell.
currentRow.Cells[0].FontWeight = FontWeights.Bold;

// Add the fourth row.
table1.RowGroups[0].Rows.Add(new TableRow());
currentRow = table1.RowGroups[0].Rows[3];

// Global formatting for the row.
currentRow.FontSize = 12;
currentRow.FontWeight = FontWeights.Normal;

// Add cells with content to the third row.
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("Wickets"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("$100,000"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("$120,000"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("$160,000"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("$200,000"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new
Run("$580,000"))));

// Bold the first cell.
currentRow.Cells[0].FontWeight = FontWeights.Bold;

table1.RowGroups[0].Rows.Add(new TableRow());
currentRow = table1.RowGroups[0].Rows[4];

// Global formatting for the footer row.
currentRow.Background = Brushes.LightGray;
currentRow.FontSize = 18;
currentRow.FontWeight = System.Windows.FontWeights.Normal;

// Add the header row with content,
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Projected
2004 Revenue: $810,000"))));
// and set the row to span all 6 columns.
currentRow.Cells[0].ColumnSpan = 6;

return table1;
}
}
}
Reply all
Reply to author
Forward
0 new messages