Hello,

I have an application which generates surveys and converts the contents to
a PDF via FOP for printing.  There is a maximum allowable number of pages
for any given survey.  Since each survey generated is dynamic and unique
based on outside information it is necessary for me to calculate the exact
number of inches that each potential question may consume on the resulting
PDF file.  The number of inches for each potential question is stored on
the database with the question content.

When the question is saved, I need to render a PDF in order to determine
the content height in inches.  We have started off by extending the
PDFRenderer class and adding several methods.  The application basically
sends the FO file for conversion and the renderer class ("MyRenderer.java")
provides a method to calculate height in inches.

I am encountering a problem with the calculation - when the final PDF is
printed the actual number of inches (when measured manually) is somewhat
smaller than what the application gives as the content height in inches.  I
am having trouble figuring out what is actually happening here in the
PDFRenderer.  The renderWordArea method seems to be called many times even
though there should only be a single word Area.

In the example below we are printing the survey on a 8.5 x 14 (Legal) size
paper.  The question itself manually measures out to be approximately 6.9
or 7 inches in height with the margins specified in the FO file.  However,
the calculated height in inches is 6.88 inches.  When the size of the
question is increased the accuracy gets worse.

I have outlined my code and test case information below.  Is there
something out of place within my FO file that is causing this increase in
height amount?  Any help you are able to provide is greatly appreciated

The FO file that is being used in this case is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format";>
<fo:layout-master-set>
<fo:simple-page-master master-name="simple" page-width="612px"
page-height="1008px">
<fo:region-body margin-top="22.0px" margin-left="60.0px"
margin-bottom="60.0px" margin-right="64.0px"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simple">
<fo:static-content flow-name="xsl-region-after"/>
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:inline white-space-collapse="false"
font-family="verdana,arial,helvetica,sans-serif" font-size="10pt">Have you
had your blood pressure checked during the last 2 years?<fo:block/> line
2<fo:block/> line 3<fo:block/> line 2<fo:block/> line 3<fo:block/> line
2<fo:block/> line 3Have you had your blood pressure checked during the last
2 years?<fo:block/> line 2<fo:block/> line 3<fo:block/> line 2<fo:block/>
line 3<fo:block/> line 2<fo:block/> line 3Have you had your blood pressure
checked during the last 2 years?<fo:block/> line 2<fo:block/> line
3<fo:block/> line 2<fo:block/> line 3<fo:block/> line 2<fo:block/> line
3Have you had your blood pressure checked during the last 2
years?<fo:block/> line 2<fo:block/> line 3<fo:block/> line 2<fo:block/>
line 3<fo:block/> line 2<fo:block/> line 3Have you had your blood pressure
checked during the last 2 years?<fo:block/> line 2<fo:block/> line
3<fo:block/> line 2<fo:block/> line 3<fo:block/> line 2<fo:block/> line
3Have you had your blood pressure checked during the last 2
years?<fo:block/> line 2<fo:block/> line 3<fo:block/> line 2<fo:block/>
line 3<fo:block/> line 2<fo:block/> line 3</fo:inline>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>

The MyRenderer.java class that I am using is as follows:

public class MyRenderer extends PDFRenderer {

      public static final int MILLIPOINTS_PER_INCH = 72000;

      private HashMap heights = new HashMap();

      public void renderWordArea(WordArea area) {
            super.renderWordArea(area);

            Page page = getPage(area);

            if (page != null) {
                  double max =
page.getBody().getMainReferenceArea().getYPosition();

                  double y = max - currentYPosition;

                  setCurrentY(page, y);
            }
      }

      public double getHeightInInches() {
            double height = 0;
            Iterator iter = heights.values().iterator();
            int count = 0;
            while (iter.hasNext()) {
                  count++;
                  height += ((Number) iter.next()).doubleValue();
            }
            return height / MILLIPOINTS_PER_INCH;
      }

      private void setCurrentY(Page page, double y) {
            Double num = new Double(page.getNumber());
            Double curr = (Double) heights.get(num);

            if (curr == null) {
                  curr = new Double(y);
            } else {
                  curr = new Double(Math.max(y, curr.doubleValue()));
            }
            heights.put(num, curr);
      }

      private Page getPage(Area area) {
            Page page = area.getPage();
            Area parent = area.getParent();
            if (page == null && parent != null) {
                  return getPage(parent);
            }
            return page;
      }
}

My test case sends the FO file into the "in" variable and from there runs
the driver...

                  //this results in the FO file
                  InputStream in = builder.createFo(tempXML);
                  InputSource inputSource = new InputSource(in);

                  MyRenderer r = new MyRenderer();

                  Driver driver =
                        new Driver(inputSource, new FileOutputStream(
"test3.pdf"));
                  driver.setRenderer(r);
                  driver.run();

                  System.err.println("height = " + r.getHeightInInches());



Thank You!
Rachel


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to