What you're doing appears to be technically correct, but has I think exposed a 
limitation in that part of the library. The specification says that the 
normAutofit element in the XML has an attribute called fontScale which if 
omitted then a scale of 100% is implied, which is probably why you see your 
text spilling over the shape in Powerpoint as it is being rendered at normal 
size. When you resize the shape by a fraction in Powerpoint it knows you want 
autofit, so recalculates and sets the scale, which is why you then see it 
correctly.

The XSLFTextShape class merely sets the normAutofit element when you call 


shape1.setTextAutofit(TextAutofit.NORMAL);

but doesn't set the fontScale attribute (and certainly doesn't calculate it for 
you). Unfortunately there doesn't appear to be a way for you to directly set 
the fontScale attribute at the moment, you would need to delve into the 
underlying XML objects for the shape to do that.
Equally calculating the size of the text could get interesting taking into 
account all the spacing, justification etc, but once you have that and you know 
the size of the anchor then it would be relatively straight-forward to 
calculate the scaling percentage you need to make the text fit; then it's just 
a case of setting the fontScale attribute.


I may of course be wrong, but that's based on my knowledge of the DrawingML 
spec relating to textboxes.


If you aren't in a position to implement and contribute a fix can I ask you to 
please raise it as bug, as I have a feeling when I get to the Powerpoint output 
side of my project I'll need this too, and if it hasn't already been picked up 
by someone then I'll have to look at doing it.


Regards
Darren



>________________________________
> From: "Chaudhari, Apoorva" <[email protected]>
>To: [email protected] 
>Sent: Monday, August 5, 2013 4:05 AM
>Subject: Text Auto fit function in XSLF POI not working
> 
>
>Hello everyone,
>
>I have issues with the implementation of the text auto fit function
>available in POI. I have a code snipplet here, where I want that the
>overflow text ( greater than the size of the anchor ) gets resized to a
>smaller font and fit within the TextBox bounds. However, it doesnt do that.
>BUT if you manually try to resize it, meaning if you try to increase or
>decrease the size of the textbox by even a very small amount, the text gets
>reduced to the size of the font that it should be, to fit within that text
>box.
>
>Can anyone see what I am doing wrong, any recommendations/feedback are
>welcome!
>
>public static void main (String[] args) throws FileNotFoundException,
>IOException {
>
>    XMLSlideShow ppt = new XMLSlideShow();
>
>    XSLFSlide slide1 = ppt.createSlide();
>    XSLFTextBox shape1 = slide1.createTextBox();
>    Rectangle anchor = new Rectangle(170, 100, 300, 100);
>    shape1.setAnchor(anchor);
>
>    XSLFTextParagraph p1 = shape1.addNewTextParagraph();
>    XSLFTextRun r1 = p1.addNewTextRun();
>    r1.setText("The Apache POI Project's mission is to create and maintain
>Java APIs for manipulating various file formats based upon the Office Open
>XML standards (OOXML) and Microsoft's OLE 2 Compound Document format
>(OLE2). In short, you can read and write MS Excel files using Java. In
>addition, you can read and write MS Word and MS PowerPoint files using
>Java. Apache POI is your Java Excel solution (for Excel 97-2008). We have a
>complete API for porting other OOXML and OLE2 formats and welcome others to
>participate.OLE2 files include most Microsoft Office files such as XLS,
>DOC, and PPT as well as MFC serialization API based file formats. Office
>OpenXML Format is the new standards based XML file format found in
>Microsoft Office 2007 and 2008. This includes XLSX, DOCX and PPTX.
>Microsoft opened the specifications to this format in October 2007. We
>would welcome contributions.");
>
>    shape1.setTextAutofit(TextAutofit.NORMAL);
>
>    FileOutputStream out = new FileOutputStream("text.pptx");
>    ppt.write(out);
>    out.close();
>    }
>
>
>-- 
>
>*Apoorva Chaudhari*
>
>The information in this email is confidential and is intended solely for the
>addressee(s). Access to this email by anyone else is unauthorized. If you are 
>not
>an intended recipient, please notify the sender of this email immediately. You
>should not copy, use or disseminate the information contained in the email.
>Any views expressed in this message are those of the individual sender, except
>where the sender specifically states them to be the views of Capco.
>http://www.capco.com
>
>
>

Reply via email to