iTextSharp的 - 如何生成一个文件,在剪贴板中的RTF文件,而不是文件、剪贴板、而不是、iTextSharp

2023-09-06 08:24:43 作者:繁华落尽丶曲终散ァ

我想用iTextSharp的库,可以复制到剪贴板中生成PDF或RTF文件,使用相同的code我用它来生成一个文件(的FileStream)的文件。

I would like to generate a PDF or RTF document using iTextSharp library that can be copied to the clipboard, using the same code I use to generate the document on a file (FileStream).

这样我的应用程序会给用户提供两种选择:生成到一个文件或复制到剪贴板

This way my application would give the user two options: generate to a file or to the clipboard.

推荐答案

基本上每个iTextSharp的文件附加到的System.IO.Stream

Basically every iTextSharp document is attached to a System.IO.Stream.

Document doc = new Document(PageSize.A4);
RtfWriter2.GetInstance(doc, stream);

通常我们的文档保存到一个文件中,使用的FileStream 。要使用相同的code粘贴在文档中的剪贴板中,我们使用了的MemoryStream 代替。

Usually we save the document to a file, using FileStream. To use the same code to paste the document in the Clipboard, we use a MemoryStream instead.

MemoryStream stream = new MemoryStream();
Document doc = new Document(PageSize.A4);
RtfWriter2.GetInstance(doc, stream);

// (...) document content

doc.Close();

string rtfText = ASCIIEncoding.ASCII.GetString(stream.GetBuffer());
stream.Close();

Clipboard.SetText(rtfText, TextDataFormat.Rtf);

我只有问题图片:似乎iTextSharp的出口保存图像的字节后, \ BIN 标记的图像。一些图书馆把二进制内容EN codeD为十六进制字符。当我在Word中粘贴(从内存中),图像不会出现,但如果我从文件加载,一切都OK。有什么建议?

I only had problems with Images: It seems that iTextSharp exports images saving the bytes of the image after the \bin tag. Some libraries put the binary content encoded as hex characters. When I paste (from memory) in Word, the images won't appear, but if I load from a file, everything is OK. Any suggestions?