Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Oct 11, 2018 9:40 am

Hi there,

We just purchased the commercial version, and got the latest Spire.PDF version. But when I tried to use the LoadFromBytes method I got the below exception:

[PdfDocumentException: Invalid/Unknown/Unsupported format]
spr១.ᜀ(Int32 A_0) +213
spr១.ᜀ(String A_0, String A_1) +182
sprៗ..ctor(Stream A_0, spr៚ A_1) +706
spr៚..ctor(Stream A_0, PdfDocumentBase A_1) +204
spr┆.ᜁ(Stream A_0) +725
Spire.Pdf.PdfDocument.LoadFromBytes(Byte[] bytes) +56

What I was trying to pass into the method was a byte array which comes directly from database table. The data is stored as image in database, not sure whether that is reason for the exception. I also tried to convert the byte array to a memory stream, and use LoadFromStream method, still no luck.

Please can you advise how I can get this to work.

Thank you

zhuxi_1986
 
Posts: 5
Joined: Wed Oct 10, 2018 11:07 am

Thu Oct 11, 2018 10:11 am

Hi,

Thanks for your inquiry.
Kindly note that our Spire.PDF is commonly used to operate the Pdf file, you use it to load the data whose format can't be loaded by our product, absolutely, you will get the "Unsupported format" exception. Could you please tell us your specific demand to operate your database data? Then we will provide the solution accordingly.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Thu Oct 11, 2018 11:09 am

Hi,

Thanks for the reply.

I just want to load PDF from the byte array that comes from database, and add header and footer to it, and then saves it to a stream. Below is the code I have got:

PdfDocument doc = new PdfDocument();
doc.LoadFromBytes(result.RenderedDocument);

PdfPageWriter.Initialise(fontPath, imagePath);
PdfPageWriter.DrawHeaderAndFooter(doc.Pages[0]);

pdfStream = new MemoryStream();
doc.SaveToStream(pdfStream);

As you may have realised, 'result.RenderedDocument' is the data from database. The code works perfectly if I have a pdf file and use doc.LoadFromFile(pdfFile). I hope I don't have to transform the byte array to a pdf file first, and then call LoadFromFile to make it work?

zhuxi_1986
 
Posts: 5
Joined: Wed Oct 10, 2018 11:07 am

Fri Oct 12, 2018 3:05 am

Hi,

Thanks for your feedback.
We need your further helps to narrow down the cause of the issue. Please check on your side if the result.RenderedDocument returns the correct bytes which conforms to Pdf format. You could use below code to write the bytes to a Pdf file, if the generated Pdf file has errors, it indicates your issue is related to your bytes itself but not our product, otherwise, please share us with your generated Pdf file for investigation (you could send it to our email [email protected]). Look forward to your reply.
Code: Select all
File.WriteAllBytes(PdfFile, result.RenderedDocument);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Fri Oct 12, 2018 3:38 pm

Hi,

Yes it was to do with the source data. I didn't realise I had to decompress it first. So I changed the code to the following which then worked:

Code: Select all
using (MemoryStream tempStream = new MemoryStream(result.RenderedDocument))
                    {
                        using (GZipStream decompressedStream = new GZipStream(tempStream, CompressionMode.Decompress))
                        {
                            using (var outputStream = new MemoryStream())
                            {
                                decompressedStream.CopyTo(outputStream);

                                doc.LoadFromStream(outputStream);

                                PdfPageWriter.Initialise(fontPath, imagePath);
                                PdfPageWriter.DrawHeaderAndFooter(doc.Pages[0]);

                                resultStream = new MemoryStream();
                                doc.SaveToStream(resultStream);
                                resultStream.Seek(0, 0);
                            }
                        }
                    }


As you can see, after the decompression I had to copy the GZipStream to a MemoryStream, before I call LoadFromStream(MemoryStream). If I pass GZipStream into LoadFromStream(), I get the following exception:

Code: Select all
[NotSupportedException: This operation is not supported.]
   System.IO.Compression.GZipStream.get_Length() +57
   spr┆.ᜀ(Stream A_0) +54
   spr┆..ctor(Stream A_0) +78
   Spire.Pdf.PdfDocument.LoadFromStream(Stream stream) +42


Can you please advise if the above process can be made any simpler?

Thanks

zhuxi_1986
 
Posts: 5
Joined: Wed Oct 10, 2018 11:07 am

Mon Oct 15, 2018 7:48 am

Hello,

Thanks for your feedback and sorry for late reply as weekend.
According to the information you provided, we can know that your issue is not related to our product, your issue is caused either by the data stored in your database itself or the wrong bytes returned by the code (result.RenderedDocument). You need to fix it through checking your data and bytes on your side.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Mon Oct 15, 2018 9:01 am

Hi,

Yes I appreciate it is not to do with your product, and I have posted my changed code that worked fine. I was just wondering if you could let me know if the code can be made simpler with Spire PDF, as at the moment I have to make use of three Stream objects following decompression.

Thanks

zhuxi_1986
 
Posts: 5
Joined: Wed Oct 10, 2018 11:07 am

Mon Oct 15, 2018 9:27 am

Hi,

Thanks for your quick reply.
The current method is the simplest, it can't be simplified anymore.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Mon Oct 15, 2018 9:43 am

Thanks for confirming that. No worries.

zhuxi_1986
 
Posts: 5
Joined: Wed Oct 10, 2018 11:07 am

Mon Oct 15, 2018 9:49 am

Hi,

If you need helps in the future, please do not hesitate to contact us.
Wish you all the best!

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF

cron