Question: I have the following code (C#) for generating a PDF using iTextSharp. This code currently svaes thePDF however I need it to automatically name the

I have the following code (C#) for generating a PDF using iTextSharp. This code currently svaes thePDF however I need it to automatically name the filename to the contents in txtID.Text. For example if the contents added in the txtID.Text is E1234, I want the file to automatically add the E1234 to the filename so the file will be E1234.pdf. I have tried a couple of things but with no luck.

Here is my current code without any auto naming included:

private void btnCreatePDF_Click(object sender, EventArgs e)//transfers data displayed in txtDisplay.Text into a PDF { using (SaveFileDialog sfd = new SaveFileDialog()

{ Filter = "PDF file|*.pdf", ValidateNames = true }) { if (sfd.ShowDialog() == DialogResult.OK) { iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4.Rotate()); try { PdfWriter.GetInstance(doc, new FileStream(sfd.FileName, FileMode.Create)); doc.Open(); doc.Add(new iTextSharp.text.Paragraph(txtDisplay.Text)); } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);

} finally { doc.Close(); } }

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!