itextpdf table使用
itextpdf table使用
? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
****************
相關(guān)類(lèi)與接口
? ? ? ? ? ? ? ? ?
Table
public class Table extends BlockElement<Table> implements ILargeElement {protected DefaultAccessibilityProperties tagProperties;private List<Cell[]> rows;private UnitValue[] columnWidths;private int currentColumn;private int currentRow;private Table header;private Table footer;private boolean skipFirstHeader;private boolean skipLastFooter;private boolean isComplete;private List<Table.RowRange> lastAddedRowGroups;private int rowWindowStart;private Document document;private Cell[] lastAddedRow;private Div caption;********* 構(gòu)造方法public Table(int numColumns) { //直接指定表的列數(shù)public Table(UnitValue[] columnWidths) { //數(shù)組長(zhǎng)度為列的個(gè)數(shù),數(shù)組節(jié)點(diǎn)的值為列寬public Table(float[] pointColumnWidths) { //數(shù)組長(zhǎng)度為列的個(gè)數(shù),數(shù)組節(jié)點(diǎn)的值為列寬public Table(int numColumns, boolean largeTable) {public Table(float[] columnWidths, boolean largeTable) {public Table(UnitValue[] columnWidths, boolean largeTable) {********* headerCellpublic Table addHeaderCell(Image image) {public Table addHeaderCell(String content) {public Table addHeaderCell(Cell headerCell) {public <T extends IElement> Table addHeaderCell(BlockElement<T> blockElement) {public Table getHeader() {public boolean isSkipFirstHeader() {public Table setSkipFirstHeader(boolean skipFirstHeader) {********* cellpublic Table addCell(Cell cell) {public Table addCell(Image image) {public Table addCell(String content) {public <T extends IElement> Table addCell(BlockElement<T> blockElement) {public Cell getCell(int row, int column) {********* footerCellpublic Table addFooterCell(Cell footerCell) {public Table addFooterCell(Image image) {public Table addFooterCell(String content) {public <T extends IElement> Table addFooterCell(BlockElement<T> blockElement) {public Table getFooter() {public boolean isSkipLastFooter() {public Table setSkipLastFooter(boolean skipLastFooter) {********* 其余操作public Table setCaption(Div caption) { //設(shè)置標(biāo)題public Table setCaption(Div caption, CaptionSide side) {public Div getCaption() {public IRenderer createRendererSubTree() {public IRenderer getRenderer() {public void complete() {public boolean isComplete() {public void flush() {public void flushContent() {public void setDocument(Document document) {public Table setExtendBottomRow(boolean isExtended) {public Table setExtendBottomRowOnSplit(boolean isExtended) {public Table setBorderCollapse(BorderCollapsePropertyValue collapsePropertyValue) {public Table setHorizontalBorderSpacing(float spacing) {public Table setVerticalBorderSpacing(float spacing) {public List<Border> getLastRowBottomBorder() {public AccessibilityProperties getAccessibilityProperties() {public Table startNewRow() { //開(kāi)啟新的一行public Table setFixedLayout() {public Table setAutoLayout() {public Table useAllAvailableWidth() {public UnitValue getColumnWidth(int column) {public int getNumberOfColumns() {public int getNumberOfRows() {protected IRenderer makeNewRenderer() {protected List<Table.RowRange> getRowGroups() {private void initializeRows() {this.rows = new ArrayList();this.currentColumn = -1;}private void ensureHeaderIsInitialized() {private void ensureFooterIsInitialized() {private void ensureCaptionPropertiesAreSet() {private void initializeLargeTable(boolean largeTable) {private static UnitValue[] normalizeColumnWidths(int numberOfColumns) {private static UnitValue[] normalizeColumnWidths(float[] pointColumnWidths) {private static UnitValue[] normalizeColumnWidths(UnitValue[] unitColumnWidths) {private boolean cellBelongsToAnyRowGroup(Cell cell, List<Table.RowRange> rowGroups) {********** 內(nèi)部類(lèi):RowRangepublic static class RowRange {int startRow;int finishRow;public RowRange(int startRow, int finishRow) {public int getStartRow() {public int getFinishRow() {? ? ? ? ? ? ? ? ??
Cell:單元格
public class Cell extends BlockElement<Cell> {private static final Border DEFAULT_BORDER = new SolidBorder(0.5F);private int row; //行索引private int col; //列索引private int rowspan; //行寬private int colspan; //列寬protected DefaultAccessibilityProperties tagProperties;********** 構(gòu)造方法public Cell(int rowspan, int colspan) {public Cell() {********** 普通方法public int getRow() {public int getCol() {public int getRowspan() {public int getColspan() {public Cell add(IBlockElement element) {public Cell add(Image element) {public IRenderer getRenderer() {public <T1> T1 getDefaultProperty(int property) {public AccessibilityProperties getAccessibilityProperties() {public String toString() {public Cell clone(boolean includeContent) {protected IRenderer makeNewRenderer() {protected Cell updateCellIndexes(int row, int col, int numberOfColumns) {? ? ? ? ? ? ? ? ? ? ? ? ?
UnitValue:單元格寬度
public class UnitValue {public static final int POINT = 1;public static final int PERCENT = 2;protected int unitType;protected float value;public UnitValue(int unitType, float value) {public UnitValue(UnitValue unitValue) {public static UnitValue createPointValue(float value) {public static UnitValue createPercentValue(float value) {public static UnitValue[] createPercentArray(float[] values) {public static UnitValue[] createPercentArray(int size) {public static UnitValue[] createPointArray(float[] values) {public void setValue(float value) {public void setUnitType(int unitType) {public float getValue() {public int getUnitType() {public boolean isPointValue() {public boolean isPercentValue() {public boolean equals(Object obj) {public int hashCode() {public String toString() {BlockElement:頁(yè)邊距、內(nèi)邊距、高寬度、對(duì)齊方式等設(shè)置
public abstract class BlockElement<T extends IElement> extends AbstractElement<T> implements IAccessibleElement, IBlockElement {protected BlockElement() {}public <T1> T1 getDefaultProperty(int property) {switch(property) {case 103:case 104:return OverflowPropertyValue.FIT;default:return super.getDefaultProperty(property);}}*********** 內(nèi)邊距:文本內(nèi)容到邊框的距離public T setPaddingLeft(float value) {public T setPaddingRight(float value) {public T setPaddingTop(float value) {public T setPaddingBottom(float value) {public UnitValue getPaddingLeft() {public UnitValue getPaddingRight() {public UnitValue getPaddingTop() {public UnitValue getPaddingBottom() {public T setPadding(float commonPadding) {public T setPaddings(float paddingTop, float paddingRight, float paddingBottom, float paddingLeft) {*********** 頁(yè)邊距:邊框到頁(yè)邊緣的距離public T setMarginLeft(float value) {public T setMarginRight(float value) {public T setMarginTop(float value) {public T setMarginBottom(float value) {public UnitValue getMarginLeft() {public UnitValue getMarginRight() {public UnitValue getMarginTop() {public UnitValue getMarginBottom() {public T setMargin(float commonMargin) {public T setMargins(float marginTop, float marginRight, float marginBottom, float marginLeft) {*********** 寬度、高度public T setWidth(float width) {public T setWidth(UnitValue width) {public UnitValue getWidth() {public T setHeight(UnitValue height) {public T setHeight(float height) {public UnitValue getHeight() {public T setMaxHeight(float maxHeight) {public T setMaxHeight(UnitValue maxHeight) {public T setMinHeight(UnitValue minHeight) {public T setMinHeight(float minHeight) {public T setMaxWidth(UnitValue maxWidth) {public T setMaxWidth(float maxWidth) {public T setMinWidth(UnitValue minWidth) {public T setMinWidth(float minWidth) {*********** 其余操作public T setVerticalAlignment(VerticalAlignment verticalAlignment) {public T setSpacingRatio(float ratio) {public Boolean isKeepTogether() {public T setKeepTogether(boolean keepTogether) {public Boolean isKeepWithNext() {public T setKeepWithNext(boolean keepWithNext) {public T setRotationAngle(float angleInRadians) {public T setRotationAngle(double angleInRadians) {? ? ? ? ? ? ? ? ? ? ? ?
AbstractElement:文件樣式、pdf動(dòng)作、頁(yè)碼等設(shè)置
public abstract class AbstractElement<T extends IElement> extends ElementPropertyContainer<T> implements IAbstractElement {protected IRenderer nextRenderer;protected List<IElement> childElements = new ArrayList();protected Set<Style> styles;public AbstractElement() {}public IRenderer createRendererSubTree() {public void setNextRenderer(IRenderer renderer) {public IRenderer getRenderer() {public T addStyle(Style style) {public T setAction(PdfAction action) {public T setPageNumber(int pageNumber) {public boolean isEmpty() {public List<IElement> getChildren() {public boolean hasProperty(int property) {public <T1> T1 getProperty(int property) {protected abstract IRenderer makeNewRenderer(); }? ? ? ? ? ? ? ? ? ?
ElementPropertyContainer:位置、邊框、字體、背景等設(shè)置
public abstract class ElementPropertyContainer<T extends IPropertyContainer> extends AbstractIdentifiableElement implements IPropertyContainer {protected Map<Integer, Object> properties = new HashMap();public ElementPropertyContainer() {}********* propertypublic boolean hasProperty(int property) {public <T1> T1 getProperty(int property) {public void setProperty(int property, Object value) {public boolean hasOwnProperty(int property) {public <T1> T1 getOwnProperty(int property) {public void deleteOwnProperty(int property) {public <T1> T1 getDefaultProperty(int property) {********* positionpublic T setRelativePosition(float left, float top, float right, float bottom) {public T setFixedPosition(float left, float bottom, float width) {public T setFixedPosition(float left, float bottom, UnitValue width) {public T setFixedPosition(int pageNumber, float left, float bottom, float width) {public T setFixedPosition(int pageNumber, float left, float bottom, UnitValue width) {********* fontpublic T setFont(PdfFont font) {public T setFontFamily(String... fontFamilyNames) {public T setFontFamily(List<String> fontFamilyNames) {public T setFontColor(Color fontColor) {public T setFontColor(Color fontColor, float opacity) {public T setFontSize(float fontSize) {public T setFontKerning(FontKerning fontKerning) {public T setFontScript(UnicodeScript script) {public T setBold() { //字體加粗public T setItalic() { //字體傾斜********* backgroundpublic T setBackgroundColor(Color backgroundColor) {public T setBackgroundColor(Color backgroundColor, float opacity) {public T setBackgroundColor(Color backgroundColor, float extraLeft, float extraTop, float extraRight, float extraBottom) {public T setBackgroundColor(Color backgroundColor, float opacity, float extraLeft, float extraTop, float extraRight, float extraBottom) {public T setBackgroundImage(BackgroundImage image) {public T setBackgroundImage(List<BackgroundImage> imagesList) {********* borderpublic T setBorder(Border border) {public T setBorderTop(Border border) {public T setBorderLeft(Border border) {public T setBorderRight(Border border) {public T setBorderBottom(Border border) {public T setBorderRadius(BorderRadius borderRadius) {public T setBorderTopLeftRadius(BorderRadius borderRadius) {public T setBorderTopRightRadius(BorderRadius borderRadius) {public T setBorderBottomRightRadius(BorderRadius borderRadius) {public T setBorderBottomLeftRadius(BorderRadius borderRadius) {public T setStrokeColor(Color strokeColor) {public Color getStrokeColor() { //邊框顏色public T setStrokeWidth(float strokeWidth) {public Float getStrokeWidth() { //邊框?qū)挾?********* underlinepublic T setUnderline() { //文本加下劃線public T setLineThrough() { //文本加刪除線public T setUnderline(float thickness, float yPosition) {public T setUnderline(Color color, float thickness, float thicknessMul, float yPosition, float yPositionMul, int lineCapStyle) {public T setUnderline(Color color, float opacity, float thickness, float thicknessMul, float yPosition, float yPositionMul, int lineCapStyle) {********* 其余操作public T setHorizontalAlignment(HorizontalAlignment horizontalAlignment) {public T setTextAlignment(TextAlignment alignment) { //文本對(duì)齊方式public T setCharacterSpacing(float charSpacing) {public T setWordSpacing(float wordSpacing) { //單詞間距public T setTextRenderingMode(int textRenderingMode) {public Integer getTextRenderingMode() { //文本渲染方式public T setSplitCharacters(ISplitCharacters splitCharacters) {public ISplitCharacters getSplitCharacters() { //文本分隔符public T setBaseDirection(BaseDirection baseDirection) {public T setDestination(String destination) {public T setOpacity(Float opacity) { //文本透明度public T setHyphenation(HyphenationConfig hyphenationConfig) { //設(shè)置連字符? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
****************
示例
? ? ? ? ? ? ? ? ? ? ? ?
public class Test4 {private static final String dest = "./table/1.pdf";private static final String dest2 = "./table/2.pdf";private static final String dest3 = "./table/3.pdf";private static final String dest4 = "./table/4.pdf";public static void fun() throws Exception{PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));Document document = new Document(pdfDocument);document.setFont(PdfFontFactory.createFont("STSong-Light","UniGB-UCS2-H"));Table table = new Table(UnitValue.createPercentArray(6)).useAllAvailableWidth();for (int i=0;i<12;i++){table.addCell("瓜田李下 "+i);}document.add(table);document.close();}public static void fun2() throws Exception{PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest2));Document document = new Document(pdfDocument);document.setFont(PdfFontFactory.createFont("STSong-Light","UniGB-UCS2-H"));Table table = new Table(UnitValue.createPercentArray(6)).useAllAvailableWidth();table.addCell(new Cell(2,1) //添加占2行、1列的單元格.add(new Paragraph("瓜田李下")).setTextAlignment(TextAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE).setBackgroundColor(ColorConstants.GREEN));for (int i=0;i<10;i++){table.addCell("瓜田李下 "+i);}document.add(table);document.close();}public static void fun3() throws Exception{ //headerCell、footerCellPdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest3));Document document = new Document(pdfDocument);document.setFont(PdfFontFactory.createFont("STSong-Light","UniGB-UCS2-H"));Table table = new Table(UnitValue.createPercentArray(6)).useAllAvailableWidth();table.addHeaderCell(new Cell(1,6).add(new Paragraph("瓜田李下 header")).setTextAlignment(TextAlignment.CENTER));table.addCell(new Cell(2,1).add(new Paragraph("瓜田李下")).setTextAlignment(TextAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE).setBackgroundColor(ColorConstants.GREEN));for (int i=0;i<10;i++){table.addCell("瓜田李下 "+i);}table.addFooterCell(new Cell(1,6).add(new Paragraph("瓜田李下 footer")).setTextAlignment(TextAlignment.CENTER));document.add(table);document.close();}public static void fun4() throws Exception{ //captionPdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest4));Document document = new Document(pdfDocument);document.setFont(PdfFontFactory.createFont("STSong-Light","UniGB-UCS2-H"));Table table = new Table(UnitValue.createPercentArray(6)).useAllAvailableWidth();table.setCaption(new Div().add(new Paragraph("caption").setTextAlignment(TextAlignment.CENTER).setBackgroundColor(ColorConstants.CYAN)), CaptionSide.TOP);table.addHeaderCell(new Cell(1,6).add(new Paragraph("瓜田李下 header")).setTextAlignment(TextAlignment.CENTER));table.addCell(new Cell(2,1).add(new Paragraph("瓜田李下")).setTextAlignment(TextAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE).setBackgroundColor(ColorConstants.GREEN));for (int i=0;i<10;i++){table.addCell("瓜田李下 "+i);}table.addFooterCell(new Cell(1,6).add(new Paragraph("瓜田李下 footer")).setTextAlignment(TextAlignment.CENTER));document.add(table);document.close();}public static void main(String[] args) throws Exception{File file = new File(dest);file.getParentFile().mkdirs();fun();fun2();fun3();fun4();} }????????????
fun
??????
??????????
fun2:添加占2行1列的單元格
??????
????????????
fun3:添加headerCell、footerCell
??????
??????????
fun4:添加caption
??????
?說(shuō)明:caption不在表格范圍內(nèi),在表格上方或者下方
????????????????????????????????????????????????????
???????????????????????????????????????????????????????????
總結(jié)
以上是生活随笔為你收集整理的itextpdf table使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Unicode编码详解
- 下一篇: C语言实数除法怎样保留小数(编程技巧)