tags:

views:

22

answers:

2

I created a pdf file with iText. the pdf is a A4 file and have some margins defined.

    Paragraph paragraph = new Paragraph();
    Document document = new Document(PageSize.A4, 36, 72, 108, 180);
    PdfWriter.getInstance(document, new FileOutputStream("c:\\margin.pdf"));
    document.open();
    document.add(new Paragraph("Margin--->>roseinia.net"));
    document.close();

Now in another program, i want to read the margins of above pdf. Is there a way to do it in iText or some other library?

A: 

PDF itself doesn't really have the notion of margins. There are a number of bounding boxes associated with a given page:

  • MediaBox - the size of the physical media needed for the page
  • CropBox - the size of the visible page in a viewer (defaults to MediaBox if not present)
  • BleedBox - a clipping boundary for rendering in a production environment (may include room for folds)
  • TrimBox - intended dimensions of the finished page after trimming
  • ArtBox - the bounds of the page's meaningful content

The ArtBox might be what the "margins" are being translated into.

plinth
A: 

I don't have a copy of the iText trunk code on this machine, but I'm pretty sure it doen't write margin information into the PDFs it generates. Even if the trunk does, I couldn't tell you which version that was added... again, no trunk here.

There are a couple potential solutions to the problem however:

1) Manually write the information into the PDF. Given that you have the source that generated the docs, you should be able to modify that source. This won't help with docs that have already been generated, but it's better than nothing. It's also much easier than the alternative. There are several places where you might write this information. a) You could add the information as a string to the PDFs "doc info fields" (metadata). I'm pretty sure iText lets you write custom fields... yep.

new Meta("margins", buildStringOfMyMargins() );

You'd then have to parse the string back out again. Remember you have to add meta info before you doc.open();

b) Write out your own ArtBox (as described by plinth). This requires a second pass with a PdfStamper to write data directly to each page's PdfDictionary

c) Modify iText to include an art box based on the margins you provide.

2) Parse out the content locations. The current com.itextpdf.text.pdf.parser.* code doesn't handle line art, which may or may not be Very Important to you. You'd have to determine the bounding box of every element on a given page and compute the bounding box that encompassed them all. Lots of work.

Mark Storer