C# iTextSharp - 如何将文档转换为字节 []
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1192384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iTextSharp - How to convert Document to byte[]
提问by Gus Cavalcanti
I need to attach a pdf I created in memory to an email. Attachments can take a stream. So I believe I need to convert a iTextSharp Document object to stream. How can I do that? I tried serializing the Document object to a stream but it is not "marked as serializable".
我需要将我在内存中创建的 pdf 附加到电子邮件中。附件可以采用流。所以我相信我需要将 iTextSharp Document 对象转换为流。我怎样才能做到这一点?我尝试将 Document 对象序列化为流,但它没有“标记为可序列化”。
采纳答案by RichardOD
Look at iText.pdf.PdfWriter. There are methods that take a stream.
看看iText.pdf.PdfWriter。有一些方法可以使用流。
Here's a sample for streaming in ASP.NET- link text
这是在 ASP.NET 中流式传输的示例-链接文本
回答by fubo
Here is a code sample
这是一个代码示例
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
//creating a sample Document
iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 30f, 30f, 30f, 30f);
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, ms);
doc.Open();
doc.Add(new iTextSharp.text.Chunk("hello world"));
doc.Close();
byte[] result = ms.ToArray();
}