我正在使用 iTextSharp 中的 PdfStamper 创建 PDF 文件并将 PDF 作为内存流返回 调用函数的对象,然后用于在 WinForms 的 Teleriks PDF 查看器组件中显示 PDF。
这就是目标。
现在,创建 PDF 工作正常,它将数据返回给调用函数,在调用函数中,我是否应该将内存流内容写入文件流,然后在 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();
这指示压模器不要关闭流。
关于c# - 将 iTextSharp PDF 作为内存流返回导致 StreamNotSupported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25415350/