2012-02-28 83 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(); 

应该解决这个问题。