Platforms to show: All Mac Windows Linux Cross-Platform
/DynaPDF/Merge PDF documents with page numbers
Required plugins for this example: MBS DynaPDF Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/Merge PDF documents with page numbers
This example is the version from Sun, 18th Jun 2022.
Project "Merge PDF documents with page numbers.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
EventHandler Sub Change()
CombineButton.Enabled = Me.ListCount > 0
End EventHandler
EventHandler Sub DropObject(obj As DragItem, action As Integer)
If obj.FolderItemAvailable Then
Do
add obj.FolderItem
Loop Until Not obj.NextItem
End If
End EventHandler
EventHandler Sub Open()
me.AcceptFileDrop MyFileTypes.Pdf
End EventHandler
End Control
Control AddButton Inherits PushButton
ControlInstance AddButton Inherits PushButton
EventHandler Sub Action()
Dim f As FolderItem = GetOpenFolderItem(MyFileTypes.Pdf)
if f<>Nil then
add f
end if
End EventHandler
End Control
Control CombineButton Inherits PushButton
ControlInstance CombineButton Inherits PushButton
EventHandler Sub Action()
Merge
End EventHandler
End Control
Control CheckPageNumbers Inherits CheckBox
ControlInstance CheckPageNumbers Inherits CheckBox
End Control
Sub Add(file as FolderItem)
List.AddRow file.DisplayName
List.Celltag(List.LastIndex,0) = file
CombineButton.Enabled = True
End Sub
Sub AddPageNumbers(pdf as DynaPDFMBS)
Const FontStyle = pdf.kfsNone
Const FontSize = 20
// we add pages on bottom, so we can use Bottom Up coordinates
Call pdf.SetPageCoords(pdf.kpcBottomUp)
// loop over pages
Dim c As Integer = pdf.GetPageCount
For i As Integer = 1 To c
// we try to edit each page
If pdf.EditPage(i) Then
Dim PageText As String = "Page "+Str(i)+" of "+Str(c)
// draw white rectangle to overdraw existing page numbers
Call pdf.SetFillColor(pdf.RGB(255, 255, 255)) // white color
Call pdf.Rectangle(pdf.GetPageWidth-250, 40-FontSize-5, 200, FontSize+8, pdf.kfmFill )
// setup font and color
Call pdf.SetFont("Helvetica", FontStyle, FontSize)
Call pdf.SetFillColor(0) // black color
Call pdf.SetStrokeColor(0)
// and write text into a right aligned text box on bottom of page with some margin
Call pdf.WriteFTextEx(pdf.GetPageWidth-250, 40, 200, 40, pdf.ktaRight, PageText )
// save page
Call pdf.EndPage
End If
Next
End Sub
Sub Merge()
Dim pdf As New DynapdfMBS
pdf.SetLicenseKey "Lite" // For this example you can use a Lite, Pro or Enterprise License
Dim outFile As folderitem = GetSaveFolderItem(MyFileTypes.Pdf, "Merge.pdf")
If outFile = Nil Then Return // cancelled
Call pdf.CreateNewPDF(outFile)
// we import all content and as pages
Dim flags As Integer = Bitwise.BitOr(pdf.kifImportAsPage, pdf.kifImportAll)
Call pdf.SetImportFlags(flags)
// loop over list to import all files there
Dim c As Integer = List.ListCount-1
For i As Integer = 0 To c
// we store folderitems for file references in the CellTag here.
Dim file As FolderItem = List.CellTag(i, 0)
// open file
Dim FileHandle As Integer = pdf.OpenimportFile(file, pdf.kptopen, "")
If FileHandle >= 0 Then
// import all pages
Call pdf.ImportPDFFile(pdf.GetPageCount+1, 1.0, 1.0)
// alternatively with Pro license, import individual pages
'Call pdf.ImportPDFPage(1)
Call pdf.CloseImportFile
End If
Next
// optional edit all pages to have new text for page numbers
If CheckPageNumbers.Value Then
AddPageNumbers pdf
End If
Call pdf.closefile
// open in preview
outFile.Launch
End Sub
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
MyFileTypes
Filetype application/pdf
End MyFileTypes
End Project
The items on this page are in the following plugins: MBS DynaPDF Plugin.