Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.
I'm working on a windows forms app which, in one of its parts needs to make and save pdf file to desired location on my PC. I want to make 2 different files in the same app (NOT closing the app and running it again)

For example, I put the text in the textbox, the file should contain that text and after clicking the button SaveFileDialog should open and I will save this file to my PC (THIS PART IS DOING FINE). Then I erase the text from textbox and I add new text in it. If I press the button again and choose the proper location, It will save the same file as before (with the same content as before). How can I deal with this problem?

EDIT:

C#
SaveFileDialog pdfFile = new SaveFileDialog();
pdfFile.Filter = "PDF|*.pdf";
pdfFile.Title = "aaPDF";
pdfFile.FileName = "aaa";
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, false);
pdfFont titleFont = new pdfFont(bf, 20);
pdfFont infoFont = new pdfFont(bf, 12);
pdfFont questionsFont = new pdfFont(bf, 14);
Chunk glue = new Chunk(new VerticalPositionMark());

if (pdfFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
       Document document = new Document(iTextSharp.text.PageSize.A4, 40, 40, 50, 50);
       PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfFile.FileName, FileMode.Create));
       document.Open();

       document.NewPage();
       ParagraphPDF title = new ParagraphPDF("AAA", titleFont);
       title.Alignment = Element.ALIGN_CENTER;
       document.Add(title);
       document.Add(Chunk.NEWLINE);

       ParagraphPDF p1 = new ParagraphPDF(textFromTextbox, infoFont);                   
       document.Add(p1);              

       document.Close();
}
Posted
Updated 4-Dec-15 23:53pm
v4
Comments
OriginalGriff 5-Dec-15 5:36am    
Show us the relevant code - without it we have no idea exactly what you are doing, so we don't know what you are doing wrong.
Richard MacCutchan 5-Dec-15 6:15am    
There is obviouly an issue with you getting the text from the textbox. In the line ParagraphPDF p1 = new ParagraphPDF(textFromTextbox, infoFont);, where do you set the contents of textFromTextbox?
PeMaCN 5-Dec-15 6:37am    
I'm getting the content of textbox from another method because I have a button save which saves text from textbox into a string and also button erase which erases text from textbox.
Richard MacCutchan 5-Dec-15 8:24am    
Then please show us the code; we cannot guess what is happening.
Afzaal Ahmad Zeeshan 5-Dec-15 7:30am    
Set it to null and restart?

Put break-points in your code: when you hit the break-points single-step through your code and examine the values of the variables. Use F11 to single-step in Visual Studio.

Either you are over-writing the file, or you are not accessing the changed content in the TextBox. This should be simple to figure out.
C#
if (pdfFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// put a break-point here: single-step forward
       Document document = new Document(iTextSharp.text.PageSize.A4, 40, 40, 50, 50);
       PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfFile.FileName, FileMode.Create));
       document.Open();
 
       document.NewPage();
       ParagraphPDF title = new ParagraphPDF("AAA", titleFont);
       title.Alignment = Element.ALIGN_CENTER;
       document.Add(title);
       document.Add(Chunk.NEWLINE);
 
       ParagraphPDF p1 = new ParagraphPDF(textFromTextbox, infoFont);
// put a break-point here: single-step forward                   
       document.Add(p1);              
// put a break-point here: single-step forward 
       document.Close();
}
Also, put a break-point just after this code returns.
 
Share this answer
 
v2
You can modify the property of SaveFileDialog.FileName.

—SA
 
Share this answer
 
v2
Comments
PeMaCN 5-Dec-15 5:47am    
Can you please explain this 'questionable'?
Sergey Alexandrovich Kryukov 5-Dec-15 5:50am    
Ah, sorry, I probably misread it. I thought that you need to save two files each time, but probably it's just your concern that the file name is not reset.
—SA
PeMaCN 5-Dec-15 5:56am    
I tried this: pdfFile.FileName = "" and pdfFile.RestoreDirectory = true but none of this worked
Sergey Alexandrovich Kryukov 5-Dec-15 6:00am    
I cannot believe that. What happens if you use pdfFile.FileNames = string.Empty; ?
(Using "" is not a bug, but using string.Empty is better for maintainability of code.)

Also, SaveFileDialog has the option for "file already exist" warning.

—SA
PeMaCN 5-Dec-15 6:17am    
I also tried Reset() but this didn't worked either.

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