Raymond Camden's Blog Rss

ColdFusion 8: Working with PDFs (Part 4)

Posted in ColdFusion | Posted on 07-13-2007 | 7,687 views

So far I've covered getting info with PDFs and adding watermarks. In this entry I'll talk about removing pages from a PDF. It will be a short entry as the syntax is rather simple. Why would you want to remove pages? You may want to remove legal junk or other fluff. You may want to take a source PDF and delete most of the PDF to create a preview. Whatever you have in mind, ColdFusion makes it simple to do. Let's take a look at an example.

I'm going to use the same PDF generator I used in the previous entry:

view plain print about
1<cfdocument format="pdf" name="mydocument">
2<cfloop index="x" from="1" to="40">
3<p>
4doloras lorem upsom doloras paris hilton is my hero loreum ipsom dsoio foom an to dht end of the world
5will anyone actually read this probably not but let me put more realtext in so it flows a bit nicely
6<cfloop index="y" from="1" to="#randRange(1,9)#">This sentence will appear a random amount of time.</cfloop>
7</p>
8</cfloop>
9</cfdocument>

I've increased the size a bit. Now let's see how big the PDF is:

view plain print about
1<cfpdf action="getinfo" source="mydocument" name="data">
2
3<cfoutput>
4<p>
5Total pages: #data.totalpages#
6</p>
7</cfoutput>

Because the PDF has a bit of randomness in it, the total you see here will vary. For my last test the number was 5.

Now lets delete some pages. As you can guess, the action provided to CFPDF is deletePages. To determine what pages are removed, you use the pages attribute. This can be:

  • A page number (2 for example)
  • A page range (6-10 for exsample)
  • Any combination of the above (2,4,9-12)

For my example I'll delete pages one through three:

view plain print about
1<cfpdf action="deletepages" source="mydocument" pages="1-3" overwrite="true" name="mydocument">

And then display information about the PDF again:

view plain print about
1<cfpdf action="getinfo" source="mydocument" name="data">
2
3<cfoutput>
4<p>
5Total pages now: #data.totalpages#
6</p>
7</cfoutput>

All together here is the demo I wrote:

view plain print about
1<cfdocument format="pdf" name="mydocument">
2<cfloop index="x" from="1" to="40">
3<p>
4doloras lorem upsom doloras paris hilton is my hero loreum ipsom dsoio foom an to dht end of the world
5will anyone actually read this probably not but let me put more realtext in so it flows a bit nicely
6<cfloop index="y" from="1" to="#randRange(1,9)#">This sentence will appear a random amount of time.</cfloop>
7</p>
8</cfloop>
9</cfdocument>
10
11<cfpdf action="getinfo" source="mydocument" name="data">
12
13<cfoutput>
14<p>
15Total pages: #data.totalpages#
16</p>
17</cfoutput>
18
19
20<cfpdf action="deletepages" source="mydocument" pages="1-3" overwrite="true" name="mydocument">
21
22<cfpdf action="getinfo" source="mydocument" name="data">
23
24<cfoutput>
25<p>
26Total pages now: #data.totalpages#
27</p>
28</cfoutput>

As an interesting aside, I plan on sharing a demo at the end of this series that demonstrates a lot of these methods rolled together into one application. One of the features my application has is a page viewer. It lets you view one page from a PDF. How did I do this? I simply used deletePages to remove every page but the one you want to see. So while CFPDF doesn't have a getPage action, it is easy enough to build one yourself.

Comments

[Add Comment] [Subscribe to Comments]