以下是我的代码,
的.aspx:
<table border="0" width="100%" id="maintable" runat="server">
<tr>
<td style="width:90%;vertical-align:top;">
<div id="content_left" runat="server" style="margin-right:10px;
float:left;vertical-align:top;width:690px" ></div>
</td>
<td style="vertical-align:top;background-color:#eee;width:100px;">
<div id="content_right" runat="server" style="background-color:#eee;float:right;
width:300px;"></div>
</td>
</tr>
</table>
.aspx.cs:
HtmlGenericControl aboutme_content = new HtmlGenericControl("div");
aboutme.ID = "aboutme1";
aboutme.Text= "ABOUT ME";
content_left.Controls.Add(aboutme_content);
PdfPTable table_left = new PdfPTable(1);
PdfPCell cell_1 = new PdfPCell();
现在,我怀疑的是我将如何将content_left添加到PdfPCell。同样,我有我的usercontrol并希望将其添加到另一个PDFPCell.I尝试使用PDFP.ADDElement(),但它没有成功。< / p>
答案 0 :(得分:0)
您还可以动态地从标记创建PDF
。
请检查以下示例:
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
protected void HTMLToPDF()
{
string strHTML = @"<div>Some html markup & text </div>";
String htmlText = strHTML.ToString();
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "MyTestPDF.pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.HTMLWorker hw =
new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(htmlText));
document.Close();
}
希望这对你有帮助..!
<强>更新强>
使用PDF Forms
填充iTextSharp
:
private void fillPdf() {
Response.ContentType = "application/pdf";
Response.AppendHeader(
"Content-Disposition",
"attachment;filename=itext_fill.pdf"
);
PdfReader r = new PdfReader(
new RandomAccessFileOrArray(Request.MapPath("test_fill.pdf")), null
);
using (PdfStamper ps = new PdfStamper(r, Response.OutputStream)) {
AcroFields af = ps.AcroFields;
af.SetField("lname", lname.Text);
af.SetField("fname", fname.Text);
af.SetField("address", address.Text);
af.SetField("zip", zip.Text);
ps.FormFlattening = true;
}
Response.End();
}