【问题标题】:convert a report to pdf using itextsharp使用 itextsharp 将报告转换为 pdf
【发布时间】:2013-11-05 19:16:30
【问题描述】:

我正在尝试将我的网站转换为 pdf,我的网站用于报告,在一个页面上输入信息,在另一页面上通过会话获取信息,如果重要的话,网站中有图片和 css。

我正在尝试使用 itextsharp,但它对我来说太复杂了。

我尝试在这里使用一些示例,但它们都不是我所需要的,所以它不起作用。 http://simpledotnetsolutions.wordpress.com/2012/04/08/itextsharp-few-c-examples/

谁能向我解释我需要做什么?

我也试过这段代码,但它不起作用,有人告诉我我需要使用 xmlworker 但我真的不明白如何(以及它是否会起作用)

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

谢谢

顺便说一句,如果有一个简单的解决方案需要花钱,我有能力支付。

【问题讨论】:

  • 你考虑过SSRS吗? SSRS 报告可以以 PDF 格式输出。将 HTML 呈现的输出转换为 PDF 并不简单。至少以更通用的格式保存报告,可以重复使用以显示为 HTML 或 PDF。

标签: c# asp.net pdf-generation itextsharp


【解决方案1】:

尝试使用Scryber,它是在商业用途中付费的,但您可以使用免费版本(它在生成的 .pdf 文件上添加水印)进行原型设计。

.pdf 文件结构和样式可在 .xml 中配置,因此非常简单灵活。

定义结构示例

<data:ForEach select="rss/channel" datasource-id="BBCRss" >
         <Template>
           <pdf:Div style:class="heading" >
             <data:If test="string-length(image/url) &gt; 0" >
               <Template>
                 <pdf:Link action="Uri" file="{xpath:image/link}" new-window="true" alt="{xpath:image/title}" >
                   <Content>
                     <pdf:Image src="{xpath:image/url}" style:width="{xpath:image/width}" style:padding="10" />
                   </Content>
                 </pdf:Link>
               </Template>
             </data:If>
               <pdf:H1 style:class="red-text" text="{xpath:title}"></pdf:H1>
               <pdf:Label text="{xpath:description}" /><BR/>
               Date: <pdf:Label text="{xpath:lastBuildDate}" />
           </pdf:Div>

           <pdf:Div style:class="content" >            
             <data:ForEach select="item" >
               <Template>
                 <pdf:Div style:class="rss-item red-underline" >
                   <pdf:Div style:class="rss-item-data" >
                     <pdf:H2 style:class="red-text" text="{xpath:title}" ></pdf:H2>
                     <pdf:Label text="{xpath:description}" />
                   </pdf:Div>
                 </pdf:Div>

          </Template>
 </data:ForEach>

风格

<style:Style applied-type="pdf:Div" applied-class="content" >
   <style:Columns count="2" alley-width="20pt"/>
   <style:Font size="12pt" bold="false" italic="false"  />
 </style:Style>

Here 你会找到示例项目和一些解释。 此外,Scryber 比 iTextSharp 有更好的文档记录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-03-07
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 2014-09-29
    • 2015-12-19
    相关资源
    最近更新 更多