Itext Demo

Posted 明湖樵夫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Itext Demo相关的知识,希望对你有一定的参考价值。

Tables and fonts

/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/27577633/itext-library-exception-throwing-on-adding-blank-cell-with-space
 */
package sandbox.tables;   import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import sandbox.WrapToTest;   import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;   @WrapToTest
public class CellMethod {
    public static final String DEST = "results/tables/cell_method.pdf";
    public static final String FONT = "resources/fonts/FreeSans.ttf";   public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CellMethod().createPdf(DEST);
    }   public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.addCell("Winansi");
        table.addCell(getNormalCell("Test", null, 12));
        table.addCell("Winansi");
        table.addCell(getNormalCell("Test", null, -12));
        table.addCell("Greek");
        table.addCell(getNormalCell("\u039d\u03cd\u03c6\u03b5\u03c2", "greek", 12));
        table.addCell("Czech");
        table.addCell(getNormalCell("\u010c,\u0106,\u0160,\u017d,\u0110", "czech", 12));
        table.addCell("Test");
        table.addCell(getNormalCell(" ", null, 12));
        table.addCell("Test");
        table.addCell(getNormalCell(" ", "greek", 12));
        table.addCell("Test");
        table.addCell(getNormalCell(" ", "czech", 12));
        document.add(table);
        document.close();
    }   public static PdfPCell getNormalCell(String string, String language, float size)
            throws DocumentException, IOException {
        if(string != null && "".equals(string)){
            return new PdfPCell();
        }
        Font f  = getFontForThisLanguage(language);
        if(size < 0) {
            f.setColor(BaseColor.RED);
            size = -size;
        }
        f.setSize(size);
        PdfPCell cell = new PdfPCell(new Phrase(string, f));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        return cell;
    }
    public static Font getFontForThisLanguage(String language) {
        if ("czech".equals(language)) {
            return FontFactory.getFont(FONT, "Cp1250", true);
        }
        if ("greek".equals(language)) {
            return FontFactory.getFont(FONT, "Cp1253", true);
        }
        return FontFactory.getFont(FONT, null, true);
    }
}

 

/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/29548762/why-is-my-table-not-being-generated-on-my-pdf-file-using-itextsharp
 */
package sandbox.tables;   import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;   @WrapToTest
public class SimpleTable7 {
    public static final String DEST = "results/tables/simple_table7.pdf";   public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleTable7().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        Font titleFont = FontFactory.getFont(FontFactory.COURIER_BOLD, 11, BaseColor.BLACK);
        Paragraph docTitle = new Paragraph("UCSC Direct - Direct Payment Form", titleFont);
        document.add(docTitle);
        Font subtitleFont = FontFactory.getFont("Times Roman", 9, BaseColor.BLACK);
        Paragraph subTitle = new Paragraph("(not to be used for reimbursement of services)", subtitleFont);
        document.add(subTitle);
        Font importantNoticeFont = FontFactory.getFont("Courier", 9, BaseColor.RED);
        Paragraph importantNotice = new Paragraph("Important: Form must be filled out in Adobe Reader or Acrobat Professional 8.1 or above. To save completed forms, Acrobat Professional is required. For technical and accessibility assistance, contact the Campus Controller‘s Office.", importantNoticeFont);
        document.add(importantNotice);   PdfPTable table = new PdfPTable(10); // the arg is the number of columns
        PdfPCell cell = new PdfPCell(docTitle);
        cell.setColspan(3);
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);
        PdfPCell cellCaveat = new PdfPCell(subTitle);
        cellCaveat.setColspan(2);
        cellCaveat.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cellCaveat);
        PdfPCell cellImportantNote = new PdfPCell(importantNotice);
        cellImportantNote.setColspan(5);
        cellImportantNote.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cellImportantNote);
        document.add(table);   document.add(new Paragraph(20, "This is the same table, created differently", subtitleFont));
        table = new PdfPTable(3);
        table.setWidths(new int[]{3, 2, 5});
        cell.setColspan(1);
        table.addCell(cell);
        cellCaveat.setColspan(1);
        table.addCell(cellCaveat);
        cellImportantNote.setColspan(1);
        table.addCell(cellImportantNote);
        document.add(table);   document.close();
    }  }
Table and cell events to draw borders
package sandbox.tables;   import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPTableEvent;
import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;   import sandbox.WrapToTest;   @WrapToTest
public class DottedLineCell {   public static final String DEST = "results/tables/dotted_line_cell.pdf";   class DottedCells implements PdfPTableEvent {   public void tableLayout(PdfPTable table, float[][] widths,
            float[] heights, int headerRows, int rowStart,
            PdfContentByte[] canvases) {
            PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
            canvas.setLineDash(3f, 3f);
            float llx = widths[0][0];
            float urx = widths[0][widths.length];
            for (int i = 0; i < heights.length; i++) {
                canvas.moveTo(llx, heights[i]);
                canvas.lineTo(urx, heights[i]);
            }
            for (int i = 0; i < widths.length; i++) {
                for (int j = 0; j < widths[i].length; j++) {
                    canvas.moveTo(widths[i][j], heights[i]);
                    canvas.lineTo(widths[i][j], heights[i+1]);
                }
            }
            canvas.stroke();
        }
    }   class DottedCell implements PdfPCellEvent {
        public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
            PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
            canvas.setLineDash(3f, 3f);
            canvas.rectangle(position.getLeft(), position.getBottom(),
                position.getWidth(), position.getHeight());
            canvas.stroke();
        }
    }   public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new DottedLineCell().createPdf(DEST);
    }   public void createPdf(String dest) throws IOException, DocumentException {
        DottedLineCell app = new DottedLineCell();
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        document.add(new Paragraph("Table event"));
        PdfPTable table = new PdfPTable(3);
        table.setTableEvent(app.new DottedCells());
        table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        table.addCell("A1");
        table.addCell("A2");
        table.addCell("A3");
        table.addCell("B1");
        table.addCell("B2");
        table.addCell("B3");
        table.addCell("C1");
        table.addCell("C2");
        table.addCell("C3");
        document.add(table);
        document.add(new Paragraph("Cell event"));
        table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell(new Phrase("Test"));
        cell.setCellEvent(app.new DottedCell());
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}

RoundedCorners.java

/**
 * This example was written by Bruno Lowagie in answer to the following questions:
 * http://stackoverflow.com/questions/30106862/left-and-right-top-round-corner-for-rectangelroundrectangle
 */
package sandbox.tables;   import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;   @WrapToTest
public class RoundedCorners {   public static final String DEST = "results/tables/rounded_corners.pdf";   class SpecialRoundedCell implements PdfPCellEvent {
        public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
            PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
            float llx = position.getLeft() + 2;
            float lly = position.getBottom() + 2;
            float urx = position.getRight() - 2;
            float ury = position.getTop() - 2;
            float r = 4;
            float b = 0.4477f;
            canvas.moveTo(llx, lly);
            canvas.lineTo(urx, lly);
            canvas.lineTo(urx, ury - r);
            canvas.curveTo(urx, ury - r * b, urx - r * b, ury, urx - r, ury);
            canvas.lineTo(llx + r, ury);
            canvas.curveTo(llx + r * b, ury, llx, ury - r * b, llx, ury - r);
            canvas.lineTo(llx, lly);
            canvas.stroke();
        }
    }   public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RoundedCorners().createPdf(DEST);
    }   public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(3);
        PdfPCell cell = getCell("These cells have rounded borders at the top.");
        table.addCell(cell);
        cell = getCell("These cells aren‘t rounded at the bottom.");
        table.addCell(cell);
        cell = getCell("A custom cell event was used to achieve this.");
        table.addCell(cell);
        document.add(table);
        document.close();
    }   public PdfPCell getCell(String content) {
        PdfPCell cell = new PdfPCell(new Phrase(content));
        cell.setCellEvent(new SpecialRoundedCell());
        cell.setPadding(5);
        cell.setBorder(PdfPCell.NO_BORDER);
        return cell;
    }
}

DottedLineHeader.java

package sandbox.tables;   import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPTableEvent;
import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;   @WrapToTest
public class DottedLineHeader {   public static final String DEST = "results/tables/dotted_line_header.pdf";   class DottedHeader implements PdfPTableEvent {   public void tableLayout(PdfPTable table, float[][] widths,
            float[] heights, int headerRows, int rowStart,
            PdfContentByte[] canvases) {
            PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
            canvas.setLineDash(3f, 3f);
            float x1 = widths[0][0];
            float x2 = widths[0][widths.length];
            canvas.moveTo(x1, heights[0]);
            canvas.lineTo(x2, heights[0]);
            canvas.moveTo(x1, heights[headerRows]);
            canvas.lineTo(x2, heights[headerRows]);
            canvas.stroke();
        }
    }   class DottedCell implements PdfPCellEvent {
        public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
            PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
            canvas.setLineDash(3f, 3f);
            canvas.moveTo(position.getLeft(), position.getTop());
            canvas.lineTo(position.getRight(), position.getTop());
            canvas.moveTo(position.getLeft(), position.getBottom());
            canvas.lineTo(position.getRight(), position.getBottom());
            canvas.stroke();
        }
    }   public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new DottedLineHeader().createPdf(DEST);
    }   public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        document.add(new Paragraph("Table event"));
        PdfPTable table = new PdfPTable(3);
        table.setTableEvent(new DottedHeader());
        table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        table.addCell("A1");
        table.addCell("A2");
        table.addCell("A3");
        table.setHeaderRows(1);
        table.addCell("B1");
        table.addCell("B2");
        table.addCell("B3");
        table.addCell("C1");
        table.addCell("C2");
        table.addCell("C3");
        document.add(table);
        document.add(new Paragraph("Cell event"));
        table = new PdfPTable(3);
        table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        table.getDefaultCell().setCellEvent(new DottedCell());
        table.addCell("A1");
        table.addCell("A2");
        table.addCell("A3");
        table.getDefaultCell().setCellEvent(null);
        table.addCell("B1");
        table.addCell("B2");
        table.addCell("B3");
        table.addCell("C1");
        table.addCell("C2");
        table.addCell("C3");
        document.add(table);
        document.close();
    }
}

CustomBorder.java

/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/23935566/table-borders-not-expanding-properly-in-pdf-using-itext
 */
package sandbox.tables;   import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPRow;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPTableEventAfterSplit;
import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;   @WrapToTest
public class CustomBorder {   public static final String DEST = "results/tables/custom_border.pdf";   public static final String TEXT = "This is some long paragraph that will be added over and over again to prove a point. It should result in rows that are split and rows that aren‘t.";   public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CustomBorder().createPdf(DEST);
    }   class BorderEvent implements PdfPTableEventAfterSplit {   protected int rowCount;
        protected boolean bottom = true;
        protected boolean top = true;   public void setRowCount(int rowCount) {
        	this.rowCount = rowCount;
        }   public void splitTable(PdfPTable table) {
	    	if (table.getRows().size() != rowCount) {
    	        bottom = false;
	    	}
        }   public void afterSplitTable(PdfPTable table, PdfPRow startRow, int startIdx) {
        	if (table.getRows().size() != rowCount) {
            // if the table gains a row, a row was split
                rowCount = table.getRows().size();
                top = false;
            }
        }   public void tableLayout(PdfPTable table, float[][] width, float[] height,
                int headerRows, int rowStart, PdfContentByte[] canvas) {
            float widths[] = width[0];
            float y1 = height[0];
            float y2 = height[height.length - 1];
            PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
            for (int i = 0; i < widths.length; i++) {
                cb.moveTo(widths[i], y1);
                cb.lineTo(widths[i], y2);
            }
            float x1 = widths[0];
            float x2 = widths[widths.length - 1];
            for (int i = top ? 0 : 1; i < (bottom ? height.length : height.length - 1); i++) {
                cb.moveTo(x1, height[i]);
                cb.lineTo(x2, height[i]);
            }
            cb.stroke();
            cb.resetRGBColorStroke();
            bottom = true;
            top = true;
        }
    }   public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.setTotalWidth(500);
        table.setLockedWidth(true);
        BorderEvent event = new BorderEvent();
        table.setTableEvent(event);
        table.setWidthPercentage(100);
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        table.setSplitLate(false);
        PdfPCell cell = new PdfPCell(new Phrase(TEXT));
        cell.setBorder(Rectangle.NO_BORDER);
        for (int i = 0; i < 60; ) {
            table.addCell("Cell " + (++i));
            table.addCell(cell);
        }
        event.setRowCount(table.getRows().size());
        document.add(table);
        document.close();
    }
}

CustomBorder2.java

/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/23935566/table-borders-not-expanding-properly-in-pdf-using-itext
 */
package sandbox.tables;   import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPRow;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPTableEventAfterSplit;
import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;   @WrapToTest
public class CustomBorder2 {   public static final String DEST = "results/tables/custom_border2.pdf";   public static final String TEXT = "This is some long paragraph that will be added over and over again to prove a point. It should result in rows that are split and rows that aren‘t.";   public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CustomBorder2().createPdf(DEST);
    }   class BorderEvent implements PdfPTableEventAfterSplit {   protected boolean bottom = true;
        protected boolean top = true;   public void splitTable(PdfPTable table) {
    	    bottom = false;
        }   public void afterSplitTable(PdfPTable table, PdfPRow startRow, int startIdx) {
        	top = false;
        }   public void tableLayout(PdfPTable table, float[][] width, float[] height,
                int headerRows, int rowStart, PdfContentByte[] canvas) {
            float widths[] = width[0];
            float y1 = height[0];
            float y2 = height[height.length - 1];
            float x1 = widths[0];
            float x2 = widths[widths.length - 1];
            PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
            cb.moveTo(x1, y1);
            cb.lineTo(x1, y2);
            cb.moveTo(x2, y1);
            cb.lineTo(x2, y2);
            if (top) {
                cb.moveTo(x1, y1);
                cb.lineTo(x2, y1);
            }
            if (bottom) {
                cb.moveTo(x1, y2);
                cb.lineTo(x2, y2);
            }
            cb.stroke();
            cb.resetRGBColorStroke();
            bottom = true;
            top = true;
        }
    }   public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.setTotalWidth(500);
        table.setLockedWidth(true);
        BorderEvent event = new BorderEvent();
        table.setTableEvent(event);
        table.setWidthPercentage(100);
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        table.setSplitLate(false);
        PdfPCell cell = new PdfPCell(new Phrase(TEXT));
        cell.setBorder(Rectangle.NO_BORDER);
        for (int i = 0; i < 60; ) {
            table.addCell("Cell " + (++i));
            table.addCell(cell);
        }
        document.add(table);
        document.close();
    }
}

DottedLineCell2.java

package sandbox.tables;   import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;   import sandbox.WrapToTest;   @WrapToTest
public class DottedLineCell2 {   public static final String DEST = "results/tables/dotted_line_cell2.pdf";   class DottedCell implements PdfPCellEvent {
        private int border = 0;
        public DottedCell(int border) {
            this.border = border;
        }
        public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
            PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
            canvas.saveState();
            canvas.setLineDash(0, 4, 2);
            if ((border & PdfPCell.TOP) == PdfPCell.TOP) {
                canvas.moveTo(position.getRight(), position.getTop());
                canvas.lineTo(position.getLeft(), position.getTop());
            }
            if ((border & PdfPCell.BOTTOM) == PdfPCell.BOTTOM) {
                canvas.moveTo(position.getRight(), position.getBottom());
                canvas.lineTo(position.getLeft(), position.getBottom());
            }
            if ((border & PdfPCell.RIGHT) == PdfPCell.RIGHT) {
                canvas.moveTo(position.getRight(), position.getTop());
                canvas.lineTo(position.getRight(), position.getBottom());
            }
            if ((border & PdfPCell.LEFT) == PdfPCell.LEFT) {
                canvas.moveTo(position.getLeft(), position.getTop());
                canvas.lineTo(position.getLeft(), position.getBottom());
            }
            canvas.stroke();
            canvas.restoreState();
        }
    }   public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new DottedLineCell2().createPdf(DEST);
    }   public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();   PdfPTable table;
        PdfPCell cell;   table = new PdfPTable(4);
        table.setSpacingAfter(30);
        cell = new PdfPCell(new Phrase("left border"));
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setCellEvent(new DottedCell(PdfPCell.LEFT));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("right border"));
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setCellEvent(new DottedCell(PdfPCell.RIGHT));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("top border"));
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setCellEvent(new DottedCell(PdfPCell.TOP));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("bottom border"));
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setCellEvent(new DottedCell(PdfPCell.BOTTOM));
        table.addCell(cell);
        document.add(table);   table = new PdfPTable(4);
        table.setSpacingAfter(30);
        cell = new PdfPCell(new Phrase("left and top border"));
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setCellEvent(new DottedCell(PdfPCell.LEFT | PdfPCell.TOP));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("right and bottom border"));
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setCellEvent(new DottedCell(PdfPCell.RIGHT | PdfPCell.BOTTOM));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("no border"));
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("full border"));
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setCellEvent(new DottedCell(PdfPCell.BOX));
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}

CustomBorder3.java

package sandbox.tables;   import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;   import sandbox.WrapToTest;   @WrapToTest
public class CustomBorder3 {   public static final String DEST = "results/tables/custom_border_3.pdf";   interface LineDash {
        public void applyLineDash(PdfContentByte canvas);
    }   class SolidLine implements LineDash {
        public void applyLineDash(PdfContentByte canvas) { }
    }   class DottedLine implements LineDash {
        public void applyLineDash(PdfContentByte canvas) {
            canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND);
            canvas.setLineDash(0, 4, 2);
        }
    }   class DashedLine implements LineDash {
        public void applyLineDash(PdfContentByte canvas) {
            canvas.setLineDash(3, 3);
        }
    }   class CustomBorder implements PdfPCellEvent {
        protected LineDash left;
        protected LineDash right;
        protected LineDash top;
        protected LineDash bottom;
        public CustomBorder(LineDash left, LineDash right,
                LineDash top, LineDash bottom) {
            this.left = left;
            this.right = right;
            this.top = top;
            this.bottom = bottom;
        }
        public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
            PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
            if (top != null) {
                canvas.saveState();
                top.applyLineDash(canvas);
                canvas.moveTo(position.getRight(), position.getTop());
                canvas.lineTo(position.getLeft(), position.getTop());
                canvas.stroke();
                canvas.restoreState();
            }
            if (bottom != null) {
                canvas.saveState();
                bottom.applyLineDash(canvas);
                canvas.moveTo(position.getRight(), position.getBottom());
                canvas.lineTo(position.getLeft(), position.getBottom());
                canvas.stroke();
                canvas.restoreState();
            }
            if (right != null) {
                canvas.saveState();
                right.applyLineDash(canvas);
                canvas.moveTo(position.getRight(), position.getTop());
                canvas.lineTo(position.getRight(), position.getBottom());
                canvas.stroke();
                canvas.restoreState();
            }
            if (left != null) {
                canvas.saveState();
                left.applyLineDash(canvas);
                canvas.moveTo(position.getLeft(), position.getTop());
                canvas.lineTo(position.getLeft(), position.getBottom());
                canvas.stroke();
                canvas.restoreState();
            }
        }
    }   public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CustomBorder3().createPdf(DEST);
    }   public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();   PdfPTable table;
        PdfPCell cell;
        LineDash solid = new SolidLine();
        LineDash dotted = new DottedLine();
        LineDash dashed = new DashedLine();   table = new PdfPTable(4);
        table.setSpacingAfter(30);
  

以上是关于Itext Demo的主要内容,如果未能解决你的问题,请参考以下文章

iText7高级教程之html2pdf——2.使用CSS定义样式

iText7高级教程之html2pdf——2.使用CSS定义样式

iText7高级教程之html2pdf——2.使用CSS定义样式

如何使用 iText 7 使下划线文本可访问

[vscode]--HTML代码片段(基础版,reactvuejquery)

12mmaction2 行为识别商用级别X3D复现 demo实现 检测自己的视频 Expanding Architecturesfor Efficient Video Recognition(代码片段