【问题标题】:Running out the PDF page while generating PDF using iTextSharp.dll使用 iTextSharp.dll 生成 PDF 时耗尽 PDF 页面
【发布时间】:2012-12-04 10:49:56
【问题描述】:

我正在使用 iTextSharp 生成 PDF。我有一个 HTMl 页面,我正在阅读 HTML 页面然后生成 PDF。但问题是页面的一半是 PDF,而页面的另一半用完了PDF中的页面。我的意思是页面的一半显示在PDF中。而页面的一半正在PDF中剪切。

我的代码在加载事件中是这样的..

    string fileContents;
    string FilePath = Server.MapPath("print-withoutlogin.html");
    StreamReader mstrFileStreamReader = new StreamReader(FilePath);
    try
    {
        fileContents = mstrFileStreamReader.ReadToEnd();
        byte[] result = createPDF(fileContents.ToString()).GetBuffer();

        Response.Clear();
        Response.AddHeader("Content-Length", result.Length.ToString());
        Response.ContentType = "application/pdf";
        Response.AddHeader("Accept-Ranges", "bytes");
        Response.Buffer = true;
        Response.AddHeader("Expires", "0");
        Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        Response.AddHeader("Pragma", "public");
        Response.AddHeader("content-Transfer-Encoding", "binary");
        Response.AddHeader("Content-Disposition", "attachment; filename=kartik.pdf");
        Response.BinaryWrite(result);
        Response.Flush();
        Response.End();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        mstrFileStreamReader.Close();
    }

 private MemoryStream createPDF(string html)
 {
    MemoryStream msOutput = new MemoryStream();
    TextReader reader = new StringReader(html);

    Document document = new Document(PageSize.A4, 0, 0, 50, 50);

    PdfWriter writer = PdfWriter.GetInstance(document, msOutput);
    HTMLWorker worker = new HTMLWorker(document);
    //worker.SetStyleSheet(styles);


    // step 4: we open document and start the worker on the document 
    document.Open();
    worker.StartDocument();
    // step 5: parse the html into the document      
    worker.Parse(reader);
    // step 6: close the document and the worker     
    worker.EndDocument();
    worker.Close();
    document.Close();

    return msOutput;
 }

【问题讨论】:

  • 那么到底是什么问题呢?您有 2 页内容,但只有 1 页被放入 PDF 中?
  • @ramhound:那里只有一页..所以我只能看到一半的页面..
  • 您能否提供一个示例 PDF 和 HTML 源代码(以及 css,如果适用)?

标签: c# .net pdf itextsharp


【解决方案1】:

您是否考虑过使用 Crystal Reports?我发现它更容易,您可以使用

pdfStream = (MemoryStream)report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

【讨论】:

  • :你能告诉我更多你想告诉我的细节吗...?我不完全是......
猜你喜欢
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多