将ASP UserControl和HTML Control添加到PdfPCell时遇到的ITextSharp PDf问题

本文关键字:遇到 ITextSharp 问题 PDf PdfPCell UserControl ASP HTML 添加 Control | 更新日期: 2023-09-27 18:34:37

下面是我的代码,

。.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.同样,我有我的用户控件并希望将其添加到另一个 PDFPCell.我尝试使用 PDFP。ADDElement((,但没有成功。

将ASP UserControl和HTML Control添加到PdfPCell时遇到的ITextSharp PDf问题

您还可以

从标记动态创建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();
    }

希望这对你有帮助..!

更新:

iTextSharp填写PDF Forms

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();
  }