Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using itextsharp to save a paragraph in to the memorystream as pdf
but i dont work why?
C#
     private void buttonSkrivUt_Click(object sender, EventArgs e)
        {

            using (MemoryStream ms = new MemoryStream())
            {

                Document document = new Document(PageSize.A4, 25, 25, 30, 30);
;
                PdfWriter writer = PdfWriter.GetInstance(document, ms);

                document.Open();
             
                document.Add(new Paragraph("hej"));

                document.Close();

                writer.Close();
            
             
            }

            
        }


This works only.

>
C#
                Document document = new Document(PageSize.A4, 25, 25, 30, 30);
;
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("test.pdf", FileMode.Create));
                //PdfWriter writer = PdfWriter.GetInstance(document, ms);

                document.Open();
             
                document.Add(new Paragraph("hej"));

                document.Close();

                writer.Close();
Posted
Updated 25-Feb-14 0:39am
v3
Comments
Kornfeld Eliyahu Peter 25-Feb-14 6:22am    
'dont work' - it's not too informative! Can you elaborate on that?
Kurac1 25-Feb-14 6:34am    
Nothings happens.
Kornfeld Eliyahu Peter 25-Feb-14 6:36am    
OK...What do you expect to happening?
Kurac1 25-Feb-14 6:37am    
i want it to open up the pdf document in a memorystream then the user can save it were he wants
Kurac1 25-Feb-14 6:40am    
i have updated ,the second one creates the document in debug , but i want it to opet in memorystream?

1 solution

You need to return something to work with when using MemoryStream. The best way to do this is to return an array. This way you get the byte[]

C#
using (MemoryStream ms = new MemoryStream())
{
    Document document = new Document(PageSize.A4, 25, 25, 30, 30);
    PdfWriter writer = PdfWriter.GetInstance(document, ms);
    document.Open();
    document.Add(new Paragraph("hej"));
    document.Close();
    writer.Close();

    return ms.ToArray();
}


PS I did not mean to sound like you did not know the difference
 
Share this answer
 
v2
Comments
Kurac1 25-Feb-14 6:55am    
Yes i know the differences , but why does the filestream work but not the memorystream is my question?
CBadger 25-Feb-14 7:56am    
I created a new solution. Does this answer your question? :-)
Kurac1 25-Feb-14 10:47am    
No nothing happens.
CBadger 26-Feb-14 3:49am    
As in nothing at all or did it give an error of sorts?
Remeber to use the ms to generate pdf now as it is in an byte[] now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900