PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Merge pdf files and choose first file
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4411
Page 1 of 1

Author:  JRDumont [ Thu Jan 05, 2023 3:44 pm ]
Post subject:  Merge pdf files and choose first file

Hello,

I have been using PDFSharp for several years in my code and it's working perfectly.
Basically, I merge all pdf files in a folder into a single file.

Is it possible to specify which file should be first, and the others can be random?

Here is my current code.

Sub PDFMerge(ByRef FolderName As String, ByRef assyName As String)

Dim MergePDFFolderName As String = FolderName
Dim MergePDFFileName As String = assyName
Dim files As String() = IO.Directory.GetFiles(MergePDFFolderName, "*.pdf")
Dim outputDocument As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument()

For Each file As String In files
MsgBox(file)
Dim inputDocument As PdfSharp.Pdf.PdfDocument = PdfSharp.Pdf.IO.PdfReader.Open(file, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import)
Dim count As Integer = inputDocument.PageCount
For idx As Integer = 0 To count - 1
Dim page As PdfSharp.Pdf.PdfPage = inputDocument.Pages(idx)
outputDocument.AddPage(page)
Next
Next

Dim filename As String = MergePDFFileName & ".pdf"

outputDocument.Save(filename)
'Process.Start(filename)
End Sub

Author:  TH-Soft [ Thu Jan 05, 2023 5:04 pm ]
Post subject:  Re: Merge pdf files and choose first file

JRDumont wrote:
Is it possible to specify which file should be first, and the others can be random?
With respect to your code: Simply sort the "files" object, making sure the first entry is the file you need to have first.

Author:  JRDumont [ Thu Jan 05, 2023 6:32 pm ]
Post subject:  Re: Merge pdf files and choose first file

Hi Thomas,

Thank you for the quick reply.
I thought of sorting but I can not always be sure the sorting will give the correct order.
However, I added an "_" as a prefix to the file name I would like to be first, and now the sorting works.
I just hope no other files already have an "_" as a prefix...lol

Thank you again!!!

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/