错误的HTML文本转换为PDF文件在asp.net中使用iTextSharp的 [英] Error converting HTML text to a pdf file using itextsharp in asp.net

查看:123
本文介绍了错误的HTML文本转换为PDF文件在asp.net中使用iTextSharp的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code到内容转换成编辑器(阿贾克斯控制),PDF,

I am using the following code to convert contents in Editor(Ajax control) to pdf,

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


}

我最初硬编码在编辑器(阿贾克斯控制)以下的HTML文本,

I am initially hard coding the following HTML text in the Editor(Ajax control),

String editorcontent = "<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>" +
                 "<br/>" + "<P align='center'><b>" + courtname + "</b></P>"
                + "<br/><P align='center'>(Before" + jname + "," + desname + ")"
                + "<br/>" + "<P align='right'><b><u>" + DropDownList1.SelectedItem + " no. " + TextBox1.Text + "/" + TextBox2.Text + "</u></b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</P>"
                + "<br />"
  **//this table is the problem//**    + "<table><tr><td width='750px'><p align='left'>" + petitioner + "</p>" + "</td><td>" + "<p align='right'>" + "..Applicant" + "</p>" + "</td></tr>" + "<tr><td><p align='center'>" + "V/s" + "</p>" + "</td><td></td>" + "</tr>" + "<tr><td width='75px'><p align='left'>" + respondent + "</p>" + "</td><td>" + "<p align='right'>" + "..Respondent" + "</p>" + "</td></tr></table>"
                + "<br/><P align='center'><b><u>ORDER</u></b>";
            Editor1.Content = editorcontent;

如果我从上面的code删除以下比PDF成功生成。但一旦我包括在我的HTML code我得到以下错误

If i remove the following table from the above code than the pdf is generated successfully. but once i include that table in my HTMl code i get the following Error

如何解决我的问题。

推荐答案

iTextSharp的当前版本做的不可以支持细胞与PX指定的宽度。它的确实支持百分比或数字,仅宽度单元宽度。

the current version of iTextSharp does not support cell width specified with 'px'. it does support percentage or numeric-only width cell widths.

当您尝试硬编码的HTML内容,有PX在两个地方:

when you tried hard-coding the HTML content, there was 'px' in two places:


  1. &LT; TD宽度='750px'&GT;

  2. &LT; TD宽度='960x75像素'&GT;

  1. <td width='750px'>
  2. <td width='75px'>

删除PX,你会好起来的。

remove 'px' and you'll be OK.

这篇关于错误的HTML文本转换为PDF文件在asp.net中使用iTextSharp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆