This is a migrated thread and some comments may be shown as answers.

PdfStreamWriter

1 Answer 309 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 02 Jul 2019, 06:12 PM

I'm trying to write a simple script to merge two files together. 

The files that are created show as corrupt. Is there something im doing wrong?

Sub Main()
      Dim file1 As Byte() = ReadFile("e:\test1\test1.pdf")
      Dim result As New IO.MemoryStream
      Dim fileWriter As New PdfStreamWriter(Result, True)
 
      Dim pdfSource As New PdfFileSource(New IO.MemoryStream(file1))
      For Each pg As PdfPageSource In pdfSource.Pages
          fileWriter.WritePage(pg)
      Next
 
      Dim b As Byte()
      b = Result.ToArray()
      Dim writer As IO.FileStream = IO.File.OpenWrite("e:\test1\abc12222.pdf")
      writer.Write(b, 0, b.Length)
      writer.Close()
 
  End Sub
  Function ReadFile(filename As String) As Byte()
      Dim io As New IO.FileInfo(filename)
      Dim fs As IO.FileStream = io.OpenRead
      Dim b() As Byte
      ReDim b(io.Length)
      fs.Read(b, 0, io.Length)
      fs.Close()
      Return b
  End Function

1 Answer, 1 is accepted

Sort by
0
Peshito
Telerik team
answered on 04 Jul 2019, 10:48 AM
Hello,

Please find a sample code for merging files using the PdfStreamWriter class:
Private Shared Sub MergeDifferentDocumentsPages(ByVal resultFileName As String)
    Dim documentsToMerge As String() = {InputFileSoundVideoAnd3D, InputFileBarChart, InputFileCentaurFeatures, InputFileMultipageDocument}
    Dim resultFile As String = Path.Combine(ResultDirName, resultFileName)
 
    Using fileWriter As PdfStreamWriter = New PdfStreamWriter(File.OpenWrite(resultFile))
 
        For Each documentName As String In documentsToMerge
 
            Using fileToMerge As PdfFileSource = New PdfFileSource(File.OpenRead(documentName))
 
                For Each pageToMerge As PdfPageSource In fileToMerge.Pages
                    fileWriter.WritePage(pageToMerge)
                Next
            End Using
        Next
    End Using
End Sub
Our XAML SDK repository on GitHub contains more examples showing the capabilities of PdfStreamWriter:
 - ManipulatePages
 - PdfStreamWriterPerformance

Hope this helps.

Regards,
Peshito
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
PdfProcessing
Asked by
Steven
Top achievements
Rank 1
Answers by
Peshito
Telerik team
Share this question
or