2016-09-22 23 views
1

我已經搜索了本主題的300多篇文章中的大多數,並且找不到專門解決此問題的文章。使用iTextSharp從C#創建PDF會產生0字節的文件

我試過最簡單的文件創建者,結果相同:(從http://www.codeproject.com/Articles/686994/Create-Read-Advance-PDF-Report-using-iTextSharp-in#1)我在另一篇文章中發現了這個鏈接。

public byte[] generatePublicationCitationReport(List<int> pubIDs) 
{ 
//Step 1: Create a System.IO.FileStream object: 
    MemoryStream ms = new MemoryStream(); 

//Step 2: Create a iTextSharp.text.Document object: 
    Document doc = new Document(); 

//Step 3: Create a iTextSharp.text.pdf.PdfWriter object. It helps to write the Document to the Specified FileStream: 
    PdfWriter writer = PdfWriter.GetInstance(doc, ms); 

//Step 4: Openning the Document: 
    doc.Open(); 

//Step 5: Adding a Paragraph by creating a iTextSharp.text.Paragraph object: 
    doc.Add(new Paragraph("Hello World")); 

//Step 6: Closing the Document: 
    doc.Close(); 

    return ms.ToArray(); 
} 

的代碼進行了修改稍微改變了「文件流」到「MemoryStream的」,並傳遞一個回調用函數打開該文件。

上面的代碼生成一個0字節的文件並嘗試打開它。打開失敗時,我收到一條錯誤消息,指出「無法加載PDF文件」。

我正嘗試從SQL數據庫中的數據創建的引用列表中生成PDF文件。我正確地獲取數據並可以使用Response.Write顯示它。

在我的代碼中,我添加一個循環來單獨創建每個引用並將其添加到段落中。

iTextSharp.text.Paragraph paragraph1 = new iTextSharp.text.Paragraph(); 
iTextSharp.text.Paragraph paraCitations = new iTextSharp.text.Paragraph(); 
iTextSharp.text.Paragraph paragraph3 = new iTextSharp.text.Paragraph(); 
iTextSharp.text.Chunk chunk1 = new iTextSharp.text.Chunk("Chunky stuff here..."); 
paragraph1.Add("Paragraph stuff goes here..."); 

for (int i = 0; i < pubIDs.Count; i++) 
{ 
    string pubCitation = createPubCitation(pubIDs[i]); 
    chunk1.Append(pubCitation); 
    paraCitations.Add(chunk1); 
} 
paragraph3.Add("New paragraph - paraCitations - goes here"); 

doc.Add(paragraph1); 
doc.Add(paraCitations); 
doc.Add(paragraph3); 
doc.Close(); 

return ms.toArray(); 

}

有什麼建議?指針?回答?

感謝, 鮑勃

這是調用和創建PDF文件並打開它的程序返回...

pubCitationAsPDF = p.generatePublicationCitationReport(pubIDs); 

Response.ClearContent(); 
Response.ClearHeaders(); 
Response.ContentType = "application/pdf"; 
Response.AddHeader("Content-Disposition", "attachment; filename=publicationCitations.pdf"); 

Response.End(); 
Response.Flush(); 
Response.Clear(); 
+0

你在哪裏使用您的內存流,節省了實際的文件? – Darren

+0

成千上萬的開發人員在使用iTextSharp之前成功地使用了與您的完全相同的代碼。如果你得到0字節,甚至在'doc.Open();'之前出錯了,因爲該行導致寫入第一個字節(更具體地說:PDF文件的頭部)。儘管在代碼片段中有一個明顯的錯誤:在文檔打開後使用AddTitle()方法。這引發了一個異常。 –

+0

@Darren,這是調用過程並打開文件的代碼。 p.generatePublicationCitationReport(pubIDs); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType =「application/pdf」; Response.AddHeader(「Content-Disposition」,「attachment; filename = publicationCitations.pdf」); Response.End(); Response.Flush(); Response.Clear(); –

回答

1

按照意見,看來你的問題更與您如何下載文件相關,與創建文件相關。

您的下載代碼不包括將內存流中的字節添加到您的響應中。

更改您的代碼下載到這一點:

Response.Clear(); 
Response.ContentType = "application/force-download"; 
Response.AddHeader("content-disposition", "attachment; filename=publicationCitations.pdf"); 
// This is the piece you're missing 
Response.BinaryWrite(p.generatePublicationCitationReport(pubIDs)); 
Response.End(); 
相關問題