« MBS Xojo Plugins, ver… | Home | Sending email via gma… »

Merge PDFs with page numbers in Xojo

You can use DynaPDFMBS class in MBS Xojo DynaPDF Plugin for a lot of PDF operations. Create PDF from scratch, add content like text, tables, vector graphics, images and place existing PDF content as templates. You can merge, split, encrypt, decrypt, optimize and print PDF documents.

Let's show you how to do merge in DynaPDF with the following method snippet:

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

As you see, we import all pages from all PDF documents and on the end do the page numbers, if a checkbox is clicked. Let's take a look on how we edit pages to add page numbers:

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

As you see we did some extra steps with drawing a rectangle over the area to wipe out any text there. We could use ExtractRect with the rectangle to look if there is existing text and then maybe use the other half of the page.

Let us know if you have questions. The example project will be included in next version as "Merge PDF documents with page numbers". Please do not hesitate to contact us if you have questions.

18 06 22 - 12:08