itextsharp字体太小:0错误

时间:2012-04-21 14:15:07

标签: c# asp.net html itextsharp

我正在尝试从html内容创建一个pdf文件。然而,有一件事阻止它字体太小:0 错误当ı点击按钮时        hw.Parse(new StringReader(HTML));线。

请帮助我如何解决这个问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

 namespace PTCreportingProject.PDF
{  
public class CreatePDFReport
{
    private string htmlContent = "";

    public string createReport()
    {
        string res = "";
        res = prepareHeader();
            return res;
    }
    private string prepareHeader()
    {
        string hed = "";
        System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
        sbHtml.Append("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
        sbHtml.Append("<html xmlns='http://www.w3.org/1999/xhtml'>");
        sbHtml.Append("<head><title>");
        sbHtml.Append("</title>");
        sbHtml.Append(" <style type='text/css'>");
        sbHtml.Append("  .style16 { width: 80%;  margin-left: auto; margin-right: auto; border-color: red; }");
        sbHtml.Append(".style19  { border-color: red; }");
        sbHtml.Append(".style30 {  width: 35px;  border-color: red;  }");
        sbHtml.Append("  .style31 { border-color: red;  }");
        sbHtml.Append(".style41 { border-color: red; }");
        sbHtml.Append(" .style55  { height: 23px;  border-color: red; }");
        sbHtml.Append(".style56  { border-color: red; }");
        sbHtml.Append(".style59  { border-color: red; }");
        sbHtml.Append(" .style60  {  width: 66px; border-color: red; }");
        sbHtml.Append(".style62 {  height: 27px; border-color: red; }");
        sbHtml.Append(" .style63  {  height: 26px; border-color: red; }");
        sbHtml.Append(".style95 { width: 92px; border-color: red;}");
        sbHtml.Append(".style100 {  width: 123px;  border-color: red; }");
        sbHtml.Append(".style101{ width: 195px; border-color: red; }");
        sbHtml.Append(" .style102 { width: 192px;  border-color: red; }");
        sbHtml.Append(".style103 { border-color: red; }");
        sbHtml.Append(" .style104 { border-color: red; }");
        sbHtml.Append(" .style106 {  width: 113px; height: 7px;  border-color: red; }");
        sbHtml.Append("  .style107 { width: 169px; border-right: #ff0000 1pt solid; border-left-color: red; border-top-color: red; border-bottom-color: red; }");
        sbHtml.Append("  .style108 { width: 113px;  height: 12px; border-color: red; }");
        sbHtml.Append("  .style109 { width: 366px; height: 12px; border-color: red; }");
        sbHtml.Append(" .style110 { height: 7px; border-color: red; }");
        sbHtml.Append("   .style111 { width: 77px; border-color: red; }");
        sbHtml.Append("   .style112 { width: 506px; border-color: red; }");
        sbHtml.Append("   .style114 { width: 87px; border-color: red; }");
        sbHtml.Append(" .style116 { width: 82px; border-color: red; }");
        sbHtml.Append(" .style117 { width: 137px; border-color: red; }");
        sbHtml.Append(" .style150 { width: 83px; border-color: red; }");
        sbHtml.Append(" .style161 {  width: 165px; border-color: red; }");
        sbHtml.Append(".style167 { width: 138px; border-color: red; }");
        sbHtml.Append("  .style177 { width: 162px; border-color: red; }");
        sbHtml.Append(" .style181 { width: 157px; border-color: red; }");
        sbHtml.Append(" .style185 { width: 180px; border-color: red; }");
        sbHtml.Append(".style189 { width: 35px; border-color: red; }");
        sbHtml.Append(" .style190 { width: 100px;  border-color: red; }");
        sbHtml.Append(".style191 { width: 91px; border-color: red; }");
        sbHtml.Append(" .style192 { width: 35px; border-color: red; }");
        sbHtml.Append(" </style>");
        sbHtml.Append("</head>");
        sbHtml.Append("<body>");
        sbHtml.Append("<form method='post' action='EngineeringChangeNoticeReport.aspx' id='form1'>");
        sbHtml.Append(" <table width='80%' align='center'>");
        sbHtml.Append(" <tr>");
        sbHtml.Append("<td>");
        //sbHtml.Append("<img id='Image1' src='images/mikes.png' style='height:51px;width:163px;' />");
        sbHtml.Append(" </td>");
        sbHtml.Append("<td>");
        sbHtml.Append(" <h2 >");
        sbHtml.Append("<span id='Label29' style='color:Red;'>ENGINEERING CHANGE NOTICE</span>");
        sbHtml.Append(" </h2>");
        sbHtml.Append("</td>");
        sbHtml.Append("</tr>");
        sbHtml.Append(" </table>");
        sbHtml.Append(" <table class='style16' border='1' cellpadding = '0' cellspacing ='0'>");
        sbHtml.Append("<tr>");
        sbHtml.Append("<td class='style108'>");
        sbHtml.Append("  <span id='Label1' style='color:Red;'>Class of Change</span>");
        sbHtml.Append(" </td>");
        sbHtml.Append(" <td class='style109'>");
        sbHtml.Append(" <span id='Label2' style='color:Red;'>Priority</span>");
        sbHtml.Append(" </td>");
        sbHtml.Append(" <td class='style104' rowspan='2' colspan='2'>  <span id='Label3' style='color:Red;font-size:XX-Large;font-weight:bold;'>ECN NO:</span> <span id='Label48' style='font-size:X-Large;'>aaa</span>  </td>");
        sbHtml.Append(" </tr>");
        sbHtml.Append(" <tr>");
        sbHtml.Append("<td class='style108'> <input id='CheckBox1' type='checkbox' name='CheckBox1' checked='checked' /><label for='CheckBox1'>I</label>  </td>");
        sbHtml.Append(" <td class='style109'> <span id='Label50'></span>  </td>");
        sbHtml.Append("</tr>");
        sbHtml.Append(" <tr> <td class='style106'>  <input id='CheckBox2' type='checkbox' name='CheckBox2' /><label for='CheckBox2'>II</label> </td>  <td class='style110' colspan='3'>  &nbsp;</td>  </tr>");
        sbHtml.Append(" <tr> <td class='style107' colspan='3'> <span id='Label4' style='color:Red;'>Project Code/Name:</span>  <span id='Label51'>aaa</span> </td>  <td class='style19'>  <span id='Label5' style='color:Red;'>Releted ECRs</span>  <span id='Label53'>aaa</span> </td> </tr>");
        sbHtml.Append(" <tr> <td class='style107' colspan='3'> <span id='Label6' style='color:Red;'>Requested by:</span> <span id='Label52'>aaa</span> </td> <td class='style19'> <span id='Label7' style='color:Red;'>Date:</span> <span id='Label54'>03.04.2012</span> </td> </tr>");
        sbHtml.Append("</table>");


        sbHtml.Append(" <table class='style16' border='1' cellpadding = '0' cellspacing ='0'>");
        sbHtml.Append(" <tr> <td class='style55' colspan='6'> <span id='Label14' style='color:Red;'>Material / Affected Item</span> </td> </tr>");
        sbHtml.Append("  <tr> <td class='style114'> <span id='Label15' style='color:Red;'>PART NO</span> </td>");
        sbHtml.Append(" <td class='style150'> <span id='Label16' style='color:Red;'>on Order</span> </td>");
        sbHtml.Append(" <td class='style116'> <span id='Label17' style='color:Red;'>in Process</span> </td>");
        sbHtml.Append(" <td class='style111'> <span id='Label18' style='color:Red;'>in Stock</span> </td>");
        sbHtml.Append(" <td class='style117'> <span id='Label19' style='color:Red;'>in Next Higher Assy</span> </td>");
        sbHtml.Append(" <td class ='style56'> <span id='Label20' style='color:Red;'>Customer Accepted</span> </td> </tr>");
        sbHtml.Append(" <tr> <td class='style114'> aaa &nbsp;</td>");
        sbHtml.Append(" <td class='style150'> aaa &nbsp;</td>");
        sbHtml.Append(" <td class='style116'> aaa &nbsp;</td>");
        sbHtml.Append(" <td class='style111'> aaa &nbsp;</td>");
        sbHtml.Append("<td class='style117'> aaa &nbsp;</td>");
        sbHtml.Append(" <td class='style30'> aaa &nbsp;</td> </tr>");
        sbHtml.Append(" </table>");
        sbHtml.Append(" <table class='style16' border='1' cellpadding = '0' cellspacing ='0'>");
        sbHtml.Append(" <tr> <td class='style41' colspan='7'> <span id='Label21' style='color:Red;'>Affected End Item/Seial Item</span> </td> </tr>");
        sbHtml.Append("<tr> <td class='style161'> <span id='Label22' style='color:Red;'>Part No</span> </td>");
        sbHtml.Append(" <td class='style177'> <span id='Label23' style='color:Red;'>Conform</span> </td>");
        sbHtml.Append(" <td class='style181'> <span id='Label24' style='color:Red;'>Rework</span> </td>");
        sbHtml.Append(" <td class='style185'> <span id='Label25' style='color:Red;'>Use</span> </td>");
        sbHtml.Append(" <td class='style167'> <span id='Label26' style='color:Red;'>Retrofit</span> </td>");
        sbHtml.Append("<td class='style189' align='center'> <span id='Label27' style='color:Red;'>Old CS</span> </td>");
        sbHtml.Append(" <td class='style189' align='center'> <span id='Label28' style='color:Red;'>New CS</span> </td> </tr>");
        sbHtml.Append(" <tr> <td class='style161'> aaa &nbsp;</td>");
        sbHtml.Append("<td class='style177'> aaa &nbsp;</td>");
        sbHtml.Append(" <td class='style181'> aaa &nbsp;</td>");
        sbHtml.Append("<td class='style185'> aaa &nbsp;</td>");
        sbHtml.Append("<td class='style167'> aaa &nbsp;</td>");
        sbHtml.Append(" <td class='style189'> aaa &nbsp;</td>");
        sbHtml.Append(" <td class='style189'> aaa &nbsp;</td> </tr>");
        sbHtml.Append("</table>");
        sbHtml.Append(" <table class='style16' border='1' cellpadding = '0' cellspacing ='0'>");
        sbHtml.Append(" <tr> <td class='style31' colspan='5'> <span id='Label8' style='color:Red;'>Documents</span> </td> </tr>");
        sbHtml.Append("<tr> <td class='style191'> <span id='Label9' style='color:Red;'>Prefix</span> </td>");
        sbHtml.Append(" <td class='style190'> <span id='Label10' style='color:Red;'>Number</span> </td>");
        sbHtml.Append(" <td class='style112'> <span id='Label11' style='color:Red;'>Description</span> </td>");
        sbHtml.Append("<td class='style192' align='center'> <span id='Label12' style='color:Red;'>Old Rev</span> </td>");
        sbHtml.Append(" <td class='style30' align='center'> <span id='Label13' style='color:Red;'>New Rev</span> </td> </tr>");
        sbHtml.Append("  <tr> <td class='style191'> aaa &nbsp;</td>");
        sbHtml.Append("<td class='style190'> aaa &nbsp;</td>");
        sbHtml.Append("  <td class='style112'> aaa &nbsp;</td>");
        sbHtml.Append(" <td class='style192'> aaa &nbsp;</td>");
        sbHtml.Append(" <td class='style30'> aaa &nbsp;</td> </tr>");
        sbHtml.Append("</table>");
        sbHtml.Append(" <table class='style16' border='1' cellpadding = '0' cellspacing ='0'>");
        sbHtml.Append(" <tr> <td class ='style56'> <span id='Label30' style='color:Red;'>Change Description</span> <span id='Label55'>aaa</span> </td> </tr> </table>");
        sbHtml.Append(" <table border='1' class='style16' cellpadding = '0' cellspacing ='0'>");
        sbHtml.Append("<tr> <td class='style59' colspan='6'> <span id='Label31' style='color:Red;'>Classification of Reason for Change</span> </td> </tr>");
        sbHtml.Append("<tr> <td class='style60'> &nbsp;</td> <td class='style95' align='center'> <span id='Label35' style='color:Red;'>Document</span> </td> <td class='style116' align='center'> <span id='Label36' style='color:Red;'>Design</span> </td> <td class='style95' align='center'> <span id='Label37' style='color:Red;'>Production</span> </td> <td class='style116' align='center'> <span id='Label39' style='color:Red;'>Purchasing</span> </td> <td class='style100' align='center'> <span id='Label40' style='color:Red;'>Customer Request</span> </td> </tr>");
        sbHtml.Append(" <tr> <td class='style60'> <span id='Label32' style='color:Red;'>Improvement</span> </td> <td class='style95' align='center'> <input id='CheckBox24' type='checkbox' name='CheckBox24' checked='checked' /> </td> <td class='style116' align='center'> <input id='CheckBox25' type='checkbox' name='CheckBox25' /> </td> <td class='style95' align='center'> <input id='CheckBox26' type='checkbox' name='CheckBox26' /> </td> <td class='style116' align='center'> <input id='CheckBox28' type='checkbox' name='CheckBox28' /> </td> <td class='style100' align='center' <input id='CheckBox29' type='checkbox' name='CheckBox29' /> </td> </tr>");
        sbHtml.Append("  <tr> <td class='style60'> <span id='Label33' style='color:Red;'>Error</span> </td> <td class='style95' align='center'> <input id='CheckBox31' type='checkbox' name='CheckBox31' /> </td> <td class='style116' align='center'> <input id='CheckBox32' type='checkbox' name='CheckBox32' checked='checked' /> </td> <td class='style95' align='center'> <input id='CheckBox33' type='checkbox' name='CheckBox33' checked='checked' /> </td> <td class='style116' align='center'> <input id='CheckBox35' type='checkbox' name='CheckBox35' /> </td> <td class='style100' align='center'> <input id='CheckBox36' type='checkbox' name='CheckBox36' checked='checked' /> </td> </tr>");
        sbHtml.Append(" <tr> <td class='style60'> <span id='Label34' style='color:Red;'>Requirement</span> </td> <td class='style95' align='center'> <input id='CheckBox38' type='checkbox' name='CheckBox38' /> </td> <td class='style116' align='center'> <input id='CheckBox39' type='checkbox' name='CheckBox39' /> </td> <td class='style95' align='center'> <input id='CheckBox40' type='checkbox' name='CheckBox40' /> </td>  <td class='style116' align='center'> <input id='CheckBox42' type='checkbox' name='CheckBox42' checked='checked' /> </td> <td class='style100' align='center'> <input id='CheckBox43' type='checkbox' name='CheckBox43' /> </td>  </tr>");
        sbHtml.Append("</table>");
        sbHtml.Append(" <table border='1' class='style16' cellpadding = '0' cellspacing ='0'> <tr>");
        sbHtml.Append("  <td class ='style56'> <span id='Label42' style='color:Red;'>Explanition of Reson for Change</span> <span id='Label56'>aaa</span> <br /> </td> </tr>");
        sbHtml.Append(" </table>");
        sbHtml.Append(" <table border='1' class='style16' cellpadding = '0' cellspacing ='0'>");
        sbHtml.Append(" <tr> <td colspan='4' class ='style56'> <span id='Label43' style='color:Red;'>Configuration Baseline Affected</span> </td> </tr>");
        sbHtml.Append("  <tr> <td class ='style56'> <input id='CheckBox6' type='checkbox' name='CheckBox6' checked='checked' /><label for='CheckBox6'>No</label> </td> <td class='style101'> <input id='CheckBox7' type='checkbox' name='CheckBox7' checked='checked' /><label for='CheckBox7'>Functional</label> </td> <td class='style102'> <input id='CheckBox8' type='checkbox' name='CheckBox8' /><label for='CheckBox8'>Allocated</label> </td> <td class ='style56'> <input id='CheckBox9' type='checkbox' name='CheckBox9' /><label for='CheckBox9'>Product</label> </td> </tr>");
        sbHtml.Append("   </table>");
        sbHtml.Append(" <table border='1' class='style16' cellpadding = '0' cellspacing ='0'>");
        sbHtml.Append("  <tr> <td class ='style56'> <span id='Label44' style='color:Red;'>BOM Affected</span> </td> </tr>");
        sbHtml.Append(" <tr> <td class='style103'> <input id='CheckBox10' type='checkbox' name='CheckBox10' checked='checked' /><label for='CheckBox10'>Yes</label> <input id='CheckBox11' type='checkbox' name='CheckBox11' /><label for='CheckBox11'>No</label> </td> </tr>");
        sbHtml.Append("</table>");
        sbHtml.Append("<table border='1' class='style16' cellpadding = '0' cellspacing ='0'>");
        sbHtml.Append("<tr> <td class ='style56'> <span id='Label45' style='color:Red;'>Evaluation</span> </td> </tr>");
        sbHtml.Append("<tr> <td class ='style56'> <input id='CheckBox12' type='checkbox' name='CheckBox12' checked='checked' /><label for='CheckBox12'>Interchangeability effected</label> </td> </tr>");
        sbHtml.Append(" <tr> <td class ='style56'> <input id='CheckBox13' type='checkbox' name='CheckBox13' /><label for='CheckBox13'>Affects test/assembly tools</label> </td> </tr>");
        sbHtml.Append(" <tr> <td class ='style56'> <input id='CheckBox14' type='checkbox' name='CheckBox14' /><label for='CheckBox14'>Affects the articles of contract</label> </td > </tr>");
        sbHtml.Append(" <tr> <td class ='style56'> <input id='CheckBox15' type='checkbox' name='CheckBox15' /><label for='CheckBox15'>Affects the contract cost</label> </td> </tr>");
        sbHtml.Append(" <tr> <td class ='style56'> <input id='CheckBox16' type='checkbox' name='CheckBox16' /><label for='CheckBox16'>Affects the maintenace</label> </td> </tr>");
        sbHtml.Append("<tr> <td class ='style56'> <input id='CheckBox17' type='checkbox' name='CheckBox17' checked='checked' /><label for='CheckBox17'>Affects the software</label> </td> </tr>");
        sbHtml.Append("  <tr> <td class ='style56'> <input id='CheckBox18' type='checkbox' name='CheckBox18' /><label for='CheckBox18'>Affects the test software</label> </td> </tr>");
        sbHtml.Append("<tr> <td class ='style56'> <input id='CheckBox20' type='checkbox' name='CheckBox20' /><label for='CheckBox20'>Affects the performance, safety, security</label> </td> </tr>");
        sbHtml.Append(" <tr> <td class ='style56'> <input id='CheckBox22' type='checkbox' name='CheckBox22' /><label for='CheckBox22'>Affects reference documents (explain)</label> </td> </tr>");
        sbHtml.Append(" <tr> <td class ='style56'> <input id='CheckBox23' type='checkbox' name='CheckBox23' /><label for='CheckBox23'>Others (explain)</label> </td> </tr>");
        sbHtml.Append(" <tr> <td class='style62'> <span id='Label46' style='color:Red;'>Attachments</span> <span id='Label57'>aaa</span> </td> </tr>");
        sbHtml.Append("  <tr> <td class='style63'> <span id='Label47' style='color:Red;'>Comments</span> <span id='Label58'>aaa</span> </td> </tr>");
        sbHtml.Append("</table></form></body></html>");

        hed = sbHtml.ToString();

        return hed;
    }




}
}


    protected void Button1_Click(object sender, EventArgs e)
    {
        PDF.CreatePDFReport pdf = new PDF.CreatePDFReport();
        string reportContent = pdf.createReport();
       string pathFile = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)  + "\\engineeringChangeNoticeReport1" + ".pdf";

       HTMLToPdf(reportContent, pathFile);
    }
    public void HTMLToPdf(string HTML, string FilePath)
    {
        Document document = new Document();

        PdfWriter.GetInstance(document, new FileStream(FilePath, FileMode.Create));
        document.Open();
        //Image pdfImage = Image.GetInstance(Server.MapPath("Penguins.jpg"));

        //pdfImage.ScaleToFit(100, 50);

        //pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING; pdfImage.SetAbsolutePosition(180, 760);

        //document.Add(pdfImage);
        iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
        hw.Parse(new StringReader(HTML));
        document.Close();
        ShowPdf(FilePath);
    }


    private void ShowPdf(string s)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "inline;filename=" + s);
        Response.ContentType = "application/pdf";
        Response.WriteFile(s);
        Response.Flush();
        Response.Clear();
    }

2 个答案:

答案 0 :(得分:4)

iText [夏普] HTMLWorker不支持HTML中至少有一行正在使用的CSS absolute-size values,并导致异常:

<span id='Label3' style='color:Red;font-size:xx-Large;font-weight:bold;'>ECN NO:</span> <span id='Label48' style='font-size:x-Large;'>aaa</span>

删除font-size:xx-Large之类的样式,你应该没问题。

如果您愿意使用新的iText [Sharp] XML / HTML解析器,那么<​​strong>可以使用CSS绝对大小值 - 类似这样的工作与XML Worker版本1.1.3.0:< / p>

StringReader stringReader = new StringReader(@"
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head><title></title></head>
<body>
<span id='Label3' style='color:Red;font-size:XX-Large;font-weight:bold;'>ECN NO:</span>
<span id='Label48' style='font-size:X-Large;'>aaa</span>
</body></html>
");
using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(document, STREAM);
  document.Open();
  XMLWorkerHelper.GetInstance().ParseXHtml(
    writer, document, stringReader
  );
}

答案 1 :(得分:0)

在某些情况下,我使用了 style =“ font-size:large;” 会导致错误。 将其更改为 style =“ font-size:18px;” 解决了该问题。