C# 如何使用ITextsharper定制自己的P.D.F表格?
这里我附上了生成p.D.F文档的代码,使用此代码我可以获得p.D.F结构,但我想在这里设计表单,如(工资单)C# 如何使用ITextsharper定制自己的P.D.F表格?,c#,asp.net-mvc,pdf-generation,C#,Asp.net Mvc,Pdf Generation,这里我附上了生成p.D.F文档的代码,使用此代码我可以获得p.D.F结构,但我想在这里设计表单,如(工资单) public void Add(int employeenumber,string fromdate,string todate) { 字符串pdfFilePath=null; Document doc=新文档(iTextSharp.text.PageSize.LETTER,10,10,42,35); 尝试 { pdfFilePath=Server.MapPath(“~/Uploads/
public void Add(int employeenumber,string fromdate,string todate)
{
字符串pdfFilePath=null;
Document doc=新文档(iTextSharp.text.PageSize.LETTER,10,10,42,35);
尝试
{
pdfFilePath=Server.MapPath(“~/Uploads/BANKformedited.pdf”);
//字符串pdfFilePath=Server.MapPath(“.”+“/pdf/myPdf.pdf”;
//创建文档类对象,并将其大小设置为字母,并在左、右、上、下边距中留出空间
PdfWriter wri=PdfWriter.GetInstance(doc,newfilestream(pdfFilePath,FileMode.Create));
doc.Open();//打开要写入的文档
Font font8=FontFactory.GetFont(“ARIAL”,7);
//写一些内容
段落=新段落(“星期一风险投资私人有限公司”);
//这用于将图像放置到P.D.F屏幕
//
//字符串clientLogo=Server.MapPath(“../Images/logo%20175%20x%2040.png”);
//字符串imageFilePath=Server.MapPath(“../Images/logo%20175%20x%2040.png”);
//iTextSharp.text.Image jpg=iTextSharp.text.Image.GetInstance(imageFilePath);
////根据需要调整图像大小
//jpg.ScaleToFit(80f、60f);
////在图像之前留出空间
//jpg.SpacingBefore=0f;
////在图像后面留出一些空间
//jpg.SpacingAfter=1f;
//jpg.Alignment=Element.HEADER;
//新增文件(jpg);
//DataTable dt=GetDataTable(员工编号、fromdate、todate);
DataTable dt=新的DataTable();
dt=GetDataTable(员工编号、fromdate、todate);
如果(dt!=null)
{
//创建pdf表格的实例,并设置该表格中的列数
PdfPTable PdfTable=新的PdfPTable(dt.Columns.Count);
PdfPCell PdfPCell=null;
//添加pdf表格的标题
PdfPCell=新PdfPCell(新短语(新区块(“周一风险投资私人有限公司”,font8));
PdfTable.AddCell(PdfPCell);
PdfPCell=新的PdfPCell(新短语(新语块(“ID”,font8));
PdfTable.AddCell(PdfPCell);
PdfPCell=新的PdfPCell(新短语(新语块(“日期”),font8));
PdfTable.AddCell(PdfPCell);
PdfPCell=新的PdfPCell(新短语(新语块(“EmployeeNo”,font8));
PdfTable.AddCell(PdfPCell);
PdfPCell=新的PdfPCell(新短语(新语块(“Head”),font8));
PdfTable.AddCell(PdfPCell);
PdfPCell=新的PdfPCell(新短语(新语块(“序列”),font8));
PdfTable.AddCell(PdfPCell);
PdfPCell=新的PdfPCell(新短语(新语块(“添加”,font8));
PdfTable.AddCell(PdfPCell);
PdfPCell=新的PdfPCell(新短语(新语块(“减法”,font8));
PdfTable.AddCell(PdfPCell);
//添加了日期为EmployeeNo的头部序列,并减去
//如何将数据表中的数据添加到pdf表中
for(int rows=0;rows
实际上,我得到了p.D.F表单,但这里我想使用itextsharper从数据库数据定制我自己的表单
提前感谢…我使用Html模板解决了这个问题。我设计了模板,并将模板与数据库数据绑定
public void Add(int employeenumber, string fromdate, string todate)
{
string pdfFilePath = null;
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
try
{
pdfFilePath = Server.MapPath("~/yourpath/");
//string pdfFilePath = Server.MapPath(".") + "/pdf/myPdf.pdf";
//Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));
//Open Document to write
doc.Open();
Font font8 = FontFactory.GetFont("ARIAL", 7);
var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD);
var subTitleFont = FontFactory.GetFont("Arial", 14, Font.BOLD);
var boldTableFont = FontFactory.GetFont("Arial", 12, Font.BOLD);
var endingMessageFont = FontFactory.GetFont("Arial", 10, Font.ITALIC);
var bodyFont = FontFactory.GetFont("Arial", 12, Font.NORMAL);
var imageFilePath = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/8.jpg"));
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
//Resize image depend upon your need
jpg.ScaleToFit(40f, 56f);
//Give space before image
jpg.SpacingBefore = 30f;
//Give some space after the image
jpg.SpacingAfter = 1f;
jpg.Alignment = Element.ALIGN_LEFT;
doc.Add(jpg);
//this is for get the month name and the year name frm the sql based on the passed date
var dateresult = new SalaryDAL().Getmonthyear(fromdate);
//getting the single property form the collection in c#//
string contents = System.IO.File.ReadAllText(Server.MapPath("~/templates/salarypdf.htm"));
var result = new SalaryDAL().GetSalaryReport(employeenumber, fromdate, todate);
contents = contents.Replace("${CompanyName}", Convert.ToString(result.GetType().GetProperty("CompanyName").GetValue(result, null)));
contents = contents.Replace("${month}", Convert.ToString(dateresult.GetType().GetProperty("Month").GetValue(dateresult, null)));
contents = contents.Replace("${year}", Convert.ToString(dateresult.GetType().GetProperty("Year").GetValue(dateresult, null)));
contents = contents.Replace("${EmployeeName}", Convert.ToString(result.GetType().GetProperty("EmployeeName").GetValue(result, null)));
contents = contents.Replace("${AccNo}", Convert.ToString(result.GetType().GetProperty("BankAccNo").GetValue(result, null)));
contents = contents.Replace("${empno}", Convert.ToString(result.GetType().GetProperty("employeeNo").GetValue(result, null)));
contents = contents.Replace("${dptname}", Convert.ToString(result.GetType().GetProperty("Department").GetValue(result, null)));
contents = contents.Replace("${bankname}", Convert.ToString(result.GetType().GetProperty("BankName").GetValue(result, null)));
contents = contents.Replace("${pfno}", Convert.ToString(result.GetType().GetProperty("PFNo").GetValue(result, null)));
contents = contents.Replace("${basic}", Convert.ToString(result.GetType().GetProperty("BasicAdd").GetValue(result, null)));
contents = contents.Replace("${basic1}", Convert.ToString(result.GetType().GetProperty("BasicSub").GetValue(result, null)));
contents = contents.Replace("${hra}", Convert.ToString(result.GetType().GetProperty("HRAAdd").GetValue(result, null)));
contents = contents.Replace("${hra1}", Convert.ToString(result.GetType().GetProperty("HRASub").GetValue(result, null)));
contents = contents.Replace("${lta}", Convert.ToString(result.GetType().GetProperty("LTAAdd").GetValue(result, null)));
contents = contents.Replace("${lta1}", Convert.ToString(result.GetType().GetProperty("LTASub").GetValue(result, null)));
contents = contents.Replace("${ta}", Convert.ToString(result.GetType().GetProperty("TAAdd").GetValue(result, null)));
contents = contents.Replace("${ta1}", Convert.ToString(result.GetType().GetProperty("LTASub").GetValue(result, null)));
contents = contents.Replace("${da}", Convert.ToString(result.GetType().GetProperty("DAAdd").GetValue(result, null)));
contents = contents.Replace("${da1}", Convert.ToString(result.GetType().GetProperty("DASub").GetValue(result, null)));
contents = contents.Replace("${bonus}", Convert.ToString(result.GetType().GetProperty("BonusAdd").GetValue(result, null)));
contents = contents.Replace("${bonus1}", Convert.ToString(result.GetType().GetProperty("BonusSub").GetValue(result, null)));
contents = contents.Replace("${pt}", Convert.ToString(result.GetType().GetProperty("PTAdd").GetValue(result, null)));
contents = contents.Replace("${pt1}", Convert.ToString(result.GetType().GetProperty("PTSub").GetValue(result, null)));
contents = contents.Replace("${pf}", Convert.ToString(result.GetType().GetProperty("PFAdd").GetValue(result, null)));
contents = contents.Replace("${pf1}", Convert.ToString(result.GetType().GetProperty("PFSub").GetValue(result, null)));
contents = contents.Replace("${other}", Convert.ToString(result.GetType().GetProperty("OtherAdd").GetValue(result, null)));
contents = contents.Replace("${other1}", Convert.ToString(result.GetType().GetProperty("OtherSub").GetValue(result, null)));
contents = contents.Replace("${ge}", Convert.ToString(result.GetType().GetProperty("GrossEarning").GetValue(result, null)));
contents = contents.Replace("${gd}", Convert.ToString(result.GetType().GetProperty("GrossDeduction").GetValue(result, null)));
contents = contents.Replace("${netslry}", Convert.ToString(result.GetType().GetProperty("NetSalary").GetValue(result, null)));
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
foreach (var htmlElement in parsedHtmlElements)
{
doc.Add(htmlElement as IElement);
}
}
catch (DocumentException docEx)
{
}
catch (IOException ioEx)
{
}
catch (Exception ex)
{
}
finally
{
doc.Close();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfFilePath + "");
Response.WriteFile(pdfFilePath);
Response.End();
}
}
你的确切要求是什么?。。无法理解你想问什么这可能对你有帮助。。嗯。。。它是“PDF”而不是“P.D.F”。这可能有用,我使用razor呈现标准html,然后将其转换为PDF。这样我就不必学习itext的造型。事实上,我是用不同的方法得到这个功能的……无论如何,谢谢。。。
public void Add(int employeenumber, string fromdate, string todate)
{
string pdfFilePath = null;
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
try
{
pdfFilePath = Server.MapPath("~/yourpath/");
//string pdfFilePath = Server.MapPath(".") + "/pdf/myPdf.pdf";
//Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));
//Open Document to write
doc.Open();
Font font8 = FontFactory.GetFont("ARIAL", 7);
var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD);
var subTitleFont = FontFactory.GetFont("Arial", 14, Font.BOLD);
var boldTableFont = FontFactory.GetFont("Arial", 12, Font.BOLD);
var endingMessageFont = FontFactory.GetFont("Arial", 10, Font.ITALIC);
var bodyFont = FontFactory.GetFont("Arial", 12, Font.NORMAL);
var imageFilePath = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/8.jpg"));
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
//Resize image depend upon your need
jpg.ScaleToFit(40f, 56f);
//Give space before image
jpg.SpacingBefore = 30f;
//Give some space after the image
jpg.SpacingAfter = 1f;
jpg.Alignment = Element.ALIGN_LEFT;
doc.Add(jpg);
//this is for get the month name and the year name frm the sql based on the passed date
var dateresult = new SalaryDAL().Getmonthyear(fromdate);
//getting the single property form the collection in c#//
string contents = System.IO.File.ReadAllText(Server.MapPath("~/templates/salarypdf.htm"));
var result = new SalaryDAL().GetSalaryReport(employeenumber, fromdate, todate);
contents = contents.Replace("${CompanyName}", Convert.ToString(result.GetType().GetProperty("CompanyName").GetValue(result, null)));
contents = contents.Replace("${month}", Convert.ToString(dateresult.GetType().GetProperty("Month").GetValue(dateresult, null)));
contents = contents.Replace("${year}", Convert.ToString(dateresult.GetType().GetProperty("Year").GetValue(dateresult, null)));
contents = contents.Replace("${EmployeeName}", Convert.ToString(result.GetType().GetProperty("EmployeeName").GetValue(result, null)));
contents = contents.Replace("${AccNo}", Convert.ToString(result.GetType().GetProperty("BankAccNo").GetValue(result, null)));
contents = contents.Replace("${empno}", Convert.ToString(result.GetType().GetProperty("employeeNo").GetValue(result, null)));
contents = contents.Replace("${dptname}", Convert.ToString(result.GetType().GetProperty("Department").GetValue(result, null)));
contents = contents.Replace("${bankname}", Convert.ToString(result.GetType().GetProperty("BankName").GetValue(result, null)));
contents = contents.Replace("${pfno}", Convert.ToString(result.GetType().GetProperty("PFNo").GetValue(result, null)));
contents = contents.Replace("${basic}", Convert.ToString(result.GetType().GetProperty("BasicAdd").GetValue(result, null)));
contents = contents.Replace("${basic1}", Convert.ToString(result.GetType().GetProperty("BasicSub").GetValue(result, null)));
contents = contents.Replace("${hra}", Convert.ToString(result.GetType().GetProperty("HRAAdd").GetValue(result, null)));
contents = contents.Replace("${hra1}", Convert.ToString(result.GetType().GetProperty("HRASub").GetValue(result, null)));
contents = contents.Replace("${lta}", Convert.ToString(result.GetType().GetProperty("LTAAdd").GetValue(result, null)));
contents = contents.Replace("${lta1}", Convert.ToString(result.GetType().GetProperty("LTASub").GetValue(result, null)));
contents = contents.Replace("${ta}", Convert.ToString(result.GetType().GetProperty("TAAdd").GetValue(result, null)));
contents = contents.Replace("${ta1}", Convert.ToString(result.GetType().GetProperty("LTASub").GetValue(result, null)));
contents = contents.Replace("${da}", Convert.ToString(result.GetType().GetProperty("DAAdd").GetValue(result, null)));
contents = contents.Replace("${da1}", Convert.ToString(result.GetType().GetProperty("DASub").GetValue(result, null)));
contents = contents.Replace("${bonus}", Convert.ToString(result.GetType().GetProperty("BonusAdd").GetValue(result, null)));
contents = contents.Replace("${bonus1}", Convert.ToString(result.GetType().GetProperty("BonusSub").GetValue(result, null)));
contents = contents.Replace("${pt}", Convert.ToString(result.GetType().GetProperty("PTAdd").GetValue(result, null)));
contents = contents.Replace("${pt1}", Convert.ToString(result.GetType().GetProperty("PTSub").GetValue(result, null)));
contents = contents.Replace("${pf}", Convert.ToString(result.GetType().GetProperty("PFAdd").GetValue(result, null)));
contents = contents.Replace("${pf1}", Convert.ToString(result.GetType().GetProperty("PFSub").GetValue(result, null)));
contents = contents.Replace("${other}", Convert.ToString(result.GetType().GetProperty("OtherAdd").GetValue(result, null)));
contents = contents.Replace("${other1}", Convert.ToString(result.GetType().GetProperty("OtherSub").GetValue(result, null)));
contents = contents.Replace("${ge}", Convert.ToString(result.GetType().GetProperty("GrossEarning").GetValue(result, null)));
contents = contents.Replace("${gd}", Convert.ToString(result.GetType().GetProperty("GrossDeduction").GetValue(result, null)));
contents = contents.Replace("${netslry}", Convert.ToString(result.GetType().GetProperty("NetSalary").GetValue(result, null)));
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
foreach (var htmlElement in parsedHtmlElements)
{
doc.Add(htmlElement as IElement);
}
}
catch (DocumentException docEx)
{
}
catch (IOException ioEx)
{
}
catch (Exception ex)
{
}
finally
{
doc.Close();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfFilePath + "");
Response.WriteFile(pdfFilePath);
Response.End();
}
}