Click here to Skip to main content
15,116,253 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 1: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?
Kornfeld Eliyahu Peter 25-Feb-14 6:42am
   
'opet in memorystream' - open from where? MemoryStream - as it's name suggest - is a kind of file that exists in your application memory range.
Kurac1 25-Feb-14 6:46am
   
I want it to ope/create the document in the computers memory then open the document and not save the document to local disk
BotCar 25-Feb-14 6:55am
   
So you want to display the document without saving it to disk? I think your best bet would be to save the document to a temporary file.
Kurac1 25-Feb-14 6:57am
   
Alright any examples how i do that?
Kornfeld Eliyahu Peter 25-Feb-14 7:02am
   
Do what?
Kurac1 25-Feb-14 7:09am
   
I think your best bet would be to save the document to a temporary file.
Kornfeld Eliyahu Peter 25-Feb-14 7:21am
   
Maybe - however I'm still do not understand what do you want to achieve...

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
   
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