Yegor,
Thanks for the response. In order to make getTextHeight() function, I had
to add the line:
breakText(graphics);
to the start of the XSLFTextShape.drawParagraphs method, otherwise I would
get a null pointer exception.
Also, when I use the calculation, it gets the same results as the
home-grown algorithm I had been using in the test case I've included below
(not really a test case, since I can't compare the end result in the file).
Essentially, I create a text box with a known width (the 82), insert a
paragraph, then measure the height of the text, and then reshape the anchor
of the text box to use that height. However, in this case, it comes up with
a height of 6. This represents a single line for the text box. However,
when I open the file, the text wraps and ends up showing two lines.
I'm going to look through the code and see if I can find out why that is,
but wanted to run this code past you first and see if I'm doing something
wrong.
On a positive note, for most every other text sample that I'm trying, it
works correctly.
Thanks,
Mike
Code sample:
public void testTextBreaking() throws Exception {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFTextBox elementTextBox = slide.createTextBox();
elementTextBox.setAnchor(new Rectangle(20, 20, 82, 1));
elementTextBox.setBottomInset(0);
elementTextBox.setTopInset(0);
elementTextBox.setLeftInset(0);
elementTextBox.setRightInset(0);
XSLFTextRun elementTextRun =
elementTextBox.addNewTextParagraph().addNewTextRun();
elementTextRun.setFontFamily("Arial");
elementTextRun.setFontSize(6);
elementTextRun.setText("25000.000001 - 31680.000000");
elementTextBox.setAnchor(new Rectangle(20, 20, 82,
(int)elementTextBox.getTextHeight()));
File file = new File("testTextBreaking.pptx");
BufferedOutputStream os = new BufferedOutputStream(new
FileOutputStream(file));
try {
ppt.write(os);
} finally {
os.close();
}
}
On Tue, Nov 22, 2011 at 11:29 PM, Yegor Kozlov <[email protected]>wrote:
> XSLFTextShape#getTextHeight() returns the cumulative height occupied
> by the text, but this method is private for now. The implementation is
> pretty complex and takes into account all character and paragraph
> properties such as font size and family, line spacing, spacings before
> and after paragraphs, margins, indents, etc.
>
> I'm going to make it public. For now you can call it via reflection or
> apply this change to trunk and use your custom build.
>
> Yegor
>
> On Wed, Nov 23, 2011 at 2:51 AM, Mike Lundin <[email protected]> wrote:
> > I have a text box with a set width (essentially, a entry in a column). I
> > would like to add text to the text box and determine how much vertical
> > space that text will take up with the document. This will allow me to
> > position the next element just below the current one.
> >
> > I'm currently performing an estimation of the height based on a
> FontMetrics
> > object, but it's not exact and will sometimes result it large gaps or
> text
> > running over other elements.
> >
> > Is there a way to do this?
> >
> > Thanks,
> > Mike
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>