locked
Stream and save PDF RRS feed

  • Question

  • User1510859543 posted

    We have the code below that pulls images from another website, streams them one by one and then saves each image.  Now we would like to stream PDF files and wonder what we need to change to allow for saving a PDF.  Below is a sample of the code we use to stream the jpeg, etc. files.

                            Dim bys As Task(Of Byte())
                            Dim stream As MemoryStream
                            While intCol < 13
                                    bys = client.GetByteArrayAsync(strwebpath)
                                    'bys.Result   get the Byte[] of image from the result
                                    stream = New MemoryStream(bys.Result)
                                    If Right(strwebpath, 3).ToLower <> "pdf" Then
                                        Using bmpImage As Bitmap = New Bitmap(stream)
                                            bmpImage.Save(Server.MapPath(strSavePath))
                                        End Using
                                    Else
    
                                    End If
    
                                    intCol += 1
                                    intImages += 1
    
                                    If Len(currentRow(intCol)) > 0 Then
                                        strImageFile = currentRow(intCol)
                                        strwebpath = strBasePath & strImageFile
                                        strSavePath = strSaveBase & strImageFile
                                    Else
                                        strwebpath = ""
                                        strSavePath = ""
                                    End If
    
                            End While
    

    Monday, November 11, 2019 6:14 PM

Answers

  • User753101303 posted

    Seems to me it could be something such as :

    content = client.GetByteArrayAsync(strwebpath).Result ' You can't use await?
    System.IO.File.WriteAllBytes(Server.MapPath(strSavePath),content)

    My understanding is that your download method returns a byte array you could save on disk. There is no need to use a Bitmap or PDF document class if you just want to save what you got from the 3rd party site wihtout any change...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 12, 2019 3:21 PM

All replies

  • User288213138 posted

    Hi dlchase,

    Dim bys As Task(Of Byte())
                            Dim stream As MemoryStream
                            While intCol < 13
                                    bys = client.GetByteArrayAsync(strwebpath)
                                    'bys.Result   get the Byte[] of image from the result
                                    stream = New MemoryStream(bys.Result)
                                    If Right(strwebpath, 3).ToLower <> "pdf" Then
                                        Using bmpImage As Bitmap = New Bitmap(stream)
                                            bmpImage.Save(Server.MapPath(strSavePath))
                                        End Using
                                    Else
    
                                    End If
    
                                    intCol += 1
                                    intImages += 1
    
                                    If Len(currentRow(intCol)) > 0 Then
                                        strImageFile = currentRow(intCol)
                                        strwebpath = strBasePath & strImageFile
                                        strSavePath = strSaveBase & strImageFile
                                    Else
                                        strwebpath = ""
                                        strSavePath = ""
                                    End If
    
                            End While

    Since there are many variables in your code that I don't know about, I can't use your code.

    But if you save a PDF file from stream, you can try below code:

    //New a PDF instance.
    PdfDocument doc = new PdfDocument();
    
    //Load PDF file from stream.
    FileStream from_stream = File.OpenRead("sample.pdf");
    doc.LoadFromStream(from_stream);
    
    //Save the PDF document.
    doc.SaveToFile("From_stream.pdf",FileFormat.PDF);
    System.Diagnostics.Process.Start("From_stream.pdf");

    More information about save a PDF file from stream you can refer to this link:  Save a PDF file from Stream in C#

    Best regards,

    Sam

    Tuesday, November 12, 2019 7:18 AM
  • User1510859543 posted

    Your code sample uses a 3rd party tool and I prefer to stay away from that.  I do, however, use iTextSharp.dll in some places.

    I already have the pdf file inside the stream variable so why can't I just use some method of saving that stream as a pdf file?

    Tuesday, November 12, 2019 2:23 PM
  • User475983607 posted

    I already have the pdf file inside the stream variable so why can't I just use some method of saving that stream as a pdf file?

    That's a good question.  Why can't you use the same concept?  Did you try?  Where is the code?  Are you receiving an error?  What troubleshooting step s have you preformed?

    Tuesday, November 12, 2019 2:29 PM
  • User753101303 posted

    Hi,

    If you start from a byte array you could use https://docs.microsoft.com/en-us/dotnet/api/system.io.file.writeallbytes?view=netframework-4.8

    You could also perhaps copy the response stream to a file stream more directly using "WriteTo" or "CopyTo".

    For your current code, going through a Bitmap class doesn't seems needed at all (you are just doing a copy, it would be usefull if you really wanted to process this content as a bitmap ie altering the image for example).

    Edit: exactyly as you don't need a 3rd party tool if you just want to copy the file content rather than really processing the PDF file to change its content etc...

    Tuesday, November 12, 2019 2:51 PM
  • User1510859543 posted

    Below is the full code where I am trying to save the memory stream to a PDF.  The PDF is read from a remote server and I want to save it to the current server (strSavePath variable). I want to save it where the code "Dim doc As PdfDocument = New PdfDocument()" is.

                        'pull in photos and create in customer photos folder
                        Dim strImageFile As String = ""
                        Dim intCol As Int32 = 7
                        If Len(currentRow(intCol)) > 0 Then
                            strImageFile = currentRow(intCol)
    
                            'get or create customer photos path
                            Dim strSaveBase As String = UtilClass.GetPhotosPath(intRecordID, True, True)
                            Dim strSavePath As String = ""
                            If Right(strSaveBase, 1) <> "/" Then
                                strSaveBase &= "/"
                            End If
                            strSavePath = strSaveBase & strImageFile
    
                            Dim client As HttpClient = New HttpClient()
    
                            Dim strwebpath As String = ""
                            Dim strBasePath As String = "http://www.fixit.com/uploads/"
                            strwebpath = strBasePath & strImageFile
    
                            Dim bys As Task(Of Byte())
                            Dim stream As MemoryStream
                            While intCol < 13
                                If strwebpath <> "" Then
                                    bys = client.GetByteArrayAsync(strwebpath)
                                    'bys.Result   get the Byte[] of image from the result
                                    stream = New MemoryStream(bys.Result)
                                    If Right(strwebpath, 3).ToLower <> "pdf" Then
                                        Using bmpImage As Bitmap = New Bitmap(stream)
                                            bmpImage.Save(Server.MapPath(strSavePath))
                                        End Using
                                    Else
                                        Dim doc As PdfDocument = New PdfDocument()
                                        Dim myPDFWriter As PdfWriter = PdfWriter.GetInstance(doc, stream)
    
                                    End If
    
                                    intCol += 1
                                    intImages += 1
    
                                    If Len(currentRow(intCol)) > 0 Then
                                        strImageFile = currentRow(intCol)
                                        strwebpath = strBasePath & strImageFile
                                        strSavePath = strSaveBase & strImageFile
                                    Else
                                        strwebpath = ""
                                        strSavePath = ""
                                    End If
                                Else
                                    intCol += 1
                                End If
    
                            End While
    
                            client.Dispose()
                            stream.Close()
                        End If

    Tuesday, November 12, 2019 3:02 PM
  • User753101303 posted

    Seems to me it could be something such as :

    content = client.GetByteArrayAsync(strwebpath).Result ' You can't use await?
    System.IO.File.WriteAllBytes(Server.MapPath(strSavePath),content)

    My understanding is that your download method returns a byte array you could save on disk. There is no need to use a Bitmap or PDF document class if you just want to save what you got from the 3rd party site wihtout any change...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 12, 2019 3:21 PM
  • User1510859543 posted

    Yes, the files on the remote site are existing image or document files. The process downloads (streams) both image files and documents so I thought that I needed a bitmap variable to save it as an image. If I can use just one process that would be ideal.  Are you saying that the 2 lines of code you gave will work on any type of file?

    And is "content" a variable of some type?

    Tuesday, November 12, 2019 3:31 PM
  • User753101303 posted

    It's just the byte array you got from the other site and which is the content (image, pdf, video, javascript, Autocad, Mdf etc...) that you want to save unchanged on disk.

    It's the same than downloading a file to a browser or doing a file copy to some target location. "Understanding" the file content is not needed if the only thing you want is doing an exact copy.

    You'll need to load the file into an application (or programmatically using  a library) only if you want to do something that would change this content.

    Tuesday, November 12, 2019 3:54 PM
  • User1510859543 posted

    Yes, what we are doing is just a "copy" of each file name from an array. Good to know.

    Tuesday, November 12, 2019 3:59 PM