2013-08-17 29 views

回答

1

最好的選擇是iTextSharp,迄今爲止最簡單的呈現PDF的方式。您可以使用html模板並以dinamically替換值,或者將任何webcontrol渲染爲html字符串並將其另存爲pdf。

你可以有這樣的事情

string html = "<h1>[PAGE_TITLE]<h1/>[STUDENTS]"; 
//get your values here 
... 

html = html.Replace("[PAGE_TITLE]", MyPageTitle); 
html = html.Replace("[STUDENTS]", MyStudentsTableHtml); 
與iTextSharp的

然後(從http://www.4guysfromrolla.com/articles/030911-1.aspx兩者)​​

// Create a Document object 
var document = new Document(PageSize.A4, 50, 50, 25, 25); 

// Create a new PdfWriter object, specifying the output stream 
var output = new MemoryStream(); 
var writer = PdfWriter.GetInstance(document, output); 

// Open the Document for writing 
document.Open(); 

var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(html), null); 
foreach (var htmlElement in parsedHtmlElements) 
{ 
    document.Add(htmlElement as IElement); 
} 

document.Close(); 

Response.ContentType = "application/pdf"; 
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=students{0}.pdf", YourStudendsPrintingId)); 
Response.BinaryWrite(output.ToArray()); 

正如你所看到的,有一些調整,從你的身邊,你可以創建PDF你需要。有一件事他們沒有說,iTextSharp對html非常挑剔,它發現html上有一些錯誤,或者不喜歡一些舊的標籤(比如說<HR>),它會拋出一個非常討厭的異常,它並不指向那種問題!

相關問題