This is a migrated thread and some comments may be shown as answers.

PdfViewer throws "Unsupported Stream Type" when using Memorystream

1 Answer 131 Views
PdfViewer and PdfViewerNavigator
This is a migrated thread and some comments may be shown as answers.
Johannes
Top achievements
Rank 1
Johannes asked on 20 Aug 2014, 10:21 PM
I'm creating a PDF file using the PdfStamper in iTextSharp and return the PDF as a memorystream object to the calling function, that is then used to display the PDF in Teleriks PDF Viewer Component for WinForms. That's the objective.Right now, creating the PDF works as it should, and it returns the data to the Calling function, and in the Calling function, should I write the memorystream contents to a file stream and then open it in Adobe Reader Everything looks just fine.However, if I choose to display the PDF in the PDF Viewer Control I just get an "Unsupported Stream type" error.Now, I figured something was wrong in the PDF data so I decided to create the PDF file, save it to disk, then read it to a memorystream in the Calling function and display that memorystream in the PDF Viewer and that, for some to me unknown reason, works.... I really can't get my head around this and need some help.

So, this won't work:

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

But this WILL work:
//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();
}

But why? I'd rather keep it all in memory and let the user save the resulting PDF if he/she so wishes than having to write it to disk and then displaying it.
Am I missing some crucial step here?


1 Answer, 1 is accepted

Sort by
0
Accepted
Johannes
Top achievements
Rank 1
answered on 21 Aug 2014, 11:24 AM
I managed to solve it using pdfStamper.CloseStream = false;

Now it works like a charm. :)
Tags
PdfViewer and PdfViewerNavigator
Asked by
Johannes
Top achievements
Rank 1
Answers by
Johannes
Top achievements
Rank 1
Share this question
or