To produce a PDF file, your best bet is to use an existing library. iTextSharp is a powerful PDF generation library for .NET (it's a port of the Java iText library). One downfall is a paucity of good free documentation (although Mike was very helpful: http://www.mikesdotnetting.com).
So, with a reference to itextsharp.dll and a couple of usings in the code (iTextSharp.text and iTextSharp.text.pdf), this is basically all you need to generate PDF label sheets from your MVC Controller:
public ActionResult LabelsPdf()
{
// Open a new PDF document
const int pageMargin = 5;
const int pageRows = 5;
const int pageCols = 2;
var doc = new Document();
doc.SetMargins(pageMargin, pageMargin, pageMargin, pageMargin);
var memoryStream = new MemoryStream();
var pdfWriter = PdfWriter.GetInstance(doc, memoryStream);
doc.Open();
// Create the Label table
PdfPTable table = new PdfPTable(pageCols);
table.WidthPercentage = 100f;
table.DefaultCell.Border = 0;
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);
foreach (var Thing in YourCollectionOfThings)
{
#region Label Construction
PdfPCell cell = new PdfPCell();
cell.Border = 0;
cell.FixedHeight = (doc.PageSize.Height - (pageMargin * 2)) / pageRows;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
var contents = new Paragraph();
contents.Alignment = Element.ALIGN_CENTER;
contents.Add(new Chunk(string.Format("Thing #{0}\n", Thing.ThingId), new Font(baseFont, 11f, Font.BOLD)));
contents.Add(new Chunk(string.Format("Thing Name: {0}\n", Thing.Name), new Font(baseFont, 8f)));
cell.AddElement(contents);
table.AddCell(cell);
#endregion
}
table.CompleteRow();
doc.Add(table);
// Close PDF document and send
pdfWriter.CloseStream = false;
doc.Close();
memoryStream.Position = 0;
return File(memoryStream, "application/pdf");
}
Thanks very match,thats help me a lot
ReplyDeleteThank you..!!!
ReplyDeleteThanks for you contribution, it has been quite useful
ReplyDeleteHi, this has been very helpful, but it seems that no matter how I set the margins, the output always looks the same. I'm saving this output to a file, BTW. Any suggestions on this?
ReplyDeleteGreat article! Do you know by any chance, how can i place text fields inside the cell, just like you did, but with an exact position? (x, y) ? I'm creating labels, and some fields need to be placed in different locations...
ReplyDeleteThanks!
Any thoughts on how to handle a gutter between the rows? ie Avery 5160 label
ReplyDeleteI'm getting an File cannot be used as method error on the return File()line
ReplyDelete