Skip to main content
Participant
October 7, 2006
Answered

Splitting Pdf

  • October 7, 2006
  • 5 replies
  • 2491 views
I am trying to write a cold fusion program which splits up a pdf file with multiple pages into separate files for each page. In other words, let's say I have a pdf document (document.pdf) that has 100 pages, I want the program to break it up so I end up with 100 files, one for each page (document0001.pdf, document0002.pdf,document0003.pdf,document0004.pdf,document0005.pdf etc, etc.). Any help would be greatly appreciated.
This topic has been closed for replies.
Correct answer cf_dev2
Here is a rough example. I only skimmed the API so no guarantees. You can change the numbering of the file names on your own.

5 replies

Participating Frequently
April 12, 2021

Hi Foresite,

 I hope you are still active here. 

I noticed your question, and I cannot seem to find the answer that helped you.

I am looking for ways to do exactly what you were looking back than.

Can you help out, please?

BKBK
Community Expert
Community Expert
April 18, 2021

@PCR3TDDV , in software terms, 2006 is quite a number of generations ago. ColdFusion has moved on since then. From ColdFusion 8 onwards, you can use cfpdf to perform operations on a PDF file.

 

For example, the following function will extract each page of a given PDF document as a single PDF document:

 

<cffunction name="extractPDFDocAsSinlePages" output="false" returntype="void">
	<cfargument name="originalPDFDocSource" required="yes" type="string">
	<cfargument name="destinationFolder" required="yes" type="string">
	<cfargument name="totalnumberOfPagesOfOriginalPDFDoc" required="yes" type="numeric">
	
	<cfset var numberOfPages=arguments.totalnumberOfPagesOfOriginalPDFDoc>
	<cfif numberOfPages eq 1><!---Nothing to do.--->
		<cfreturn>
	</cfif>
	
	<cfset var currentPage=0>
	<cfset var pagesToDelete="">
	
	<cfloop index="currentPage" from="1" to="#numberOfPages#">	
		<cfif currentPage eq 1>
			<cfset pagesToDelete="2-#numberOfPages#">
		<cfelseif currentPage eq numberOfPages>
			<cfset pagesToDelete="1-#numberOfPages-1#">
		<cfelse>
			<cfset pagesToDelete="1-#currentPage-1#,#currentPage+1#-#numberOfPages#">
		</cfif>
		
		
		<cfpdf
	    action = "deletepages"
	    pages = "#pagesToDelete#"
	    source = "#arguments.originalPDFDocSource#"
	    overwrite = "yes"
	    destination = "#arguments.destinationFolder#\myDoc_page_#currentPage#.pdf">
	    
	</cfloop>

</cffunction>

<!--- Test Code --->
<!--- testDoc.pdf has 18 pages --->
<!--- The 18 extracted pages are stored in C:\Users\BKBK\Desktop\docs\extracted --->
<!---
<cfoutput>#extractPDFDocAsSinlePages("C:\Users\BKBK\Desktop\docs\testDoc.pdf","C:\Users\BKBK\Desktop\docs\extracted",18)#</cfoutput>
--->

 

Participating Frequently
April 20, 2021

Thank you very much @ BKBK.

Inspiring
October 7, 2006
This thread has an example of reading/writing a pdf. Its concatenating files
instead of splitting, but it might get you started

http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=1&catid=7&t
hreadid=1191362

http://itextdocs.lowagie.com/tutorial/index.html

foresiteAuthor
Participant
October 8, 2006
Thanks for your response. I had already seen that posting and struggled with the code. It works great for concatenating files but I am not a good enough programmer to use it to get to spiltting a pdf file. It works extremely fast in concatenating files and I was hoping to use similar coade to split a pdf up.
cf_dev2Correct answer
Inspiring
October 8, 2006
Here is a rough example. I only skimmed the API so no guarantees. You can change the numbering of the file names on your own.
Inspiring
October 7, 2006
..
Inspiring
October 7, 2006
..
Inspiring
October 7, 2006
This thread has an example of reading/writing a pdf. Its concatenating files instead of splitting, but is should get you started
http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=1&catid=7&threadid=1191362

http://itextdocs.lowagie.com/tutorial/index.html