我有一个场景,我需要创建一个带有邮件标签的PDF。所有页面属性都是用户定义的,例如每页行数,无列,页面高度,宽度,左边距等等......沿此,我可能需要省略一定数量的单元格以重复使用已使用的页面。这个有什么示例代码吗?
答案 0 :(得分:3)
可能会帮助你。
using System;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text;
using System.Diagnostics;
protected void btnGeneratePDFFile_Click(object sender, EventArgs e)
{
//Create document
Document doc = new Document();
//Create PDF Table
PdfPTable tableLayout = new PdfPTable(4);
//Create a PDF file in specific path
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("Sample-PDF-File.pdf"), FileMode.Create));
//Open the PDF document
doc.Open();
//Add Content to PDF
doc.Add(Add_Content_To_PDF(tableLayout));
// Closing the document
doc.Close();
btnOpenPDFFile.Enabled = true;
btnGeneratePDFFile.Enabled = false;
}
private PdfPTable Add_Content_To_PDF(PdfPTable tableLayout)
{
float[] headers = { 20, 20, 30, 30 }; //Header Widths
tableLayout.SetWidths(headers); //Set the pdf headers
tableLayout.WidthPercentage = 80; //Set the PDF File witdh percentage
//Add Title to the PDF file at the top
tableLayout.AddCell(new PdfPCell(new Phrase("Creating PDF file using iTextsharp", new Font(Font.HELVETICA, 13, 1, new iTextSharp.text.Color(153, 51, 0)))) { Colspan = 4, Border = 0, PaddingBottom = 20, HorizontalAlignment = Element.ALIGN_CENTER });
//Add header
AddCellToHeader(tableLayout, "Cricketer Name");
AddCellToHeader(tableLayout, "Height");
AddCellToHeader(tableLayout, "Born On");
AddCellToHeader(tableLayout, "Parents");
//Add body
AddCellToBody(tableLayout, "Sachin Tendulkar");
AddCellToBody(tableLayout, "1.65 m");
AddCellToBody(tableLayout, "April 24, 1973");
AddCellToBody(tableLayout, "Ramesh Tendulkar, Rajni Tendulkar");
AddCellToBody(tableLayout, "Mahendra Singh Dhoni");
AddCellToBody(tableLayout, "1.75 m");
AddCellToBody(tableLayout, "July 7, 1981");
AddCellToBody(tableLayout, "Devki Devi, Pan Singh");
AddCellToBody(tableLayout, "Virender Sehwag");
AddCellToBody(tableLayout, "1.70 m");
AddCellToBody(tableLayout, "October 20, 1978");
AddCellToBody(tableLayout, "Aryavir Sehwag, Vedant Sehwag");
AddCellToBody(tableLayout, "Virat Kohli");
AddCellToBody(tableLayout, "1.75 m");
AddCellToBody(tableLayout, "November 5, 1988");
AddCellToBody(tableLayout, "Saroj Kohli, Prem Kohli");
return tableLayout;
}
// Method to add single cell to the header
private static void AddCellToHeader(PdfPTable tableLayout,string cellText)
{
tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.HELVETICA, 8, 1, iTextSharp.text.Color.WHITE))) {HorizontalAlignment = Element.ALIGN_CENTER, Padding=5, BackgroundColor = new iTextSharp.text.Color(0, 51, 102) });
}
// Method to add single cell to the body
private static void AddCellToBody(PdfPTable tableLayout, string cellText)
{
tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.HELVETICA, 8, 1, iTextSharp.text.Color.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER,Padding = 5, BackgroundColor = iTextSharp.text.Color.WHITE });
}
btnGeneratePDFFile_Click
在这种方法中,我在特定位置创建PDF文件。然后我调用Add_Content_To_PDF方法。
Add_Content_To_PDF
在这个方法中,我给出了单元格的大小以及PDF文件的宽度,然后我使用AddCellToHeader和AddCellToBody两种方法将内容添加到PDF文件中。
AddCellToHeader
在这种方法中,我将一个单元格添加到PDF标题中,并带有一些样式。
AddCellToBody
在这个方法中,我将一个单元格添加到PDF主体中。
现在,在“打开PDF”按钮中,单击以下代码以打开已创建的PDF:
protected void btnOpenPDFFile_Click(object sender, EventArgs e)
{
//Open the PDF file
Process.Start(Server.MapPath("Sample-PDF-File.pdf"));
btnGeneratePDFFile.Enabled = true;
btnOpenPDFFile.Enabled = false;
}