我正在使用iTextSharp中的PdfStamper创建一个PDF文件,并将PDF作为内存流对象返回到调用函数,然后用于在WinForms的Teleriks PDF Viewer Component中显示PDF.
这是目标.
现在,创建PDF可以正常工作,并将数据返回给Calling函数,在Calling函数中,我应该将memorystream内容写入文件流,然后在Adobe Reader中打开它看起来很好.
但是,如果我选择在PDF查看器控件中显示PDF,我只会收到"不支持的流类型"错误.
现在,我认为PDF数据有问题所以我决定创建PDF文件,将其保存到磁盘,然后在Calling函数中将其读取到内存流,并在PDF Viewer中显示该内存流,对于我来说不明原因,作品....
我真的无法理解这一点并需要一些帮助.
所以,这将无法正常工作:
//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(); …Run Code Online (Sandbox Code Playgroud)