You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by paoim101084 <pa...@gmail.com> on 2018/11/29 02:56:09 UTC

How display text fits with the width of column?

In Power point, I have a table with 4 rows and 6 columns.
For example:
ID, Name, Description, Price, Percent, Current Value
123,Car, It is new car in 2019, $40000, 88%, $38000

How to make text fit to each columns?




--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: How display text fits with the width of column?

Posted by paoim101084 <pa...@gmail.com>.
@Kiwiwings:
Now I have modified your code to make similar on what I did in actual
project.
Here is the whole demo code:
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.poi.sl.draw.DrawTableShape;
import org.apache.poi.sl.usermodel.TextShape;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTable;
import org.apache.poi.xslf.usermodel.XSLFTableCell;
import org.apache.poi.xslf.usermodel.XSLFTableRow;
import org.apache.poi.xslf.usermodel.XSLFTextRun;

public class TableResizeDemo {

	public static void main(String[] args) throws Exception {
		String[][] data = {
				{ "ID", "Name", "Description", "Price", "Percent", "Current Value" },
				{ "123", "Car", "It is new car in 2019", "$40000", "88%", "$38000" },
				{ "123", "Car", "It is new car in 2019", "$40000", "88%", "$38000" },
				{ "123", "Car", "It is new car in 2019", "$40000", "88%", "$38000" },
				{ "123", "Car", "It is new car in 2019", "$40000", "88%", "$38000" }
			};
		
		try (XMLSlideShow ppt = new XMLSlideShow()) {
			XSLFSlide slide = ppt.createSlide();

			Rectangle2D nextAnchor = displayTable(slide, data, null);//First
			displayTable(slide, data, nextAnchor);//Second

			try (FileOutputStream fos = new
FileOutputStream("output/tableTest.pptx")) {
				ppt.write(fos);
			}
		}
	}
	
	static Rectangle2D displayTable(XSLFSlide slide, String[][] data,
Rectangle2D nextAnchor) {
		XSLFTable tab = slide.createTable(data.length, data[0].length);

		for (int row = 0; row < data.length; row++) {
			for (int col = 0; col < data[row].length; col++) {
				XSLFTableCell c = tab.getCell(row, col);
				c.setText(data[row][col]);
				c.setTextAutofit(TextShape.TextAutofit.SHAPE);
				XSLFTextRun tr = c.getTextParagraphs().get(0).getTextRuns().get(0);
				double fontSize = (0 == row) ? 20. : 15.;
				tr.setFontSize(fontSize);
			}
		}
		tab.setColumnWidth(0, 120);//FIXME - issue?
		tab.setColumnWidth(1, 120);//FIXME - issue?

		// Calculate Gap between tables
		Rectangle2D rect = (null != nextAnchor) ? getGapAnchor(nextAnchor, 20) :
new Rectangle2D.Double(40, 40, tab.getAnchor().getWidth(),
tab.getAnchor().getHeight());
		tab.setAnchor(rect);
		
		// Calculate Table's height
		calculateTableHeight(tab, tab.getAnchor());
		
		// Get Next Anchor
		Rectangle2D anchor = getNextAnchor(tab.getAnchor());
		
		DrawTableShape dts = new DrawTableShape(tab);
        dts.setOutsideBorders(Color.DARK_GRAY, 1.0);
		
		return anchor;
	}
	
	static void calculateTableHeight(XSLFTable tab, Rectangle2D anchor)
{//FIXME - issue?
		double totalCellAnchorHeight = 0;
		for (XSLFTableRow row : tab.getRows()) {
			/*for (XSLFTableCell cell : row.getCells()) {
				cell.resizeToFitText(); //make Cell Height fits with Text Height
			}*/
			List<Double> cellAnchorHeightList = row.getCells().stream().map(c ->
c.getAnchor().getHeight()).collect(Collectors.toList());
			double maxCellAnchorHeight = Collections.max(cellAnchorHeightList);// Get
only Max Cell Anchor's height
			totalCellAnchorHeight += maxCellAnchorHeight;
		}
		Rectangle2D newAnchor = new Rectangle2D.Double(anchor.getX(),
anchor.getY(), anchor.getWidth(), totalCellAnchorHeight);
		tab.setAnchor(newAnchor);
	}
	
	static Rectangle2D getNextAnchor(Rectangle2D anchor) {//FIXME - issue?
		return new Rectangle2D.Double(anchor.getX(), anchor.getMaxY(),
anchor.getWidth(), anchor.getHeight());
	}
	
	static Rectangle2D getGapAnchor(Rectangle2D anchor, int gap) {//FIXME -
issue?
		return new Rectangle2D.Double(anchor.getX(), anchor.getY() + gap,
anchor.getWidth(), anchor.getHeight());
	}
}

When you run that program, you will see two issues as I wrote:
1. Display text does not fit with column. 
For example, less text should display in small column. long text should 
display in big column. 

2. Calculate table's height, so I can display two or more tables in the same 
slide. 
I mean after display one table and then need to display second table.

It seems work well with static data but when I applied real data, it does
not work well.




--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: How display text fits with the width of column?

Posted by paoim101084 <pa...@gmail.com>.
Hi Andi,

Thank for detail explanation.
I am looking forward to getting the @2.



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: How display text fits with the width of column?

Posted by Andreas Beeker <ki...@apache.org>.
On 18.12.18 16:46, paoim101084 wrote:
> Yes, I have two issues when I used XSLFTable.
> 1. Display text does not fit with column.
> For example, less text should display in small column. long text should
> display in big column.
>
> 2. Calculate table's height, so I can display two or more tables in the same
> slide.
> I mean after display one table and then need to display second table.
>
> Great if you can help.
>
@1. that was the too much effort part. Opposed to Excels "find optimal width for one column", which doesn't change the size of the other columns - I would need to change the other columns width in case of long texts.
This is similar to FOPs table layout <auto> ... Maybe having one auto column (e.g. width = "*")  could be done,
but you also need to consider hyphenation for text spanning more than one line when doing this.
Long story short, this is not on my todo list.

@2. I'll check, what I can provide here on POIs table API. this will be something similar to your calculateTableHeight method, i.e. extended the anchor height after a row was added to the table.

Andi


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: How display text fits with the width of column?

Posted by paoim101084 <pa...@gmail.com>.
Yes, I have two issues when I used XSLFTable.
1. Display text does not fit with column.
For example, less text should display in small column. long text should
display in big column.

2. Calculate table's height, so I can display two or more tables in the same
slide.
I mean after display one table and then need to display second table.

Great if you can help.



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: How display text fits with the width of column?

Posted by Andreas Beeker <ki...@apache.org>.
On 17.12.18 22:17, paoim101084 wrote:
> Do you know how to autoResize the column in XSLFTable?
>
> I got painful when I use XSLFTable. I cannot calculate the exact table
> height.

I've just realized that the table height is not updated [1], only the bounding boxes of the cells are updated on rendering. So I need to rework that, but ...

So currently you are left with setting the column width and the start position of the table anchor.
Why do you need to calculate it's height?

Do you want something like an autofit option, where you set the table content and the column sizes should adapt?
I thought about this, but this is kind of an optimization where you need to find a minima on all row heights/column widths and for a table in slideshow this seems to be a bit too much effort.

If you generate such a price list and want to know when to continue on the next page, that's something feasible for my mentioned rework ... so you would add a row, check the table height and eventually jump to the next page.

Andi


[1] Test program to show that table is displayed more or less ok, when not knowing it's size:


public void testResize()throws Exception {
     String[][] data = {
         {"ID","Name","Description","Price","Percent","Current Value" },
         {"123","Car","It is new car in 2019","$40000","88%","$38000"},
         {"123","Car","It is new car in 2019","$40000","88%","$38000"},
         {"123","Car","It is new car in 2019","$40000","88%","$38000"}
     };

     XMLSlideShow ppt =new XMLSlideShow();
     XSLFSlide slide = ppt.createSlide();
     // a red box in the background, to show/verify the table dimensions XSLFAutoShape as = slide.createAutoShape();
     XSLFTable tab = slide.createTable(data.length, data[0].length);

     for (int row=0; row<data.length; row++) {
         for (int col=0; col<data[row].length; col++) {
             XSLFTableCell c = tab.getCell(row, col);
             c.setText(data[row][col]);
             c.setTextAutofit(TextShape.TextAutofit.SHAPE);
             XSLFTextRun tr = c.getTextParagraphs().get(0).getTextRuns().get(0);
             tr.setFontSize(20.);
         }
     }
     tab.setColumnWidth(0,60);
     tab.setColumnWidth(1,60);

     // this is not working correctly, the width/height is 0 Rectangle2D rect = tab.getAnchor();
     rect =new Rectangle2D.Double(100,100,rect.getWidth(),rect.getHeight());
     tab.setAnchor(rect);

     as.setShapeType(ShapeType.RECT);
     as.setFillColor(Color.RED);
     as.setAnchor(rect);

     try (FileOutputStream fos =new FileOutputStream("tabtest.pptx")) {
         ppt.write(fos);
     }
}


Re: How display text fits with the width of column?

Posted by paoim101084 <pa...@gmail.com>.
Do you know how to autoResize the column in XSLFTable?

I got painful when I use XSLFTable. I cannot calculate the exact table
height. 



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: How display text fits with the width of column?

Posted by Rob Sargent <ro...@gmail.com>.
You can autoResize the column

> On Nov 28, 2018, at 7:56 PM, paoim101084 <pa...@gmail.com> wrote:
> 
> In Power point, I have a table with 4 rows and 6 columns.
> For example:
> ID, Name, Description, Price, Percent, Current Value
> 123,Car, It is new car in 2019, $40000, 88%, $38000
> 
> How to make text fit to each columns?
> 
> 
> 
> 
> --
> Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: How display text fits with the width of column?

Posted by Rob Sargent <ro...@gmail.com>.
Getting the length in mm of a string is tricky. Type face and font size etc come into play. Better off producing the one line you describe then in PP adjust the widths off the columns to what looks best. Record the widths and use in your code.  

> On Nov 28, 2018, at 7:56 PM, paoim101084 <pa...@gmail.com> wrote:
> 
> In Power point, I have a table with 4 rows and 6 columns.
> For example:
> ID, Name, Description, Price, Percent, Current Value
> 123,Car, It is new car in 2019, $40000, 88%, $38000
> 
> How to make text fit to each columns?
> 
> 
> 
> 
> --
> Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org