PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 11:07 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Jan 05, 2023 3:44 pm 
Offline

Joined: Thu Jul 02, 2020 3:16 pm
Posts: 12
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 05, 2023 5:04 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 05, 2023 6:32 pm 
Offline

Joined: Thu Jul 02, 2020 3:16 pm
Posts: 12
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!!!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 144 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group