I am having a hard time finding solution to this problem, I am trying to save a PDF file using iText Sharp in Windows Form Application however everytime I try to save the file I have to feed the file name manually, What i want to do is that when "Save As" Dialog Box appears the file name should auto populate by concatenating values from 3 different textboxes as a file name.
For eg. File Name:
CID1CON4INV125 (CID stands for Customer ID which is 1, CON stands for Contract ID which is 4, INV stands for Invoice ID which is 125 and is unique) Above example may be very tedious to remember if i have to chose a file name everytime, is there a way that i can automate this process by taking the file name from txtCustomerID.Text and txtContractID.Text and txtInvoiceID.Text and prepare a file name to be saved as PDF.
Thanks.
VulpesPosted Feb 3, 2015, 4:06 PM
Haneef AhmadPosted Feb 3, 2015, 1:37 PM
VulpesPosted Feb 3, 2015, 12:21 PM
If you already have a definite file name do you need to use the SaveFileDialog at all? Couldn't you just save it directly to disk using that name?
EDIT:
In your last post, you're missing the line:
dlg.FileName = fileName;
before you call ShowDialog()
Haneef AhmadPosted Feb 3, 2015, 12:15 PM
//if (!Directory.Exists(folderPath))
//{
// Directory.CreateDirectory(folderPath);
//}
//using (FileStream stream = new FileStream(folderPath + "CON" + txtContractID.Text + "CID" + txtCustomerID.Text + "INV" + txtInvoiceNo.Text + ".pdf", FileMode.Create))
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "PDF|*.pdf";
dlg.FilterIndex = 0;
string fileName = string.Format("CID{0}CON{1}INV{2}.pdf", txtCustomerID.Text, txtContractID.Text, txtInvoiceNo.Text);
if (dlg.ShowDialog() == DialogResult.OK)
{
fileName = dlg.FileName;
File.WriteAllText(Name, "test");
The commented codes actually puts a file in the folder by taking the values from the textbox however solution you proposed does not work.
Haneef AhmadPosted Feb 3, 2015, 12:07 PM
VulpesPosted Feb 3, 2015, 7:22 AM
saveFileDialog.FileName = string.Format("CID{0}CON{1}INV{2}.pdf", txtCustomerID.Text, txtContractID.Text, txtInvoiceID.Text);