简体   繁体   English

我可以将现有的iTextSharp文档转换为字节数组吗

[英]Can I convert a pre-existing iTextSharp Document to a byte array

There a number of solutions for creating an iTextSharp document and then returning the memory stream object associated with the creation of that document but is there a way to convert a pre-existing iTextSharp document to a memory stream or byte array? 有多种解决方案可用于创建iTextSharp文档,然后返回与该文档的创建相关联的内存流对象,但是有没有办法将先前存在的iTextSharp文档转换为内存流或字节数组?

// convert the PDF Document to a byte array 
public byte[] ToByte (Document pdf_doc)
{
    byte[] rtn_array = null;
    **// what goes here, something using PdfReader ???**
    return rtn_array;
}

is there a way to convert a pre-existing iTextSharp document to a memory stream or byte array? 有没有办法将现有的iTextSharp文档转换为内存流或字节数组?

No. 没有。

In the architecture of iText(Sharp) before version 7 Document is a mere facade class that knows some formatting information (default page size, margins, ...) and can be given abstract document parts (paragraphs, tables, ...) which it forwards to registered listener classes which make them entities of the output format and write them out to a stream and immediately mostly forget about them. 在版本7之前的iText(Sharp)的体系结构中, Document是一个仅有的外观类,它知道一些格式设置信息(默认页面大小,边距等),并且可以为其提供抽象的文档部分(段落,表格等),它转发给已注册的侦听器类,这些类使它们成为输出格式的实体,并将它们写出到流中,并立即几乎忘记了它们。

(Before version 5 the iText distribution also contained classes for RTF creation! But even for versions 5.x you could register your own listeners to transform the abstract document parts to content of your choice.) (在版本5之前,iText发行版还包含用于创建RTF的类!但是即使对于版本5.x,您也可以注册自己的侦听器,以将抽象文档部分转换为您选择的内容。)

Thus, serializing and deserializing an iText Document (if it were possible) effectively only could restore some abstract document format and not any concrete content. 因此,有效地序列化和反序列化iText Document (如果可能)只能恢复某些抽象文档格式,而不能恢复任何具体内容。 But even that is not supported. 但是,即使那样也不被支持。


In contrast to libraries with Document classes that keep a model of the whole content in memory, iText is designed for high throughput, large size, low footprint server applications. 与带有Document类的库将整个内容的模型保存在内存中相比,iText专为高吞吐量,大尺寸,低占用空间的服务器应用程序而设计。

You can compare this to the difference between XML DOM versus XML SAX; 您可以将其与XML DOM与XML SAX之间的区别进行比较。 iText can be compared to SAX (or StAX) where only very little of the document is known by the library in contrast to DOM where there is a full-blown document representation in memory. 可以将iText与SAX(或StAX)进行比较,在SAX(或StAX)中,库仅了解很少的文档,而DOM在内存中具有完整的文档表示形式。


What you can do, of course, is to have the registered PdfWriter to write to a MemoryStream which you can later on read by a PdfReader and manipulate by a PdfStamper . 你能做什么,当然是有登记的PdfWriter写入一个MemoryStream ,你以后可以通过阅读PdfReader ,并通过操纵PdfStamper But I assume that is not what you were thinking about. 但是我认为那不是您的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM