错误,因为输入字符串的格式不正确

发布于 2024-11-03 17:22:34 字数 3718 浏览 0 评论 0原文

单击按钮时出现以下错误 输入字符串的格式不正确

单击按钮时 出现以下错误我正在调用以下方法:

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

如何解决此错误?

@极客,他发布的代码中有错误

在此处输入图像描述

I am getting the following error on click of a button input string was not in a correct format,

On click of a button i am calling the following method:

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

}

the stack trace is:

[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

How can I resolve this error?

@ geek for error in the code he has posted

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

那伤。 2024-11-10 17:22:34

我遇到了同样的错误,“输入字符串的格式不正确。”,我检查我的 html 字符串,发现如果我在样式标记之外写入表格宽度,我会收到此错误,
例如,:- 在 htmlWorker.Parse() 方法中给出错误。

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

我希望这会对您有所帮助。

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

当我将宽度标签放入样式标签中时,我解决了这个错误,

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

我希望这会对您有所帮助。

I have facing same error, "Input string was not in a correct format.", i check my html string and found that, if i write table width outside of style tag, i get this error,
For eg, :- Give error at htmlWorker.Parse() method.

when i put width tag in style tag, i resolve this error,
For eg,

I hope, this will help you little bit.

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

when i put width tag in style tag, i resolve this error,

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

I hope, this will help you little bit.

荒人说梦 2024-11-10 17:22:34

您可以首先缩小范围 pdfgenerator...为该 dll 启用构建符号将是一个开始,但甚至可以进行一些简单的跟踪,以便您可以知道它的位置到它爆炸的时候会有帮助。

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

或者更简单:点击“开始调试”,并在该方法上放置一个断点;现在逐步查看 a:它爆炸的位置,b:该点的关键值是什么。

You could start by narrowing it down within pdfgenerator... Enabling build symbols for that dll would be a start, but even some simple tracing so that you can tell where it got to when it exploded would help.

Ultimately PdfWriter isn't core .NET, so you will have to help us narrow it down.

Or even simpler: hit "Start Debugging", and put a break-point on that method; now step through and see a: where it explodes, and b: what the key values are at that point.

ゞ花落谁相伴 2024-11-10 17:22:34

看起来您有一个非数字样式值,而 iTextSharp 需要一个数字。 “字体大小:正常”或类似的东西。

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 附带调试信息已经有一段时间了。如果一切都失败了,就自己构建一个调试版本。

This looks like you've got a non-numeric style value where iTextSharp is expecting a number. "font-size:normal" or something like that.

CellWrapper(String, ChainedProperties) is looking at the HtmlTags.WIDTH. Here's the source from 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);
        }
    }

It looks an awful lot like the problem is in the float.Parse() call. It looks like this code can't handle anything but '%' or a bald number. If your width is defined in 'cm', 'px', or whatever, that may well be the problem.

Use the Source!

PS: What version are you using? IIRC, iText has been shipping with debug info for quite some time. If all else fails, just build a debug version yourself.

阪姬 2024-11-10 17:22:34

我遇到了和你一样的问题,我找到了另一个解决方案。

当它尝试解析带有“p​​x”部分的尺寸时,会发生该错误。要解决这个问题,只需将出现的 HTML 字符串“px”替换为“”即可。它仍然知道它是像素。

希望它适用于您的情况!

I was having the same problem you had and I found another solution.

That error occurs when it tries to parse a size with the "px" part. To solve it, just replace the HTML string "px" occurences for "". It still knows that it is pixels.

Hope it works on your case!

南风起 2024-11-10 17:22:34

试试这个

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

try this

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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文