Can't fully load PDF

I believe there is a bug with the OpenDocument method. I am storing files in a database and then can read the file into an array of bytes. I am able to save the file to a file in a temp folder and it reads fine with OpenDocument.

However instead of wasting resourses writing/reading a file, I am trying to read the array of bytes directly with OpenDocument. This works perfectly fine on all documents EXCEPT for PDFs. For the PDF’s it knows the number of pages but only is able to display the first page and the remaining pages state “no preview available”. image.png (86.5 KB)

I’m including the code (sorry it’s in VB). The section that is commented out is the part that writes to a file, which works. The last line where it opens the document is the part that only displays the first page.

Public Sub GetFile()
    Dim cnn As SqlConnection
    Dim strSQL As String
    Dim cmdSQL As SqlCommand
    Dim dtrRS As SqlDataReader
    Dim FileName As String = ""

    cnn = New SqlConnection(Session("cnnCUS"))

    Dim data As Byte() = Nothing
    strSQL = "select FileName, FileData, FileType from FileMgr where FileID='" & ViewState("FileID") & "'"
    cmdSQL = New SqlCommand(strSQL, cnn)
    cnn.Open()
    dtrRS = cmdSQL.ExecuteReader
    If dtrRS.Read = True Then
        data = dtrRS("FileData")
        FileName = dtrRS("FileName")
    End If
    dtrRS.Close()
    cnn.Close()

    'get the file extension

    Dim periodPos As Integer = InStrRev(FileName, ".",, CompareMethod.Text)
    Dim ext As String = Right(FileName, Len(FileName) - periodPos)

    'Dim strUploadPath As String = "~/Temp/"
    'File.WriteAllBytes(Server.MapPath(strUploadPath) + FileName, data)
    'ctlDoc.OpenDocument(Server.MapPath(strUploadPath) + FileName)
    'If System.IO.File.Exists(Server.MapPath(strUploadPath) + FileName) Then System.IO.File.Delete(Server.MapPath(strUploadPath) + FileName)

    ctlDoc.OpenDocument(data, ext)
End Sub

@RJT,

Thanks for contacting us. Could you be so kind as to send a document sample that presents this issue?

Also, I want to repeat the problem to clarify I understood it correctly.

Reading the bytes of PDF documents fails, only rendering the first page. Is this correct?

Sanitary (002).pdf (197.4 KB)
Attached is a copy of the same file in the example. I does the same thing with several other PDFs too. From my tests here’s what I found so far:

  • Taking the data from the database into an array of bytes and storing it as a file works. I can manually open the PDF in windows and also view the file in OpenDocument.
  • Taking the array of bytes and directly trying directly using OpenDocument will read only the first page and then show the “new preview available” for the other pages. This is only for PDF. I have tried with xlsx and docx and they work fine.

The sample code should almost work for you if you just convert it to c#. You’ll just need provide a better connection string and tweek the SQL for your database.

Thanks!

1 Like

@RJT,

A very important questiong i forgot to ask is what version are you using.

The .NET Framework or the .Net Core?

.net framework. 4.8

@RJT,

I tested it multiple times, and I was able to replicate the issue. I have informed this to the dev team, and they will work on this to fix it.

There are 2 workarounds so far:

Number one:

byte[] file = File.ReadAllBytes(filePath);
Stream stream = new MemoryStream(file);
FileInfo fi = new FileInfo(filePath);
var viewToken = ctlDoc.OpenDocument(stream, fi);

If you use a Stream and FileInfo, the issue will not happen.

Number two:
Transform the stream into a file, save it to disk, and use it directly.

I think the first option is the one that will be the best one for you.

Let me know if I can assist you with anything else.

Thanks for the update. I think I will have to keep extracting the file from the database and save it to disk and then just open it with ctlDoc.OpenDocument(filename).

With your example using the stream, it looks like you still have to obtain the FileInfo from a file anyway so it doesn’t seem to make sense to use this solution which I can just open the file directly. I don’t see a way to get the fileinfo from the array of bytes since it looks like FileInfo is looking for a physical file.

I guess I’ll keep using the “inefficient” method until a fix for this is released. :wink:

Thanks for your help!

@RJT,

You are right! I was so excited to find a workaround that I did not noticed the most evident problem with this suggestion.

I hope you can manage to do this only for PDF so you do not have to convert everything. I will keep you posted when the new version is around.