将iTextSharp PDF作为内存流返回会导致StreamNotSupported
我正在使用iTextSharp中的PdfStamper创建一个PDF文件,并将PDF作为内存流对象返回到调用函数,然后用于在WinForms的Teleriks PDF Viewer Component中显示PDF。
这是目标。
现在,创建PDF可以正常工作,并将数据返回给Calling函数,在Calling函数中,我应该将memorystream内容写入文件流,然后在Adobe Reader中打开它看起来很好。
但是,如果我选择在PDF查看器控件中显示PDF,我只会收到“不支持的流类型”错误。
现在,我发现PDF数据有问题所以我决定创建PDF文件,将其保存到磁盘,然后在调用函数中将其读取到内存流,并在PDF查看器中显示该内存流,对于某些人来说不明原因,作品….
我真的无法理解这一点并需要一些帮助。
所以,这不起作用:
//The Calling function private void dlgViewPDF_Load(object sender, EventArgs e) { MemoryStream ms = PDFcreator.GeneratePDFdata(id); rPdfView.LoadDocument(ms); } //The PDF generator public static MemoryStream GeneratePDFdata(string id) { MemoryStream ms = new MemoryStream(); string sTemplate = string.Concat(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "\\template.pdf"); PdfReader pdfReader = new PdfReader(sTemplate); PdfStamper pdfStamper = new PdfStamper(pdfReader, ms); PdfContentByte cb = pdfStamper.GetOverContent(1); BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED); BaseFont baseFontBold = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.EMBEDDED); cb.SetColorFill(iTextSharp.text.Color.BLACK); cb.SetFontAndSize(baseFontBold, 14); cb.BeginText(); cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "TEST!!", 385, 750, 0); cb.EndText(); cb.SetColorStroke(new CMYKColor(0f, 0f, 0f, 1f)); cb.SetColorFill(new CMYKColor(0f, 0f, 0f, 1f)); cb.MoveTo(139, 398); cb.LineTo(146, 398); cb.LineTo(146, 391); cb.LineTo(139, 391); cb.ClosePathEoFillStroke(); pdfStamper.Close(); pdfReader.Close(); return ms; }
但是,由于某种原因,这确实有效:
//The Calling function private void dlgViewPDF_Load(object sender, EventArgs e) { MemoryStream ms = new MemoryStream(); FileStream file = new FileStream(@"c:\temp\testfile.pdf", FileMode.Open, FileAccess.Read); byte[] bytes = new byte[file.Length]; file.Read(bytes, 0, (int)file.Length); ms.Write(bytes, 0, (int)file.Length); rPdfView.LoadDocument(ms); } //The PDF generator public static void GeneratePDFdata(string id) { MemoryStream ms = new MemoryStream(); string sTemplate = string.Concat(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "\\template.pdf"); PdfReader pdfReader = new PdfReader(sTemplate); FileStream fs = new FileStream(@"c:\temp\testfile.pdf", FileMode.Create, FileAccess.Write, FileShare.None); PdfStamper pdfStamper = new PdfStamper(pdfReader, fs); PdfContentByte cb = pdfStamper.GetOverContent(1); BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED); BaseFont baseFontBold = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.EMBEDDED); cb.SetColorFill(iTextSharp.text.Color.BLACK); cb.SetFontAndSize(baseFontBold, 14); cb.BeginText(); cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "TEST!!", 385, 750, 0); cb.EndText(); cb.SetColorStroke(new CMYKColor(0f, 0f, 0f, 1f)); cb.SetColorFill(new CMYKColor(0f, 0f, 0f, 1f)); cb.MoveTo(139, 398); cb.LineTo(146, 398); cb.LineTo(146, 391); cb.LineTo(139, 391); cb.ClosePathEoFillStroke(); pdfStamper.Close(); pdfReader.Close(); }
但为什么? 我宁愿把它全部保存在内存中,让用户保存生成的PDF,如果他/她愿意,而不是将其写入磁盘然后显示它。
出现问题是因为当PdfStamper关闭时,内存流会被隐式关闭。 为了防止这种添加
pdfStamper.Writer.CloseStream = false;
之前
pdfStamper.Close();
这指示压模不要关闭流。
- 为什么在尝试使用动态参数调用扩展方法时出现错误CS1973
- 如果实例已经处理掉,在不调用EndXXX的情况下调用BeginXXX是否安全
- 在wpf mvvm中绑定时间跨度,并且仅显示分钟:秒?
- 直接访问TableLayoutPanel Cells
- ITextSharp中的页眉和页脚
- C#为什么Bitmap.Save会忽略Bitmap的PixelFormat?
- Visual Studio:在调试时编辑XAML文件
- C#如何使用WM_GETTEXT / GetWindowText API
- 如果不支持Contains,如何在LINQ to Entities(entity framework)中执行SQL样式的“IN”语句?