我正在编写一个程序,我需要将文本(从富文本框)保存到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));
自:
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(textBox1.Text, FileMode.Create));
然后它工作正常,但文件没有扩展名.pdf
.
如何使文档的标题灵活,以便用户可以分配它,并且扩展名将被.pdf
?
感谢用户 mkl,我找到了答案。我不得不把:
textBox1.Text + ".pdf"