无法加载PDF文档

时间:2012-02-28 05:10:29

标签: c# asp.net

//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);
        }
    }

}

我写了Memory流,回到了context.response.Outputstream;但pdf没有加载,页面'无法加载Pdf'结果;转换btwn memorystream和Pdf内容有什么问题,或者可能是什么问题?

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,事实证明原因并未关闭文档,并添加了以下代码行:

document.Close();

应该解决问题。