0

I have seen plenty of ways to change the pagesize of a new blank document, but I can not get it to work on a PDF i've accessed via pdfReader

Help?

Gene R
  • 1,555
  • 5
  • 19
  • 32

2 Answers2

2

Warning: I know Very Little about CF, but quite a bit about iText.

Henry's answer will work, but it could be a bit more efficient.

You want a PdfStamper. Changing the page size isn't something that is directly supported at a higher level, so you'll have to use low-level pdf object calls. Like this:

final static float POINTS_PER_INCH = 72f;
final static float INCHES_TO_ADD = 3f;

PdfReader reader = new PdfReader(pdfPath); // throws
PdfStamer stamper = new PdfStamper(reader, outputStream); // throws

for (int curPageNum = 1; curPageNum <= reader.getNumberOfPages(); ++curPageNum) {
  PdfDictionary pageDict = reader.getPageN(curPageNum);
  // pdf rects are stored as [llx, lly, urx, ury].
  // X increases to the right, Y increases upward.  
  // Note that the origin doesn't have to be 0,0.
  PdfArray mediaBox = pageDict.getAsArray(PdfName.MEDIABOX);
  float curBottom = mediaBox.getAsNumber(1).floatValue();
  curBottom -= INCHES_TO_ADD * POINTS_PER_INCH;
  mediaBox.set(1, new PdfNumber(curBottom));
}

stamper.close(); // throws

In addition to the media box, you may also have to modify the CROPBOX, using the same "get the rect, adjust the bottom" technique. Note that there are several other page boxes that might exist in your PDF that may or may not need to be modified... "art box", "trim box", "bleed box". I may have forgotten one or two as well.

This will almost certainly result in a negative value for your bottom Y coordinate[s]. If your PDF is processed by a Less-Than-Good bit of software, this could be a problem. This would be a bug in their software, not this process. However, if you need to work around a problem like this, Henry's code will do the trick, producing pages with 0,0 as the lower left corner. Adobe won't bat an eye, nor will iText itself, though software written using iText might not be so savvy.

Mark Storer
  • 15,672
  • 3
  • 42
  • 80
1

not sure if it works, but here's what I found

You can use a blank PDF of the proper page size as a starting point and then overlay the PDF segments in the right position as watermarks using DDX. You probably need to flatten the PDF after each watermark has been added.

http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:62109

Haven't tested the code, but maybe something like this?

<cfpdf name="resizedPdf" action="addwatermark" source="blank.pdf" copyfrom="image.pdf">
<cfpdf name="resizedPdfWithFooter" action="addfooter" source="resizedPdf" text="xyz">

see <cfpdf> doc : http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7995.html

Henry
  • 32,689
  • 19
  • 120
  • 221
  • The PDF i'm opening is basically a large graphic. currently 8.5x11, the client wants to add an extra 3" at the bottom to print details... – Gene R Nov 03 '10 at 17:53
  • Ok, not sure if i'm making my end result clear. 1) I have an 8.5x11 PDF I receive from client 2) I need to make that PDF 8.5x14 3) I need to add some footer text I need to add in that bottom 3" So, what I've done so far (per your advice) is create a blank 8.5x14 PDF I then Read in the original PDF and create my footer text. But with Watermark in DDX, I only see how to overlay text onto an existing PDF, not contents from another PDF such as is in my original. SO what I end up with is 1 final PDF but with 2 pages, 1 page that is 8.5x11 with footer text and 2nd page that is the blank 8.5x14 – Gene R Nov 03 '10 at 18:42
  • Hmm.. I thought the DDX supported using another pdf as the watermark *source*? – Leigh Nov 03 '10 at 19:20
  • don't even need DDX if you use , see updated answer. I haven't tested it – Henry Nov 03 '10 at 19:22
  • 1
    Yes, that should work. Simpler too. (Though I looked it up in the elusive DDX reference and it also supports pdf's as the source ) – Leigh Nov 03 '10 at 19:29
  • THAT'S IT!!! Guys, I have been struggling with this over the past 3 days, but bit by bit with the help, I've got it. Thank you both so much! – Gene R Nov 03 '10 at 19:34