Itextsharp Pdfwriter.getinstance返回1kb pdf文件

时间:2015-10-18 10:13:20

标签: c# itextsharp

我正在尝试创建一个PDF格式的内存流,用于测试我是否将其写入磁盘。当我写流时,我得到一个1kb的PDF文件。有什么想法吗?

更新 看起来我没有调用doc.close,但是当我这样做时,我处理了我的最终报告流。有办法解决这个问题吗?

public static Stream CombinePages(Stream firstPageStream, string nextPagesString)
    {


        var firstPage = new PdfReader(firstPageStream);
        var nextPages = new PdfReader(nextPagesString);

        Stream finalReportStream = new MemoryStream();
        var doc = new Document();
        var w = PdfWriter.GetInstance(doc, finalReportStream);

            doc.Open();

            doc.SetPageSize(firstPage.GetPageSize(1));
            doc.NewPage();

            //Add Page 1
            w.DirectContent.AddTemplate(w.GetImportedPage(firstPage, 1), 0, 0);

            //Add the rest of the pages
            //copy readnextpages to doc starting page2 this cuts the first page
            for (var page = 2; page <= nextPages.NumberOfPages; page++)
            {
                doc.SetPageSize(nextPages.GetPageSize(page));

                doc.NewPage();
                w.DirectContent.AddTemplate(w.GetImportedPage(nextPages, page), 0, 0);
            }



        return finalReportStream;




    }

然后将其写入磁盘:

        var fileStream = File.Create(destfilename);

        finalReportStream.CopyTo(fileStream);

        fileStream.Close();

0 个答案:

没有答案