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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started