开发者

PDFBOX.net PDDocument Object into a database / serialization

开发者 https://www.devze.com 2023-01-29 06:39 出处:网络
I hope some can shed some light on this. It looks like I need to recompile PDFBOX with the [SERIALIZE] option. But I have no idea how to.

I hope some can shed some light on this. It looks like I need to recompile PDFBOX with the [SERIALIZE] option. But I have no idea how to.

What I am t开发者_StackOverflow社区rying to do is store PDDocument doc object inside a database.

ERROR: Type 'org.apache.pdfbox.pdmodel.PDDocument' in Assembly 'PDFBox, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

    PDDocument doc = (PDDocument)documents.get(d);

    string str = ConvertForWrite(doc);

    /// Convert any object into a string which can be store in a database
    public  string ConvertForWrite(object obj)
    {
        MemoryStream memoryStream = new MemoryStream();
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        binaryFormatter.Serialize(memoryStream, obj);
        string str = System.Convert.ToBase64String(memoryStream.ToArray());
        return str;
    }

Thank you, Gavin


I doubt PDFBox is easy to transform into a fully serializable library.

It won't suffice to simply add some option: While many of its classes are simple enough, others reference temporary files ("scratch files") carrying their actual contents. Custom serialization code, therefore, would be necessary in those classes.

The most natural way to serialize the data represented by a PDDocument instance and de-serialize it again would be to... save it as PDF document and read that document into a PDDocument instance! Using memory streams this does not require actual file system files.

Thus, you might want to create a wrapper class for PDDocument using Custom Serialization techniques which (de-)serialize by saving to / reading from a PDF document.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号