Wednesday, November 12, 2014
Generate PDF file in MVC using itextsharp dll
//pdf file Generate Code
Document pdfDoc = new Document(PageSize.A4, 25, 10, 10, 10);
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, pdfDoc.PageSize.Height, 0.99f);
pdfDoc.Open();
Font font11 = FontFactory.GetFont("ARIAL", 11);
Font font12 = FontFactory.GetFont("ARIAL", 11,Font.UNDERLINE, iTextSharp.text.BaseColor.RED);
Font font20 = FontFactory.GetFont("ARIAL", 20);
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/BBClogo.jpg"));
jpg.ScaleToFit(121F, 131F);
jpg.Alignment = Image.ALIGN_RIGHT;
pdfDoc.Add(jpg);
Paragraph text = new Paragraph(20f, "Commercial Invoice", font20);
text.Alignment = Element.ALIGN_CENTER;
pdfDoc.Add(text);
PdfPTable pdftable1 = new PdfPTable(4);
float[] widthstab1 = new float[] { 1f, 2f, 1f, 2f };
pdftable1.SetWidths(widthstab1);
pdftable1.HorizontalAlignment = 1;
pdftable1.SpacingBefore = 20f;
pdftable1.SpacingAfter = 10f;
PdfPCell cell = new PdfPCell();
pdftable1.AddCell("Sender’s name:");
pdftable1.AddCell(objInternationalBookModels.senderName);
pdftable1.AddCell("Invoice No:");
pdftable1.AddCell(" ");
pdftable1.AddCell("Sender’s Address:");
var pCollAdd = new Paragraph();
if (objInternationalBookModels.collectionCompany != "" && objInternationalBookModels.collectionCompany != null)
{
pCollAdd.Add(objInternationalBookModels.collectionCompany + "," + "\n");
}
if (objInternationalBookModels.collectionaddress1 != "" && objInternationalBookModels.collectionaddress1 != null)
{
pCollAdd.Add(objInternationalBookModels.collectionaddress1 + "," + "\n");
}
if (objInternationalBookModels.collectionaddress2 != "" && objInternationalBookModels.collectionaddress2 != null)
{
pCollAdd.Add(objInternationalBookModels.collectionaddress2 + "," + "\n");
}
if (objInternationalBookModels.collectiontown != "" && objInternationalBookModels.collectiontown != null)
{
pCollAdd.Add(objInternationalBookModels.collectiontown + "," + "\n");
}
if (objInternationalBookModels.collectionpostcode != "" && objInternationalBookModels.collectionpostcode != null)
{
pCollAdd.Add(objInternationalBookModels.collectionpostcode + "," + "\n");
}
if (objInternationalBookModels.CountryName != "" && objInternationalBookModels.CountryName != null)
{
pCollAdd.Add(objInternationalBookModels.CountryName);
}
PdfPCell celsendadd = new PdfPCell(pCollAdd);
celsendadd.Colspan = 2;
pdftable1.AddCell(celsendadd);
PdfPTable nested = new PdfPTable(2);
nested.AddCell("HAWB:");
nested.AddCell(objInternationalBookModels.HAWBNumber);
nested.AddCell("Date:");
nested.AddCell(objInternationalBookModels.collectionDate);
nested.AddCell("Pieces:");
nested.AddCell(objInternationalBookModels.totalNoofpieces.ToString());
nested.AddCell("Weight:");
nested.AddCell(objInternationalBookModels.totalweight.ToString());
nested.AddCell("Currency:");
nested.AddCell("GBP");
PdfPCell nesthousing = new PdfPCell(nested);
nesthousing.Padding = 0f;
pdftable1.AddCell(nesthousing);
pdftable1.AddCell("Consignee Name:");
PdfPCell celDelConname = new PdfPCell(new Phrase(objInternationalBookModels.deliveryContactname));
celDelConname.Colspan = 3;
pdftable1.AddCell(celDelConname);
pdftable1.AddCell("Delivery Address:");
var pDelAdd = new Paragraph();
if (objInternationalBookModels.deliveryCompany != "" && objInternationalBookModels.deliveryCompany != null)
{
pDelAdd.Add(objInternationalBookModels.deliveryCompany + "," + "\n");
}
if (objInternationalBookModels.deliveryaddress1 != "" && objInternationalBookModels.deliveryaddress1 != null)
{
pDelAdd.Add(objInternationalBookModels.deliveryaddress1 + "," + "\n");
}
if (objInternationalBookModels.deliveryaddress2 != "" && objInternationalBookModels.deliveryaddress2 != null)
{
pDelAdd.Add(objInternationalBookModels.deliveryaddress2 + "," + "\n");
}
if (objInternationalBookModels.deliverytown != "" && objInternationalBookModels.deliverytown != null)
{
pDelAdd.Add(objInternationalBookModels.deliverytown + "," + "\n");
}
if (objInternationalBookModels.deliverypostcode != "" && objInternationalBookModels.deliverypostcode != null)
{
pDelAdd.Add(objInternationalBookModels.deliverypostcode + "," + "\n");
}
if (objInternationalBookModels.deliveryCountryName != "" && objInternationalBookModels.deliveryCountryName != null)
{
pDelAdd.Add(objInternationalBookModels.deliveryCountryName);
}
PdfPCell celdeladd = new PdfPCell(pDelAdd);
celdeladd.Colspan = 3;
pdftable1.AddCell(celdeladd);
pdftable1.AddCell("Telephone:");
pdftable1.AddCell(objInternationalBookModels.deliveryContactNo);
pdftable1.AddCell("Shipped via:");
pdftable1.AddCell(objInternationalBookModels.SupplierName);
pdfDoc.Add(pdftable1);
if (objInternationalBookModels.lstContentdetails != null && objInternationalBookModels.lstContentdetails.Count > 0)
{
PdfPTable pdftable2 = new PdfPTable(4);
float[] widthstab2 = new float[] { 1f, 2f, 1f, 2f };
pdftable2.SetWidths(widthstab2);
pdftable2.HorizontalAlignment = 1;
pdftable2.SpacingBefore = 20f;
// pdftable2.SpacingAfter = 20f;
Decimal totalShipmentValue = 0;
pdftable2.AddCell("Quantity");
pdftable2.AddCell("Contents");
pdftable2.AddCell("Item Value");
pdftable2.AddCell("Total Value");
for (int i = 0; i < objInternationalBookModels.lstContentdetails.Count; i++)
{
pdftable2.AddCell(objInternationalBookModels.lstContentdetails[i].quantity.ToString());
pdftable2.AddCell(objInternationalBookModels.lstContentdetails[i].content.ToString());
pdftable2.AddCell(objInternationalBookModels.lstContentdetails[i].totalvalue.ToString());
Decimal TotalXValue = objInternationalBookModels.lstContentdetails[i].quantity * objInternationalBookModels.lstContentdetails[i].totalvalue;
pdftable2.AddCell(TotalXValue.ToString());
totalShipmentValue = totalShipmentValue + TotalXValue;
}
pdfDoc.Add(pdftable2);
PdfPTable pdfTotalValue = new PdfPTable(4);
float[] widthsT = new float[] { 1f, 2f, 1f, 2f };
pdfTotalValue.SetWidths(widthsT);
pdfTotalValue.HorizontalAlignment = 1;
BaseColor basecol = new BaseColor(System.Drawing.Color.Red);
Font fontTotalvalue = FontFactory.GetFont("ARIAL", 12, basecol);
PdfPCell cellTotalValue = new PdfPCell(new Phrase("Total Shipment Value", fontTotalvalue));
cellTotalValue.Colspan = 3;
cellTotalValue.HorizontalAlignment = Element.ALIGN_RIGHT;
pdfTotalValue.AddCell(cellTotalValue);
pdfTotalValue.AddCell(totalShipmentValue.ToString());
pdfDoc.Add(pdfTotalValue);
}
if (objInternationalBookModels.producttype != null && objInternationalBookModels.producttype == "Document")
{
PdfPTable pdftable3 = new PdfPTable(4);
float[] widths = new float[] { 1f, 2f, 1f, 2f };
pdftable3.SetWidths(widths);
pdftable3.HorizontalAlignment = 1;
pdftable3.SpacingBefore = 20f;
//pdftable3.SpacingAfter = 20f;
pdftable3.AddCell("Quantity");
pdftable3.AddCell("Contents");
pdftable3.AddCell("Item Value");
pdftable3.AddCell("Total Value");
pdftable3.AddCell(" ");
pdftable3.AddCell(" ");
pdftable3.AddCell(" ");
pdftable3.AddCell(" ");
pdftable3.AddCell(" ");
pdftable3.AddCell(" ");
pdftable3.AddCell(" ");
pdftable3.AddCell(" ");
pdfDoc.Add(pdftable3);
PdfPTable pdfTotalValueDoc = new PdfPTable(4);
float[] widthsT = new float[] { 1f, 2f, 1f, 2f };
pdfTotalValueDoc.SetWidths(widthsT);
pdfTotalValueDoc.HorizontalAlignment = 1;
BaseColor basecol = new BaseColor(System.Drawing.Color.Red);
Font fontTotalvalue = FontFactory.GetFont("ARIAL", 12, basecol);
PdfPCell cellTotalValue = new PdfPCell(new Phrase("Total Shipment Value", fontTotalvalue));
cellTotalValue.Colspan = 3;
cellTotalValue.HorizontalAlignment = Element.ALIGN_RIGHT;
pdfTotalValueDoc.AddCell(cellTotalValue);
pdfTotalValueDoc.AddCell(" ");
pdfDoc.Add(pdfTotalValueDoc);
}
PdfPTable pdftable4 = new PdfPTable(2);
float[] widthstab4 = new float[] { 1f, 2f };
pdftable4.SetWidths(widthstab4);
pdftable4.HorizontalAlignment = 1;
pdftable4.SpacingBefore = 20f;
pdftable4.SpacingAfter = 30f;
pdftable4.AddCell("Senders Vat No:");
pdftable4.AddCell(" ");
pdftable4.AddCell("Reason For Export:");
pdftable4.AddCell(" ");
pdftable4.AddCell("ECO Licence");
pdftable4.AddCell(" ");
pdftable4.AddCell("Terms of Delivery:");
pdftable4.AddCell(" ");
pdfDoc.Add(pdftable4);
//Chunk chnk = new Chunk("I declare the above information is true and correct to the best of my knowledge.\n", font11);
//chnk.SetUnderline(0.1f, -2f);
Paragraph text2 = new Paragraph("I declare the above information is true and correct to the best of my knowledge.\n", font11);
pdfDoc.Add(text2);
Paragraph text5 = new Paragraph("Signed for on behalf of the shipper.\n", font11);
pdfDoc.Add(text5);
Paragraph text3 = new Paragraph("Name (in print):\n\n\n", font11);
pdfDoc.Add(text3);
Paragraph text4 = new Paragraph("Signature:", font11);
pdfDoc.Add(text4);
PdfAction pdfAct = PdfAction.GotoLocalPage(1, pdfDest, pdfWriter);
pdfWriter.SetOpenAction(pdfAct);
pdfWriter.CloseStream = false;
pdfDoc.Close();
Response.Buffer = true;
Response.ContentType = "Application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Commercial_Invoice_template_2014.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment