I know this thread is kind of dead, but i myself have found it very helpful. 
So first off let me just say thank you for posting this solution I was
working on a project that allowed users to select reports to include in
their final pacakage and upload pdf attachments and then generated their
reports and merged all the resulting PDFs and PDF attachments into one giant
PDF.  The problem here was that every page needed to have a information at
the top only there was no way for me to know if i was going to stamp over
any content on the attachments, because they could virtually upload any PDF
they wanted.  From personal experience i would like to point out that
resizing the media box seems the best solution since it allows you to keep
the size of the original content the same when viewed on the screen and the
user will have the ability to scale it when printing from reader.

Alright so the real reason I am posting is to point out one small issue with
the media box solution.  I encountered this issue and it took me quite
awhile to figure it out.  In some cases a PDF might have a crop box in
place.  If this occurs you will be able to resize the media box like normal
and the dimensions of the page rectangle will change and it will seem as
though it resized and you can even put content in the area you just
expanded, only when you open the PDF the page is exactly the same size as it
was.  This is because the crop box does not allow you to see anything
outside of itself.  To resolve this issue you must resize the crop box as
well.  So in solution2 by Bruno:
>
>         public static void solution2() throws IOException,
> DocumentException {
>                 PdfReader reader = new PdfReader("sample.pdf");
>                 int n = reader.getNumberOfPages();
>                 PdfDictionary pageDict;
>                 ArrayList old_mediabox;
>                 PdfArray new_mediabox;
>                 PdfNumber value;
>                 PdfStamper stamper = new PdfStamper(reader, new
> FileOutputStream("solution2.pdf"));
>                 BaseFont font = BaseFont.createFont(BaseFont.HELVETICA,
> BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
>                 PdfContentByte directcontent;
>                 for (int i = 1; i <= n; i++) {
>                         pageDict = reader.getPageN(i);
>                         new_mediabox = new PdfArray();
>                         old_mediabox = pageDict.getAsArray(
> PdfName.MEDIABOX).getArrayList();
>                         value = (PdfNumber)old_mediabox.get(0);
>                         new_mediabox.add(new PdfNumber(value.floatValue()
> - 36));
>                         value = (PdfNumber)old_mediabox.get(1);
>                         new_mediabox.add(new PdfNumber(value.floatValue()
> - 36));
>                         value = (PdfNumber)old_mediabox.get(2);
>                         new_mediabox.add(new PdfNumber(value.floatValue()
> + 36));
>                         value = (PdfNumber)old_mediabox.get(3);
>                         new_mediabox.add(new PdfNumber(value.floatValue()
> + 36));
>                         pageDict.put(PdfName.MEDIABOX, new_mediabox);


The media box has been resized now resize the crop box if it exists (my code
is vb.net sorry):

If Not pageDict.GetAsArray(iTextSharp.text.pdf.PdfName.CROPBOX) Is Nothing
Then
    newCropBox = New iTextSharp.text.pdf.PdfArray
    oldCropBox =
pageDict.GetAsArray(iTextSharp.text.pdf.PdfName.CROPBOX).ArrayList
    value = CType(oldCropBox(0), iTextSharp.text.pdf.PdfNumber)
    newCropBox.Add(New PdfNumber(value.FloatValue() - 36))
    value = CType(oldCropBox(1), iTextSharp.text.pdf.PdfNumber)
    newCropBox.Add(New PdfNumber(value.FloatValue() - 36))
    value = CType(oldCropBox(2), iTextSharp.text.pdf.PdfNumber)
    newCropBox.Add(New PdfNumber(value.FloatValue() + 36))
    value = CType(oldCropBox(3), iTextSharp.text.pdf.PdfNumber)
    newCropBox.Add(New PdfNumber(value.FloatValue() + 36))

    pageDict.Put(iTextSharp.text.pdf.PdfName.CROPBOX, newCropBox)
End If



>                         directcontent = stamper.getOverContent(i);
>                         directcontent.beginText();
>                         directcontent.setFontAndSize(font, 12);
>                         directcontent.showTextAligned(Element.ALIGN_LEFT,
> "TEST", 0, -18, 0);
>                         directcontent.endText();
>                 }
>                 stamper.close();
>         }
> }
-- 
View this message in context: 
http://www.nabble.com/How-to-Shrink-Content-and-Add-Margins-tp11547554p22928738.html
Sent from the iText - General mailing list archive at Nabble.com.


------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php

Reply via email to