poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paoim101084 <paokimsr...@gmail.com>
Subject Re: How display text fits with the width of column?
Date Tue, 18 Dec 2018 16:58:23 GMT
@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


Mime
View raw message