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


1 Answer 257 Views
This is a migrated thread and some comments may be shown as answers.
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
      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)
  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)
      Return b
  End Function

1 Answer, 1 is accepted

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

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
            End Using
    End Using
End Sub
Our XAML SDK repository on GitHub contains more examples showing the capabilities of PdfStreamWriter:
 - ManipulatePages
 - PdfStreamWriterPerformance

Hope this helps.

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.
Asked by
Top achievements
Rank 1
Answers by
Telerik team
Share this question