Search code examples
c#visual-studiovisual-studio-2017

How to allow user choose save location but not filename with dialog box C#


I've made a tax calculator (all theoretical for practise) and it has a function whereby the user can press a button to export a basic PDF tax report. There are slightly different requirements to the tax report depending on whether or not it is an employee or a contractor. Contractors are denoted by having a EmployeeID start with a "C". So far all good.

Now I have a requirement for only contractors that the contractor PDF is saved with a specific name "ContractorTax". Is there a way to have a safe file dialog that only lets users select a location while having a locked file name (and I guess file extension).

This is the relevant code I have so far:

   //Creates PDF.
        private void CreatePdf()
        {           
            //sets default options for save file
            saveFileDialog1.FileName = "Tax Report for " + txtFirstName.Text + " " + txtSurname.Text;
            saveFileDialog1.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
            saveFileDialog1.DefaultExt = "pdf";
            saveFileDialog1.ShowDialog();

            /*- create FileStream object (fs) with pdf name, mode type, access type, and if it can be shared 
              - create new document object (A4 page size)
              - create a iTextSharp.text.pdf.PdfWriter object, it helps to write the Document to the Specified
                FileStream 
              - open document
              - add paragraph
              - close document*/

            if (txtEmployeeID.Text.ToUpper().StartsWith("C"))
            {
                FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write, FileShare.None);
                Document doc = new Document(PageSize.A4);
                PdfWriter writer = PdfWriter.GetInstance(doc, fs);
                doc.Open();

                doc.Add(new Paragraph("TAX REPORT" + nl + nl + "Employee ID: " + txtEmployeeID.Text.ToUpper() + nl + "Name: " + txtFirstName.Text + " " + txtSurname.Text + nl + "Mobile Number: " + txtMobileNumber.Text + nl +
                        "Email: " + txtEmail.Text + nl + "Department: " + comboBoxDepartment.Text + nl + "Salary: $" + salary.ToString() + nl + "Tax Payable: $" + taxPayable.ToString()));
                doc.Close();

            }
            else
            {
                FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write, FileShare.None);
                Document doc = new Document(PageSize.A4);
                PdfWriter writer = PdfWriter.GetInstance(doc, fs);
                doc.Open();

                doc.Add(new Paragraph("TAX REPORT" + nl + nl + "Employee ID: " + txtEmployeeID.Text.ToUpper() + nl + "Name: " + txtFirstName.Text + " " + txtSurname.Text + nl + "Mobile Number: " + txtMobileNumber.Text + nl +
                        "Department: " + comboBoxDepartment.Text + nl + "Salary: $" + salary.ToString() + nl + "Tax Payable: $" + taxPayable.ToString()));
                doc.Close();
            }



        }

I found this question asked before but the answer was only relevant for Java and I'm using visual studio 2017 and coding in C#.


Solution

  • For your question, You want the name of the employer's tax receipt to be specified and cannot be changed.

    You could try the following code to get it.

    private void CreatePdf()
        {         
            string path1 = "ContractorTax.pdf";
            if (txtEmployeeID.Text.ToUpper().StartsWith("C")) 
            {
    
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.ShowDialog();
                string path2 = Path.Combine(dialog.SelectedPath, path1);
                FileStream fs = new FileStream(path2, FileMode.Create, FileAccess.Write, FileShare.None);
                Document doc = new Document(PageSize.A4);
                PdfWriter writer = PdfWriter.GetInstance(doc, fs);
                doc.Open();
    
                doc.Add(new Paragraph("TAX REPORT" + nl + nl + "Employee ID: " + txtEmployeeID.Text.ToUpper() + nl + "Name: " + txtFirstName.Text + " " + txtSurname.Text + nl + "Mobile Number: " + txtMobileNumber.Text + nl +
                        "Email: " + txtEmail.Text + nl + "Department: " + comboBoxDepartment.Text + nl + "Salary: $" + salary.ToString() + nl + "Tax Payable: $" + taxPayable.ToString()));
                doc.Close();         
                MessageBox.Show("success");
            }
            else
            {
                saveFileDialog1.FileName = "Tax Report for " + txtFirstName.Text + " " + txtSurname.Text;
                saveFileDialog1.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
                saveFileDialog1.DefaultExt = "pdf";
                saveFileDialog1.ShowDialog();
    
                FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write, FileShare.None);
                Document doc = new Document(PageSize.A4);
                PdfWriter writer = PdfWriter.GetInstance(doc, fs);
                doc.Open();
    
                doc.Add(new Paragraph("TAX REPORT" + nl + nl + "Employee ID: " + txtEmployeeID.Text.ToUpper() + nl + "Name: " + txtFirstName.Text + " " + txtSurname.Text + nl + "Mobile Number: " + txtMobileNumber.Text + nl +
                        "Department: " + comboBoxDepartment.Text + nl + "Salary: $" + salary.ToString() + nl + "Tax Payable: $" + taxPayable.ToString()));
                doc.Close();
            }
    
    
    
        }
    

    Hope this could help you.