简体   繁体   中英

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?

// 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?

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.

(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.)

Thus, serializing and deserializing an iText Document (if it were possible) effectively only could restore some abstract document format and not any concrete content. 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.

You can compare this to the difference between XML DOM versus 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.


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 . But I assume that is not what you were thinking about.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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