Copy/Merge PDF files

This Forum is for the use of Software Developers requiring help and assistance for Tracker Software's PDF-Tools SDK of Library DLL functions(only) - Please use the PDF-XChange Drivers API SDK Forum for assistance with all PDF Print Driver related topics or PDF-XChange Viewer SDK if appropriate.

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Tracker Supp-Stefan

Post Reply
kenbla
User
Posts: 40
Joined: Wed Jun 21, 2006 1:29 pm

Copy/Merge PDF files

Post by kenbla »

Hi everybody, I haven't used PDFXchange for a while but now I'm at it again. This time I need to merge a number of PDF files from disk into one PDF file! Not a big thing I guess but I don't know how it is done and I would appreciate any assistance!
I work as a system developer in a Borland Delphi environment.
Regards,
Kenbla
User avatar
Paul - Tracker Supp
Site Admin
Posts: 6897
Joined: Wed Mar 25, 2009 10:37 pm
Location: Chemainus, Canada
Contact:

Re: Copy/Merge PDF files

Post by Paul - Tracker Supp »

Hi kenbla,

is there a specific example form the docs you are having trouble with? What language/environment are you using?
Best regards

Paul O'Rorke
Tracker Support North America
http://www.tracker-software.com
kenbla
User
Posts: 40
Joined: Wed Jun 21, 2006 1:29 pm

Re: Copy/Merge PDF files

Post by kenbla »

I'm now able to merge a number of PDF files into one PDF file! So that's the good thing! but I have now run into another problem! After the merging of the PDF files I want to add another PDF file (containing the table of contents) to the recently created PDF file (the combined one) everything seems to go well BUT when I come to "PXCp_InsertPagesTo" the program abnomally ends!! And I don't have clue why!
I issue a "PXCp_Delete" method call when I'm done with each PDFXChange sequence in order to release memory, but it appears as if PDFXChange calls is still "opened" for some reason. What do I need to do to make it work?
My code runs in the following sequence:
1. Create a number of PDF files (lets say 20 files). I use XSL FO to create these PDF files and save them to disk. This works fine!
2. Merge these files into a new PDF file using "PXCp_InsertPagesTo" method calls for each of the 20 files read from disk. This works fine! I use "PXCp_Delete" to release each of the 20 PDF files when I'm done.
3. Merge the t.o.c. PDF file to the PDF file created in item 2 above. This fails!!
Regards,
Kenbla
kenbla
User
Posts: 40
Joined: Wed Jun 21, 2006 1:29 pm

Re: Copy/Merge PDF files

Post by kenbla »

One more thing! In item number 3 i use "PXCp_InsertPagesTo" and this is where it fails!
Regards,
Kenbla
User avatar
Lzcat - Tracker Supp
Site Admin
Posts: 677
Joined: Thu Jun 28, 2007 8:42 am

Re: Copy/Merge PDF files

Post by Lzcat - Tracker Supp »

Hi kenbla,

can you provide us a complete sample reproducing the problem?

We will need code, a working executable and source pdf files.

Please pack all files in a zip archive and attach here or to a private message (or send using e-mail to lazycat@tracker-software.com ).
Sometimes code which illustrates the call sequence is enough, but a complete sample is preferred.

Also please ensure that you are using latest build of our SDK.
Victor
Tracker Software
Project manager

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
kenbla
User
Posts: 40
Joined: Wed Jun 21, 2006 1:29 pm

Re: Copy/Merge PDF files

Post by kenbla »

Here is part of my Delphi code:
oNewPDF := TGENPDFXChange.Create(sPdfPath);
oGENPDFXChange := TGENPDFXChange.Create(sProdPath + sKatalog + '.pdf');
iEndPage := oGENPDFXChange.PageCount;
oGENPDFXChange.ReleasePDFDocument; //Actually executes this: PXCp_Delete(mhNewPDFDocument);
FreeAndNil(oGENPDFXChange);
oNewPDF.InsertPagesToPdfDocument(sProdPath + 'Betreg_' + msKatalogFbet + '.pdf', 1, iEndPage);
oNewPDF.SavePDFDocument(sProdPath + 'Tempreg.pdf'); //Actually executes this: PXCp_WriteDocumentW(mhNewPDFDocument, PWChar(WideString(sFilePath)), PXCp_CreationDisposition_Overwrite, PXCp_Write_Release);
oGENPDFXChange.ReleasePDFDocument; //Actually executes this: PXCp_Delete(mhNewPDFDocument);
FreeAndNil(oNewPDF);

The first around this works fine! but when I execute the same piece of code a second time the program abnormally ends on the "InsertPagesToPdfDocument" codeline. It seems as if the objects still exists even afterPXCP_Delete and Free (FreeAndNil). Do I have to do something after the "WriteDocumentW" in addition the release and free instructions?

I get the following error message when I run my tests: "Access violation at address 10014466 in module 'xcpro35.dll'. Read of address 00000035"
Regards,
Kenbla
User avatar
Lzcat - Tracker Supp
Site Admin
Posts: 677
Joined: Thu Jun 28, 2007 8:42 am

Re: Copy/Merge PDF files

Post by Lzcat - Tracker Supp »

Sorry, but this code is not enoug - I need complete sequnce call to PXCp_... functions, not calls to your Delphi wrapper. Better is complete project with source codes which reproduce problem.
Please make sure that you are using latest version of V3 sdk - Version 3.60130 (available here https://www.pdf-xchange.com/version/pdf-tools-sdk).
And note that support of this version is topped years ago, so if there is problem in library it will be not fixed in V3 - you will need to updgrade to V4.
Victor
Tracker Software
Project manager

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
kenbla
User
Posts: 40
Joined: Wed Jun 21, 2006 1:29 pm

Re: Copy/Merge PDF files

Post by kenbla »

OK, this is the "PXCP sequence"
//First time around and everything works fine
//First instance
hr := PXCp_Init(@mhNewPDFDocument, PChar(mpxLicense), PChar(mpxDevcode));
pxRC := PXCp_ReadDocumentW(mhNewPDFDocument, PWChar(WideString(sFilepath)), 0);
//Second instance
hr := PXCp_Init(@mhNewPDFDocument, PChar(mpxLicense), PChar(mpxDevcode));
pxRC := PXCp_ReadDocumentW(mhNewPDFDocument, PWChar(WideString(sFilepath)), 0);
hr := PXCp_GetPagesCount(mhNewPDFDocument, @dwTotalPagesCount); //Get page count for the second instance
PXCp_Delete(mhNewPDFDocument);
//Back to the first instance again
hr := PXCp_Init(@hSourcePDFDocument, PChar(mpxLicense), PChar(mpxDevcode));
hr := PXCp_ReadDocumentW(hSourcePDFDocument, PWChar(WideString(sPDFExtractPagesFrom)),0);
hr := PXCp_InsertPagesTo(hSourcePDFDocument, mhNewPDFDocument, @Range, RangesCount, 0);
hr := PXCp_Delete(hSourcePDFDocument);
hr := PXCp_WriteDocumentW(mhNewPDFDocument, PWChar(WideString(sFilePath)), PXCp_CreationDisposition_Overwrite, PXCp_Write_Release);
PXCp_Delete(mhNewPDFDocument);

//Second time around within the same "session" exactly the same code is run. This time the program abnormally ends when issuing the PXCP_InsertPagesTo
//First instance
hr := PXCp_Init(@mhNewPDFDocument, PChar(mpxLicense), PChar(mpxDevcode));
pxRC := PXCp_ReadDocumentW(mhNewPDFDocument, PWChar(WideString(sFilepath)), 0);
//Second instance
hr := PXCp_Init(@mhNewPDFDocument, PChar(mpxLicense), PChar(mpxDevcode));
pxRC := PXCp_ReadDocumentW(mhNewPDFDocument, PWChar(WideString(sFilepath)), 0);
hr := PXCp_GetPagesCount(mhNewPDFDocument, @dwTotalPagesCount); //Get page count for the second instance
PXCp_Delete(mhNewPDFDocument);
//Back to the first instance again
hr := PXCp_Init(@hSourcePDFDocument, PChar(mpxLicense), PChar(mpxDevcode));
hr := PXCp_ReadDocumentW(hSourcePDFDocument, PWChar(WideString(sPDFExtractPagesFrom)),0);
hr := PXCp_InsertPagesTo(hSourcePDFDocument, mhNewPDFDocument, @Range, RangesCount, 0);
hr := PXCp_Delete(hSourcePDFDocument);
hr := PXCp_WriteDocumentW(mhNewPDFDocument, PWChar(WideString(sFilePath)), PXCp_CreationDisposition_Overwrite, PXCp_Write_Release);
PXCp_Delete(mhNewPDFDocument);

What can the problem be? I thought that the PXCP_Delete method should do the trick!
Regards,
Kenbla
kenbla
User
Posts: 40
Joined: Wed Jun 21, 2006 1:29 pm

Re: Copy/Merge PDF files

Post by kenbla »

I might have solved the problem!
Hi John,
I have run another test and discovered the following:
When I run the application using the following parameter list
"hr := PXCp_WriteDocumentW(mhNewPDFDocument, PWChar(WideString(sFilePath)), PXCp_CreationDisposition_Overwrite, PXCp_Write_NoRelease);" it works fine!

But when I use this parameterlist "hr := PXCp_WriteDocumentW(mhNewPDFDocument, PWChar(WideString(sFilePath)), PXCp_CreationDisposition_Overwrite, PXCp_Write_Release);" it only works once per session!

What is the difference between PXCp_Write_NoRelease and PXCp_Write_Release? It sounds to me that I should use the latter! but that doesn'r work for me!
Regards,
Kenbla
User avatar
Lzcat - Tracker Supp
Site Admin
Posts: 677
Joined: Thu Jun 28, 2007 8:42 am

Re: Copy/Merge PDF files

Post by Lzcat - Tracker Supp »

Please take a little more time to read the documentation - this behaviour is documented :wink:

When you call PXCp_WriteDocumentW with the PXCp_Write_Release flag it will automatically release the saved document (assuming the save is successful), so a subsequent call to PXCp_Delete function causes problems.
The correct solution therefore - do not call PXCp_Delete if call to PXCp_WriteDocumentW is/was successful (check the return code for this status).

It is recommended therefore to use PXCp_Write_NoRelease flag - and PXCp_WriteDocumentW will not release the document regardless of whether the operation was successful or not, and in this instance you must call PXCp_Delete yourself to release the document once the task is complete.

This code is more clear, and gives more options (you may use document after saving it, without reopening etc).

And some more tips about merging:

If you need to merge several documents it is much faster to not save after each merge operation, but wait and merge all at once when. This should not affect file size, but avoid unnecessary saving/reading.

If you need to merge large numbers of files - do not merge all of them at once.

This may seem to conflict with the advice above, but this is necessary because of Windows memory limitations (particularly 32 bit Windows). When merging - significant portions of inserted pages are kept in memory, so you may quickly reach the memory limit (less than 2Gb for 32-bit applications). The best solution for this - merge files by groups. Open a destination file, append (insert) several files (10-20), save the destination file and close it. Open destination file again, append the next portion, again save and close.
Repeat until all files will be appended.
HTH.
Victor
Tracker Software
Project manager

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
kenbla
User
Posts: 40
Joined: Wed Jun 21, 2006 1:29 pm

Re: Copy/Merge PDF files

Post by kenbla »

Thanks Victor!
I have read every piece of documentation that we have at our site but I can't find anything which describes this! I have looked in the "PDF-XChange Help" file as well as the "PDF-XChange Users Manual" and those two are the only pieces of information that we have. Where is this type of information described?
Anyway my apllication runs OK now when I use ....NoRelease and PXCp_Delete.
Regards,
Kenbla
User avatar
John - Tracker Supp
Site Admin
Posts: 5219
Joined: Tue Jun 29, 2004 10:34 am
Location: United Kingdom
Contact:

Re: Copy/Merge PDF files

Post by John - Tracker Supp »

Please see the latest SDK help - PDF-ToolsV4SDK.pdf

and using any good PDF Viewer (e.g PDF-XChange Viewer) you can search for the relevant terms ;)

e.g. page 289 details : PXCp_Write_NoRelease

HTH
If posting files to this forum - you must archive the files to a ZIP, RAR or 7z file or they will not be uploaded - thank you.

Best regards
Tracker Support
http://www.tracker-software.com
User avatar
John - Tracker Supp
Site Admin
Posts: 5219
Joined: Tue Jun 29, 2004 10:34 am
Location: United Kingdom
Contact:

Re: Copy/Merge PDF files

Post by John - Tracker Supp »

Further to your emailed request :
------------------------------------

Hello again, how can I find the manual you suggested on the forum (SDK help
- PDF-ToolsV4SDK.pdf). We run a fully licensed 3.6x version of PDFxChange
and I don't think that we have a copy of it!
Regards,
Kenbla

------------------------------------

Hi,

Its in the SDK download as all doc's are - but....

The function is detailed in the Version 4 doc's (current release) - it is possible this was not documented in Version 3.x ..

So I would recommend you download Version 4 from the page link below - even if if just for the doc's - though note some functionality will obviously be strictly available in V4.x

https://www.pdf-xchange.com/product/downloads/sdk

HTH
If posting files to this forum - you must archive the files to a ZIP, RAR or 7z file or they will not be uploaded - thank you.

Best regards
Tracker Support
http://www.tracker-software.com
Post Reply