输入字符串格式不正确时出错

时间:2011-04-29 06:24:30

标签: c# asp.net itextsharp

点击按钮input string was not in a correct format时,我收到以下错误,

点击按钮,我调用以下方法:

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

using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
/// <summary>
/// Summary description for pdfgeneration
/// </summary>
public class pdfgeneration
{
    public pdfgeneration()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public void pdfgenerator(String name1, AjaxControlToolkit.HTMLEditor.Editor Editor1)
    {

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/pdf";
        // Create PDF document
        Document pdfDocument = new Document(PageSize.A4, 70, 55, 40, 25);

        PdfWriter wri = PdfWriter.GetInstance(pdfDocument, new FileStream("e://" +name1 + ".pdf", FileMode.Create));

        PdfWriter.GetInstance(pdfDocument, HttpContext.Current.Response.OutputStream);

        pdfDocument.Open();
        string htmlText = Editor1.Content;
        System.Collections.Generic.List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null);

        for (int k = 0; k < htmlarraylist.Count; k++)
        {
            pdfDocument.Add((IElement)htmlarraylist[k]);
        }

        pdfDocument.Close();
        HttpContext.Current.Response.End();
    }

}

堆栈跟踪是:

[FormatException: Input string was not in a correct format.]
   System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7471335
   System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt) +115
   System.Single.Parse(String s, NumberStyles style, NumberFormatInfo info) +192
   iTextSharp.text.html.simpleparser.CellWrapper..ctor(String tag, ChainedProperties chain) +148
   iTextSharp.text.html.simpleparser.HTMLTagProcessor_TD.StartElement(HTMLWorker worker, String tag, IDictionary`2 attrs) +84
   iTextSharp.text.html.simpleparser.HTMLWorker.StartElement(String tag, Dictionary`2 attrs) +79
   iTextSharp.text.xml.simpleparser.SimpleXMLParser.ProcessTag(Boolean start) +30
   iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader) +1008
   iTextSharp.text.xml.simpleparser.SimpleXMLParser.Parse(ISimpleXMLDocHandler doc, ISimpleXMLDocHandlerComment comment, TextReader r, Boolean html) +48
   iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style, IDictionary`2 tags, Dictionary`2 providers) +94
   iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style) +9
   pdfgeneration.pdfgenerator(String name1, Editor Editor1) in C:\inetpub\wwwroot\dcis\App_Code\pdfgeneration.cs:37
   EntryForm.Button4_Click(Object sender, EventArgs e) in C:\inetpub\wwwroot\dcis\EntryForm.aspx.cs:224
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

如何解决此错误?

@ geek在他发布的代码中出现错误

enter image description here

5 个答案:

答案 0 :(得分:9)

我面临同样的错误,“输入字符串的格式不正确。”,我查看我的html字符串,发现如果我在样式标签之外写表宽度,我得到这个错误, 例如,: - 在htmlWorker.Parse()方法中给出错误。

当我在样式标签中放置宽度标签时,我解决了这个错误, 例如,

我希望,这对你有所帮助。

For eg, <table width="610px"> </table> :- Give error at htmlWorker.Parse() method.

当我在样式标记中放置宽度标记时,我解决了这个错误,

For eg, <table style="width:610px"> </table>

我希望,这对你有所帮助。

答案 1 :(得分:3)

你可以先把它缩小 pdfgenerator内的 ...为那个dll启用构建符号将是一个开始,但即使是一些简单的跟踪,你也可以知道它到底在哪里爆炸时会有所帮助。

最终PdfWriter不是核心.NET,因此您必须帮助我们缩小范围。

甚至更简单:点击“开始调试”,并在该方法上设置一个断点;现在单步执行并查看:爆炸的位置,以及b:此时键值是什么。

答案 2 :(得分:2)

看起来你有一个非数字样式值,其中iTextSharp期待一个数字。 “font-size:normal”或者类似的东西。

CellWrapper(String, ChainedProperties)正在查看HtmlTags.WIDTH。这是iTextSharp 5.0.6的源代码:

    public CellWrapper(String tag, ChainedProperties chain) {
        this.cell = CreatePdfPCell(tag, chain);
        String value = chain[HtmlTags.WIDTH];
        if (value != null) {
            value = value.Trim();
            if (value.EndsWith("%")) {
                percentage = true;
                value = value.Substring(0, value.Length - 1);
            }
            width = float.Parse(value, CultureInfo.InvariantCulture);
        }
    }

看起来非常像问题在于float.Parse()调用。看起来这段代码除了'%'或秃头数字之外什么都不能处理。如果你的宽度是以'cm','px'或其他形式定义的,那很可能就是问题所在。

使用来源!

PS:你用的是什么版本? IIRC,iText已经发布调试信息很长一段时间了。如果所有其他方法都失败了,那就自己构建一个调试版本。

答案 3 :(得分:2)

我遇到了同样的问题,我找到了另一种解决方案。

尝试使用&#34; px&#34;解析大小时发生错误。部分。要解决它,只需替换HTML字符串&#34; px&#34;出现&#34;&#34;。它仍然知道它是像素。

希望它能适用于您的情况!

答案 4 :(得分:0)

试试这个

public void CreatePDFDocument(string strHtml)
    {

        string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
        // step 1: creation of a document-object
        Document document = new Document();
        // step 2:
        // we create a writer that listens to the document
        PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
        StringReader se = new StringReader(strHtml);
        HTMLWorker obj = new HTMLWorker(document);
        document.Open();
        obj.Parse(se);
        document.Close();
        ShowPdf(strFileName);



    }







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