Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, everybody
I want to create pdf using c# and I wrote that code :

C#
private void btnCreate_Click(object sender, EventArgs e)
       {
           Document doc = new Document(iTextSharp.text.PageSize.LETTER,10,10,42,35);
           PdfWriter pwrt = PdfWriter.GetInstance(doc, new
                                      FileStream("test.pdf",FileMode.Create));
           //open docement
           doc.Open();
           //write some comment in pdf
           Paragraph prg = new Paragraph("I love you");
           //Add paragraph to document
           doc.Add(prg);
           //close document
           doc.Close();
       }


But that code can not work. Please help me.
Posted
Updated 2-Oct-14 8:09am
v2
Comments
[no name] 2-Oct-14 14:00pm    
"code can not work".... means what?
ZurdoDev 2-Oct-14 14:03pm    
I suggest reading the documentation for whatever PdfWriter is.
goksurahsan 2-Oct-14 14:04pm    
I want to create a pdf in local disk (C).But that code that I wrote create a pdf in debug
Richard Deeming 2-Oct-14 14:20pm    
Hardly surprising, given you haven't specified the path:
new FileStream("test.pdf", FileMode.Create)

What you've specified there is a relative path. That will be treated as a path relative to the current directory - in this case, the directory from which the application is executing. In other words, the "bin\Debug" path.

Specify the full path where you want to save the file. For example:
new FileStream(@"C:\Your\Path\Here\test.pdf")

Be careful trying to save the file to the root of your C: drive - most applications won't have permission to write to that directory.
CHill60 2-Oct-14 14:27pm    
Worth posting as the solution

Based on your comments, the file is being created, but in the bin\Debug folder under your project. You want the file to be created in a specific path.

The problem is that you've only specified a relative path in the FileStream constructor:
C#
new FileStream("test.pdf",FileMode.Create)

This path will be resolved relative to the current path, which is usually the same as the path from which the application is running. In other words, the bin\Debug path under your project.

To save to a specific path, you'll need to pass the full path to the FileStream constructor:
C#
new FileStream(@"C:\Your\Path\Here\test.pdf",FileMode.Create)


NB: Be careful trying to save the document to the root of your C: drive; most applications won't have permission to write to that directory. Make sure you chose a path to which your application has write permissions.
 
Share this answer
 
Comments
goksurahsan 2-Oct-14 14:37pm    
I wrote new FileStream(@"C:\Windows\test.pdf"),FileMode.Create)
but it was denied
Richard Deeming 2-Oct-14 14:39pm    
Because your application doesn't have permission to write to the Windows directory. If you read the final paragraph of my answer, you'll see that I did point that out!

Make sure you chose a path to which your application has write permissions.
goksurahsan 2-Oct-14 15:31pm    
thank you very much...
Refer - Creating PDF Document in 6 Steps:[^].

It says...
Quote:
C#
FileStream fs = new FileStream("Chapter1_Example1.pdf", FileMode.Create, FileAccess.Write, FileShare.None);

But you have initialized only in Create mode. I hope it solves the issue. Otherwise, let me know.
 
Share this answer
 

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