12 Replies - 21066 Views - Last Post: 16 February 2016 - 02:43 PM Rate Topic: -----

#1 kokkinogenis   User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 25
  • Joined: 20-December 15

How to save pdf doc using iTextSharp to specific path

Posted 08 February 2016 - 04:47 AM

hello ! I am creating a program, that among others, creates pdf documents. I use the itextSharp. My code is this:

            
             Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
            PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(textBox1.Text + ".pdf", FileMode.Create));//textBox1(in the form) assign here the title of the document
            doc.Open(); //open the document in order to write inside.
            Paragraph paragraph = new Paragraph(richTextBox1.Text);
            // Now adds the above created text using different class object to our pdf document
            doc.Add(paragraph);
            doc.Close();
            }


Now, in my program(windows form application) i have in a textbox(textBox1), the path that i want the file to be created. The problem is, that i don't know how to "link" the path on the textbox, to this code. As you can see in the code. I tried some noob things that i thought, but neither of them worked. Any ideas ??

Is This A Good Question/Topic? 0
  • +

Replies To: How to save pdf doc using iTextSharp to specific path

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,972
  • Joined: 05-May 12

Re: How to save pdf doc using iTextSharp to specific path

Posted 08 February 2016 - 05:48 AM

What behavior are you seeing with that code? What behavior were you expecting to see?
Was This Post Helpful? 0
  • +
  • -

#3 kokkinogenis   User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 25
  • Joined: 20-December 15

Re: How to save pdf doc using iTextSharp to specific path

Posted 08 February 2016 - 06:10 AM

View PostSkydiver, on 08 February 2016 - 05:48 AM, said:

What behavior are you seeing with that code? What behavior were you expecting to see?

The behavior that i see is that it generates the .pdf file on the directory of the program. What i want, is to generate the .pdf file in the directory that i have on the text box.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,972
  • Joined: 05-May 12

Re: How to save pdf doc using iTextSharp to specific path

Posted 08 February 2016 - 06:23 AM

And what value do you have for textBox1.Text? Do you have a fully qualified path there?
Was This Post Helpful? 0
  • +
  • -

#5 kokkinogenis   User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 25
  • Joined: 20-December 15

Re: How to save pdf doc using iTextSharp to specific path

Posted 08 February 2016 - 08:13 AM

View PostSkydiver, on 08 February 2016 - 06:23 AM, said:

And what value do you have for textBox1.Text? Do you have a fully qualified path there?

Yes. Like this for example: C:\Users\George\Desktop
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,972
  • Joined: 05-May 12

Re: How to save pdf doc using iTextSharp to specific path

Posted 08 February 2016 - 08:19 AM

new FileStream(textBox1.Text + ".pdf", FileMode.Create) would have created a file to what ever path is passed to it. Assuming that iTextSharp really just uses the stream given to it, it wouldn't care what directory the .PDF file is here.

There is something else at play here that you are not showing us.

Are you sure that is the value of the textbox? Where is the filename part of the path? Right now with the value, the file stream created would have been name "C:\Users\George\Desktop.pdf".
Was This Post Helpful? 0
  • +
  • -

#7 kokkinogenis   User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 25
  • Joined: 20-December 15

Re: How to save pdf doc using iTextSharp to specific path

Posted 08 February 2016 - 08:44 AM

yes. Sorry. My bad. I am really really sorry. I didn't saved the project and i gave you the old code. This is the right:

if (checkBox2.Checked)
            {
             Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
            PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(textBox2 + textBox1.Text + ".pdf", FileMode.Create));
            doc.Open(); 
            Paragraph paragraph = new Paragraph(richTextBox1.Text);
           
            doc.Add(paragraph);
            doc.Close();


The textBox2 has the path of the directory that i want the file to be saved. The textBox1 has the name of the file.

This is what i tried in order to have the output that i wanted(the .pdf file to be saved on a custon directory). But it failed. Now, if i change this line of code:
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(textBox2 + textBox1.Text + ".pdf", FileMode.Create));


to this:
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(textBox1.Text + ".pdf", FileMode.Create));

The file is saved in the directory of the program. The hole code that i gave you on the beginning of this post is what i tried in order to make somehow the program, to save the .pdf file on the directory that i want, and not on the directory of the program.

Sorry for my bad English. I hope you understand :(/>

This post has been edited by kokkinogenis: 08 February 2016 - 08:46 AM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,972
  • Joined: 05-May 12

Re: How to save pdf doc using iTextSharp to specific path

Posted 08 February 2016 - 10:17 AM

Look into using Path.Combine(). I suspect that your raw appending of strings is not handling putting in the separators in the right places.
Was This Post Helpful? 0
  • +
  • -

#9 rgfirefly24   User is online

  • D.I.C Lover
  • member icon


Reputation: 466
  • View blog
  • Posts: 2,212
  • Joined: 07-April 08

Re: How to save pdf doc using iTextSharp to specific path

Posted 08 February 2016 - 11:14 AM

Are you doing any kind of Debugging and stepping through to see what the values actually are in your UI Controls? (http://www.dreamincode.net/forums/topic/312252-debugging-express/) I would suspect that you are forgetting the ending \ when you enter a path so the output would look like so:

if textbox2.text was c:\users\rgfirefly24\desktop and textbox1.text was fileName, then this line of code: textbox2.Text + textbox1.Text would produce: c:\users\rgfirefly24\desktopfileName which isn't valid and would be why you were seeing errors. I would echo Skydiver's suggestion to use Path.Combine() as it will add/remove slashes as needed to create the valid path. https://msdn.microso...(v=vs.110).aspx
Was This Post Helpful? 0
  • +
  • -

#10 kokkinogenis   User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 25
  • Joined: 20-December 15

Re: How to save pdf doc using iTextSharp to specific path

Posted 08 February 2016 - 02:03 PM

Ok path.Combine, but will "stick" on to the rest of the code ?? I don't see the connection....

EDIT: Ok guys. I found a brilliant idea and i worked it out. I did this:

if (checkBox2.Checked)
            {
                string SavePath = textBox2.Text + @"\" + textBox1.Text + ".pdf";

             Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
            PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(SavePath, FileMode.Create));//textBox1(in the form) assign here the title of the document
            doc.Open(); //open the document in order to write inside.
            Paragraph paragraph = new Paragraph(richTextBox1.Text);
            // Now adds the above created text using different class object to our pdf document
            doc.Add(paragraph);
            doc.Close();
            }


Now it works fine !! Thanks guys !!

This post has been edited by kokkinogenis: 08 February 2016 - 02:04 PM

Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,972
  • Joined: 05-May 12

Re: How to save pdf doc using iTextSharp to specific path

Posted 08 February 2016 - 02:05 PM

You obviously didn't follow the link to the documentation...
Was This Post Helpful? 0
  • +
  • -

#12 kokkinogenis   User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 25
  • Joined: 20-December 15

Re: How to save pdf doc using iTextSharp to specific path

Posted 14 February 2016 - 01:32 PM

View PostSkydiver, on 08 February 2016 - 02:05 PM, said:

You obviously didn't follow the link to the documentation...

Sorry that i didn't answere earlier, but i was away fro my computer. I didn't understood completely what the link told me, that's why i was "blocked". Now i read it, and i changed it.
Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How to save pdf doc using iTextSharp to specific path

Posted 16 February 2016 - 02:43 PM

You should still use Path.Combine.

All Path.Combine does is take parts of paths, and combine them.

Example:

Quote

string part1 = "root", part2 = "folder", part3 = "subfolder", part4 = "file.ext";
string fullPath = Path.Combine(part1, part2, part3, part4);


Now fullPath == "root\folder\subfolder\file.ext"

I don't know what your form looks like, or how it behaves, but it's typically a bad idea to allow users to enter text for paths (or rather, to rely on that as the only option). Look into using a SaveFileDialog to allow the user a native, familiar way to choose a path. This would make it so you don't have to do any kind of path building yourself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1