2011-07-16 20 views
2

私はItextSharpを使用してaspxページからpdfファイルを生成しています。aspxからpdfへの変換

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

私はこの問題で立ち往生しています - :SEは、文字列のリーダーであり、それはのパスを受け取り

しかし、それはobj.Parseに私にエラーを与える(SE)、。

助けてください。

はここであなたに

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.IO; 
using System.Text.RegularExpressions; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 
using iTextSharp.text.html; 
using iTextSharp.text.xml; 
using System.Xml; 
using iTextSharp.text.html.simpleparser; 
public partial class Pdf : System.Web.UI.Page 
{ 
protected override void Render(HtmlTextWriter writer) 
{ 
    MemoryStream mem = new MemoryStream(); 
    StreamWriter twr = new StreamWriter(mem); 
    HtmlTextWriter myWriter = new HtmlTextWriter(twr); 
    base.Render(myWriter); 
    myWriter.Flush(); 
    myWriter.Dispose(); 
    StreamReader strmRdr = new StreamReader(mem); 
    strmRdr.BaseStream.Position = 0; 
    string pageContent = strmRdr.ReadToEnd(); 
    strmRdr.Dispose(); 
    mem.Dispose(); 
    writer.Write(pageContent); 
    CreatePDFDocument(pageContent); 


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

エラーメッセージは私たちと共有できますか? – iandotkelly

+0

チェックアウトこのリンクを、それはあなたを助けることができるかどう http://stackoverflow.com/questions/6702884/how-to-convert-aspx-page-to-pdf-file/6703583#6703583 –

+0

よりよい解決策: http://stackoverflow.com/q/1220423/2291 –

答えて

0

をありがとう私のために働いた例です。コードは少しありますが、CSSのようなものを利用する方法を示しています。それが役に立てば幸い。

private void ExportToPDF() 
{ 
    string s = "<table><td><tr>First<b>row</b></tr></td></table>"; 
    Document document = new Document(PageSize.LETTER, 30, 30, 60, 35); 
    MemoryStream msReport = new MemoryStream(); 
    string strHTMLpath = Server.MapPath("../myHTML.html"); 
    string strPDFpath = Server.MapPath("../myPDF.pdf"); 
    try 
    { 
     StringWriter sw = new StringWriter(); 
     HtmlTextWriter htw = new HtmlTextWriter(sw); 
     StreamWriter strWriter = new StreamWriter(strHTMLpath, false, Encoding.UTF8); 
     strWriter.Write(s.ToString()); 
     strWriter.Close(); 
     strWriter.Dispose(); 
     iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet(); 
     styles.LoadTagStyle("ol", "leading", "16,0"); 
     PdfWriter.GetInstance(document, new FileStream(strPDFpath, FileMode.Create)); 
     document.Add(new Header(iTextSharp.text.html.Markup.HTML_ATTR_STYLESHEET, "Style.css")); 
     document.Open(); 
     ArrayList objects; 
     styles.LoadTagStyle("li", "face", "garamond"); 
     styles.LoadTagStyle("span", "size", "8px"); 
     styles.LoadTagStyle("body", "font-family", "times new roman"); 
     styles.LoadTagStyle("body", "font-size", "10px"); 
     document.NewPage(); 
     objects = iTextSharp.text.html.simpleparser. 
     HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles); 
     for (int k = 0; k < objects.Count; k++) 
     { 
      document.Add((IElement)objects[k]); 
     } 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
    finally 
    { 
     document.Close(); 
     Response.Write(strPDFpath); 
     Response.ClearContent(); 
     Response.ClearHeaders(); 
     Response.AddHeader("Content-Disposition", "attachment; filename=myFileName.pdf"); 
     Response.ContentType = "application/octet-stream"; 
     Response.WriteFile(strPDFpath); 
     Response.Flush(); 
     Response.Close(); 
     File.Delete(strPDFpath); 
    } 
} 
関連する問題