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?