1

我正在使用 iTextSharp 5.3.0.0、XmlWorker 1.1.4.0 和 Asp.Net C# 3.5 从带有 ListView 和其他基本控件的 UserControl 呈现的 HTML 生成 PDF。

我已经有一个 PDF 文件,但它只呈现第一页,即使内容需要更多。

有什么建议么?

public void GeneratePdf()
{
    ////reset response
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ContentType = "application/pdf";

    ////define pdf filename
    string strFileName = "newFile.pdf";
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);

    //get html
    string outXml = [A HTML from a UserControl];

    //Generate PDF
    using (Document document = new Document(PageSize.A4, 30, 30, 30, 30))
    {
        //define output control HTML
        MemoryStream memStream = new MemoryStream();
        TextReader xmlString = new StringReader(outXml);

        PdfWriter writer = PdfWriter.GetInstance(document, memStream);

        //open doc
        document.Open();

        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
        htmlContext.SetImageProvider(new ImageProvider());

        ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
        cssResolver.AddCssFile(Server.MapPath("~/css/style.css"), true);

        IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
        XMLWorker worker = new XMLWorker(pipeline, true);
        XMLParser xmlParse = new XMLParser(true, worker);
        xmlParse.Parse(xmlString);
        xmlParse.Flush();

        document.Close();
        document.Dispose();

        HttpContext.Current.Response.BinaryWrite(memStream.ToArray());
    }

    HttpContext.Current.Response.End();
    HttpContext.Current.Response.Flush();
}

/// <summary>
/// Custom ImageProvider
/// </summary>
public class ImageProvider : AbstractImageProvider
{
    public ImageProvider() { }
    public override string GetImageRootPath()
    {
        return "/";
    }
}
4

0 回答 0