C# 如何将文件扩展名.pdf附加到用户输入的字符串中?

C# 如何将文件扩展名.pdf附加到用户输入的字符串中?,c#,C#,我正在编写一个程序,我需要保存文本(从富文本框)到pdf 我使用iTextSharp,这是我的代码: Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35); PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("TitleOfPdf.pdf", FileMode.Create));//textBox1(in the form) ass

我正在编写一个程序,我需要保存文本(从富文本框)到pdf

我使用iTextSharp,这是我的代码:

Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("TitleOfPdf.pdf", FileMode.Create));//textBox1(in the form) assign here the title of the document
doc.Open(); //open the document in order to write inside.
Paragraph paragraph = new Paragraph(richTextBox1.Text);
// Now adds the above created text using different class object to our pdf document
doc.Add(paragraph);
doc.Close();
我想该文件的标题是灵活的,并由用户指定(从文本框)

如果我更改行:

PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("TitleOfPdf.pdf", FileMode.Create));
致:

然后它工作正常,但是文件没有扩展名
.pdf


如何使文档标题灵活,以便用户可以分配它,扩展名将是
.pdf

感谢用户mkl,我找到了Ansare。我不得不说:

textBox1.Text + ".pdf"

你知道你可以连接字符串吗?像
textBox1.Text+“.pdf”
?你甚至可以检查
textBox1.Text
是否以
“.pdf”
结尾,并且只在需要时附加它,这样你就不会得到
filename.pdf.pdf
。一种方法是使用
子字符串(textBox1.Text.Length-4)
,另一种方法是使用常规的espression。哦,文件系统上的文件名可能区分大小写,但为了您自己的心智健全,请检查是否区分大小写。注意
textBox1.Text的空值或空值,或者如果小于4个字符。确切的代码留给读者练习。尽管提到了iTextSharp,但这个问题不是关于pdf创建,而是关于初级C#编程。但是如果用户已经将
filename.pdf
放在文本框中怎么办?然后您的文件将是
filename.pdf.pdf
textBox1.Text + ".pdf"