iTextSharp - 如何在剪贴板中生成 RTF 文档而不是文件

发布于 2024-08-06 09:35:26 字数 125 浏览 2 评论 0原文

我想使用 iTextSharp 库生成 PDF 或 RTF 文档,该文档可以复制到剪贴板,使用与在文件 (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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2024-08-13 09:35:26

基本上每个 iTextSharp 文档都附加到一个 System.IO.Stream。

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

通常我们使用FileStream将文档保存到文件中。要使用相同的代码将文档粘贴到剪贴板中,我们使用 MemoryStream 来代替。

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 标记之后保存图像的字节。一些库将二进制内容编码为十六进制字符。当我(从记忆中)粘贴到Word中时,图像不会出现,但如果我从文件加载,一切正常。有什么建议吗?

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

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

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);

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文