VNS FPDF import and save pages

Hi…i am working on a project (2025R2.1, Mac/Win) where I have to split a pdf file into new pdf files. Therefore I have an array with the filenames, that have to be created. Another array holds the information of the pdfs. In my example the pdffilenamearray holds 3 filenames A,B and C. The pdf_page array has pdf_page(0).Filename=A, pdf_page(1).Filename=A, pdf_page(2).Filename=B and pdf_page(3 to 5).Filename=C. I want to import the 6 pages pdf file docname and create the new files in a loop.

For Each pdffilename As String In pdffilenamearray
 Var newpdf As New VNSPDFDocument()
 Call newpdf.SetSourceFile(docname)

 For Each pdf_page As classPageData In pdf_pages
  If pdf_page.Filename = pdffilename Then
   Var tpl As Integer = newpdf.ImportPage(pdf_page.PageNumber)
   newpdf.AddPage() 
   newpdf.UseTemplate(tpl, 0, 0, 0, 0)
  End If
 Next

 Var output As FolderItem = outputdocpath.Child(pdffilename + “.pdf”)
 newpdf.Save(output)
Next

So far, this works. But in all 3 created files the first page is an empty white page. When I swap the two lines .AddPage.. and .UseTemplate.. then the last page in the created pdf file is the empty page. The PageNumber-Variable is 1-based.
What is wrong with my code?

when you create the newpdf as new vnspdfdocument(), it creates a new blank page
so when you addpage() later you already have a blank page
just remove the addpage() line it should work.

Thank you for your answer.

No, removing the .AddPage does not work. When I do this, he last pdf is a one-page combined pdf of the 3 pages.

Var newpdf As New VNSPDFDocument()    // Move this  here

For Each pdffilename As String In pdffilenamearray
Call newpdf.SetSourceFile(docname)

For Each pdf_page As classPageData In pdf_pages
If pdf_page.Filename = pdffilename Then
Var tpl As Integer = newpdf.ImportPage(pdf_page.PageNumber)
newpdf.AddPage()
newpdf.UseTemplate(tpl, 0, 0, 0, 0)
End If
Next

Var output As FolderItem = outputdocpath.Child(pdffilename + “.pdf”)
newpdf.Save(output)
Next

you must understand that when you create a new vnspdfdodument, it creates automatically a blank page.
so you must not do the addpage() for the first page only.

2 Likes

Yes, here is the solution:

For Each pdffilename As String In pdffilenamearray
  Var newpdf As New VNSPDFDocument()
  Var isFirstPage As Boolean = True
  Call newpdf.SetSourceFile(docname)
  
  For Each pdf_page As classPageData In pdf_pages
    If pdf_page.Filename = pdffilename Then
      Var tpl As Integer = newpdf.ImportPage(pdf_page.PageNumber) 
      If Not isFirstPage Then
        newpdf.AddPage()
        isFirstPage = False
      End If
      newpdf.UseTemplate(tpl, 0, 0, 0, 0)  
    End If
  Next
  
  Var output As FolderItem = outputdocpath.Child(pdffilename + ".pdf")
  newpdf.Save(output)
Next

Works as it should…thank you for the help!

1 Like

it does not change the result but I would move the
isFirstPage = false
inside the
If Not isFirstPage Then
block

2 Likes

[quote=“Jean-Yves Pochez”]
it does not change the result but I would move the
isFirstPage = false
inside the
If Not isFirstPage Then
block
[/quote]

this does not work…when I do this, I get all the pdf pages with the same name combined in one pdf again :smiley:

btw…I compiled my app for Windows today and when I run it on a Windows 11 system, I got an TypeMismatchException when the program reaches my loop. On a MacBook Air M3 there is no problem, whether in the IDE or as a compiled app…

as it is in your code, and it’s only a part that we see here, I won’t be able to help you much…
if the previous code was working like you wanted, best way is to use this one ?

What i mean is, that my app has absolutely no problem under MacOS. It also compiles for Windows without an error. But when i run the Windows version, i get this TypeMismatchException when the programm enters the loop. When i remove all VNS..-functions in the loop, there is no problem in the Windows version.

if you can build a small test project that exibits the error I will be able to test it ( and also fix it)
you can send it by private message here if you want
if not, not having your code I won’t be able to understand where it can come from.
at least give some details about the type mismatch exception ?

the error you get is because you must install the ZLIB1.DLL library next to your exe file under windows
this library is included in macos systems but not on windows
you can get it here : ZLIB DLL Home Page
or you can purchase my ZLib premium module for the VNS FPDF Xojo code and it will then work everywhere.

this happens because the pdf you try to import contains some FlatDecode compressed streams that need ZLib to decompress, and it is not part of windows 11.
I will fix this error on the next release to have a less brutal error !

1 Like

Wow…thank you! I’ll try my app on sunday, when I get to my work pc again…

for the records, the zlib library is also included in linux systems
it is included in ios but the sandboxing make its use impossible
it seems the same under android.
I made the premium zlib module to be able to import pdf on all platforms.