如何使用iTextSharp将文本和图像导出为PDF?

时间:2013-05-19 08:41:31

标签: c# itextsharp rtf richtext

我遇到iTextSharp问题并导出到.PDF

我正在使用iTextSharp 5.1.2.0。但问题是,当我尝试使用引用图像的RTF片段导出一个时,图像不会显示。

此外,如果我在文档中插入任何格式,则会显示丰富的格式代码,而不是实际显示带格式的解释文本。由于图像不会导出,并且文档没有保留其格式,因此我认为代码存在问题,不允许将其保存为富文本文档。

我将文档导出为PDF的代码如下:

SaveFileDialog dlg = new SaveFileDialog();
        dlg.Filter = "Portable Document Format (.pdf)|*.pdf";
        dlg.FilterIndex = 0;
        iTextSharp.text.Document myDocument = new
        iTextSharp.text.Document(PageSize.A4);
        DialogResult dialog = dlg.ShowDialog();
        if (dialog == DialogResult.OK)
            try
            {
                fileName = dlg.FileName;
                PdfWriter.GetInstance(myDocument, new FileStream(dlg.FileName, FileMode.Create));
                myDocument.Open();
                myDocument.Add(new iTextSharp.text.Paragraph(richTextBoxPrintCtrl1.Rtf));
            }
            catch (Exception exc)
            {
                MessageBox.Show("Error exporting to PDF. Please try again. \n \n" + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        myDocument.Close();
    }

请参阅附图中的示例。

感谢。

Within the application (Formatting)

Exported to PDF file

0 个答案:

没有答案
相关问题