2012-02-28 250 views
0
//the func merges the src pdf with the memory stream, wherein the stream may contain 
//few othr src pdf streams in previous calls to this func 
//in first cal, ms would be null 
public static void MergePdf(MemoryStream ms, string srcFile) 
{ 
    PdfReader reader = new PdfReader(srcFile); 
    Document document = null; 
    PdfWriter writer = null; 
    int n = reader.NumberOfPages; 
    if (document == null) 
    { 
     document = new Document(reader.GetPageSizeWithRotation(1)); 

     writer = PdfWriter.GetInstance(document, ms); 

     document.Open(); 
    } 
    PdfContentByte cb = writer.DirectContent; 
    PdfImportedPage page; 
    int rotation; 

    int i = 0; 
    while (i < n) 
    { 
     i++; 
     document.SetPageSize(reader.GetPageSizeWithRotation(i)); 
     document.NewPage(); 
     page = writer.GetImportedPage(reader, i); 
     rotation = reader.GetPageRotation(i); 
     if (rotation == 90 || rotation == 270) 
     { 
      cb.AddTemplate(page, 0, -1f, 1f, 0, 0, 
            reader.GetPageSizeWithRotation(i).Height); 
     } 
     else 
     { 
      cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); 
     } 
    } 

} 

我寫內存流,回context.response.Outputstream;但pdf不加載,頁面'加載PDF'失敗的結果;轉換btwn記憶流和PDF內容有什麼問題,或者可能是什麼問題?無法加載PDF文檔

+0

您需要真正問一個問題。如果此代碼不能按照您的要求運行,那麼您必須解釋您認爲應該發生的事情以及實際發生的事情。 – 2012-02-28 05:16:00

+0

你想要什麼?你是否有任何錯誤,如果是這樣指定錯誤。 – SoftwareNerd 2012-02-28 06:03:06

+0

我已經提到了錯誤,它是'無法加載PDF' – Krishnan 2012-02-28 06:46:32

回答

2

我有同樣的問題,而且事實證明,原因是不關閉的文件,並添加以下代碼行:

document.Close(); 

應該解決這個問題。