In earlier project, I used PowerBuilder to generate PDF. I ran into a lot of issues. But once I had all those figured out, it was not too bad.
Later we bought ComponentOne components for .NET. They have variety features including generate PDF files. So we decided to use it in future projects.
Using it is straight forward, but there are some tricks. In this project, we have some RTF templates. We need to load them, replace the text in those placeholders, merge them together and output to a PDF file.
Instantiation
First I instantiate the c1PrintDocument1
C1PrintDocument c1PrintDocument1 = new C1.C1PrintDocument.C1PrintDocument();Set header and footer
The following code shows how to set header in c1PrintDocument1. This one has table layout. On the left there is a logo bitmap image. The second column is a spacer. The third column displays the title of the document. The forth column shows the page numbers. Set footer is similar to header.
public void SetHeaderWithLogo(string header)
{
// Prepare a style template for the page header and footer
RenderTable headerTable = new RenderTable(c1PrintDocument1);
headerTable.Style.Borders.AllEmpty = true;
headerTable.StyleTableCell.BorderTableHorz.Empty = true;
headerTable.StyleTableCell.BorderTableVert.Empty = true;
headerTable.Columns.AddSome(4);
headerTable.Body.Rows.AddSome(1);
// set columns' widths and alignments
headerTable.Columns[0].WidthStr = "25%";
headerTable.Columns[0].StyleTableCell.ImageAlign.AlignHorz = ImageAlignHorzEnum.Left;
string assemblyName = this.GetType().Namespace;
System.Reflection.Assembly a = System.Reflection.Assembly.Load(assemblyName);
string logoImage = "logo.gif";
Image image = Image.FromStream(a.GetManifestResourceStream(assemblyName + "." + logoImage));
RenderImage renderImage = new RenderImage(headerTable.Document);
renderImage.Image = image;
headerTable.Body.Cell(0, 0).RenderObject = renderImage;
headerTable.Columns[1].WidthStr = "10%";
headerTable.Columns[2].WidthStr = "35%";
headerTable.Columns[2].StyleTableCell.TextAlignHorz = AlignHorzEnum.Center;
headerTable.Columns[2].StyleTableCell.AlignChildrenVert = AlignVertEnum.Bottom;
headerTable.Columns[2].StyleTableCell.Font = new Font("Arial", 14, FontStyle.Bold);
headerTable.Body.Cell(0, 2).RenderText.Text = header;
headerTable.Columns[3].WidthStr = "30%";
headerTable.Columns[3].StyleTableCell.TextAlignHorz = AlignHorzEnum.Right;
headerTable.Columns[3].StyleTableCell.Font = new Font("Arial", 10, FontStyle.Bold);
headerTable.Body.Cell(0, 3).RenderText.Text = "Page [@@PageNo@@] of [@@PageCount@@]";
c1PrintDocument1.PageHeader.RenderObject = headerTable;
}
Here is how to set the height
c1PrintDocument1.PageHeader.Height = height;
Page settings
You can set the page settings the following way. However, there is something need to be mentioned. If the printer the application output to is a network printer, you will receive “No printers installed” exception when execute the following line. You will need to setup a local printer using IP address of the network printer.
public void SetPageSettings(System.Drawing.Printing.PageSettings pageSettings)
{
c1PrintDocument1.PageSettings = pageSettings;
}
Render content
You can render plain text or RTF text using following code.
c1PrintDocument1.StartDoc();
c1PrintDocument1.RenderBlockText(contextText);
RenderRichText obj = new RenderRichText(c1PrintDocument1);
obj.Rtf = contentRTFText;
c1PrintDocument1.RenderBlockRichText(obj.Rtf);
c1PrintDocument1.EndDoc();
Sometimes you may want the whole block of text to be printed on one page, you need to check the available height and start a new page if necessary.
if (c1PrintDocument1.AvailableBlockFlowHeight < c1PrintDocument1.MeasureBlock(obj).Height) c1PrintDocument1.NewPage() Print different header between pages
If you want to print different header on the first page and the rest of pages, you will need to write code in NewPageSetup event
c1PrintDocument1.NewPageSetup += new NewPageSetupEventHandler(c1PrintDocument1_NewPageSetup);
private void c1PrintDocument1_NewPageSetup(C1PrintDocument sender, NewPageSetupEventArgs e)
{
// We will need only change the setting for the first two pages. The rest of pages will be as same as the second page
if (Header != null && Header != string.Empty)
{
if (HeaderOnPageOneOnly != null && HeaderOnPageOneOnly != string.Empty)
{
if (sender.PageCount == 1) SetHeader(true);
else if (sender.PageCount == 2) SetHeader(false);
}
else if (sender.PageCount == 1) SetHeader();
}
}
Print PDF document
public void PrintDocument(string printerIdentifier, short numberOfCopies)
{
System.Drawing.Printing.PrinterSettings settings = new System.Drawing.Printing.PrinterSettings();
settings.PrinterName = printerIdentifier;
settings.Copies = numberOfCopies;
c1PrintDocument1.Print(settings, false);
}
No comments:
Post a Comment