Skip to main content
Participant
Oct 7, 2006 12:08 AM UTC
Answered

Splitting Pdf

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 is closed to new replies. Start a new post to keep the conversation going.
cf_dev2Correct answer
Inspiring
Oct 8, 2006 8:12 PM UTC
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.

10 replies

Participating Frequently
Apr 12, 2021 11:37 PM UTC

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?

Translate
BKBK
Community Expert
Community Expert
Apr 18, 2021 2:23 PM UTC

@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>
--->

 

Translate
Participating Frequently
Apr 20, 2021 5:27 PM UTC

Thank you very much @ BKBK.

Translate
Inspiring
Oct 7, 2006 5:43 AM UTC
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

Translate
foresiteAuthor
Participant
Oct 8, 2006 5:06 PM UTC
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.
Translate
cf_dev2Correct answer
Inspiring
Oct 8, 2006 8:12 PM UTC
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.
Translate
Inspiring
Oct 7, 2006 5:43 AM UTC
..
Translate
Inspiring
Oct 7, 2006 5:43 AM UTC
..
Translate
Inspiring
Oct 7, 2006 5:37 AM UTC
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
Translate