我正在使用iText 7.0.0(Java风格),似乎忽略了表格单元格HorizontalAlignment,因为CENTER和RIGHT都不起作用。你可以重现这个吗?
和重现的代码:
private static void brokenTableCellHorizontalAlignmentPdf(OutputStream output) throws IOException {
PdfWriter writer = new PdfWriter(output);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
Table table = new Table(new float[] {15f, 16f, 4f}).setWidthPercent(100);
for (int y = 1; y <= 3; ++y) {
for (int x = 1; x <= 3; ++x) {
table.addCell(
new Cell()
.setVerticalAlignment(VerticalAlignment.MIDDLE)
.setHorizontalAlignment(HorizontalAlignment.CENTER)
.add(new Paragraph(String.format("(%d, %d)%s", y, x, x == 1 ? "\n\ntest" : ""))
.setFont(font)
.setFontSize(8)));
}
}
document.add(table);
document.close();
}
答案 0 :(得分:4)
请查看CellAlignment示例:
/* Empty. Add your own CSS if you like */
@import url(http://fonts.googleapis.com/css?family=Titillium+Web:900|Roboto:400,100);
body { background-color: #fff; padding: 20px; }
/* Main Container
=================== */
.scroll-content {
font-family: 'Roboto', sans-serif; font-size: 16px; font-weight: 400;
/* width: 650px; height: 650px; */
position: relative; /* Others -absolute positinoed- will get position in relation to this position */
overflow: hidden; /* anything outside of myQuiz container will be clipped or masked */
color: #fff;
background-color: #1abc9c;
}
.scroll-content h2 {font-size: 3em; margin: 0px; font-weight: 100; }
.scroll-content h3 {font-size: 2.4em; margin: 0px; font-weight: 100; }
.scroll-content p { margin: 0px 0px 14px 0px; }
.scroll-content .btn {
display: inline-block; cursor: pointer; background-color: red;
color: #fff; text-decoration: none;
padding: 5px 15px; border-radius: 6px;
}
.scroll-content h1 {
font-weight: 100; font-size: 2em; text-transform: uppercase; margin: 0px;
position: absolute; top: 25px; left: 36px;
}
/* Progress Bar */
.scroll-content .progress {
width: 550px; position: absolute; top: 160px; left: 40px;
}
.scroll-content .progress div {
position: relative; display: inline-block; width: 30px; height: 30px; margin-right: 30px;
border-radius: 50%; background-color: rgba(225,225,225,.2); transition: background-color 1s;
}
.scroll-content .progress div.on,
.scroll-content .progress div.answered {
background-color: #efbe5e;
}
/* Intro */
.scroll-content .intro { position: absolute; top: 225px; left: 2660px; width: 550px; }
.scroll-content .intro p { margin: 0px 0px 40px 0px; }
/* Questions */
.scroll-content .question {
width:550px; position: absolute; top: 225px; left: 2660px;
}
.scroll-content .question .txt {
font-size: 1.6em; margin: 0px 0px 20px 0px;
}
.scroll-content .question .ans {
display: inline-block; font-size: 1.1em; width: 225px; border: 2px solid rgba(238,189,102,.4);
border-radius: 6px; padding: 10px; margin: 0px 15px 15px 0px; position: relative;
}
.scroll-content .question .ans.selected {
border-color: #be4b16;
}
.scroll-content .question .ans.correct {
border-color: #459a2e;
}
/* Insert corecct or incorrect images */
.scroll-content .question .ans::after {
content:''; display: block; width: 40px; height: 40px;
background: no-repeat: 0px 0px; background-size: 40px 40px;
position: absolute; top: 5px; right: 5px;
}
.scroll-content .question .ans.selected::after {
background-image: url(../img/close-circled.png)
}
.scroll-content .question .ans.correct::after {
background-image: url(../img/checkmark-circled.png)
}
.scroll-content .question .ans.selected::after {
background-image: url(../img/close-circled.png)
}
.scroll-content .ans.correct::after {
background-image: url(../img/checkmark-circled.png)
}
.scroll-content .question.unanswered .ans {
cursor: pointer;
}
.scroll-content .question.unanswered .ans:hover {
background-color: mediumvioletred;
}
.scroll-content .question.answered .ans {
cursor: default;
}
/* Feedback */
.scroll-content .feedback {
color: #efbe5e; margin-top: 10px; transition: opacity 1.5s, margin-top 1.5s;
visibility: hidden; opacity: 0;
}
.scroll-content .feedback .btn {
margin-top; 5px;
}
.scroll-content .feedback strong {
color: #fff;
}
.scroll-content .answered .feedback {
visibility: visible; opacity: 1; margin-top: 10px;
}
/* Results */
.scroll-content .results {
position: absolute; top: 225px; left: 2660px; right: 40px;
}
.scroll-content .active, .scroll-content .inactive {
transition: left 1.5s ease-in-out;
}
.scroll-content .active {
left: 40px;
}
.scroll-content .intro.inactive, .scroll-content .inactive.answered { left: -1350px;}
.start-quiz {
margin: auto;
border: 3px solid green;
margin-top: 10px;
display: block;
}
.start-lesson {
margin: auto;
border: 3px solid green;
margin-top: 10px;
display: block;
}
.pane {
background-color: #3498db;
}
运行此示例时生成的PDF如下所示:
对齐没有问题,垂直,水平或文本对齐都没有问题。
答案 1 :(得分:1)
哦,没关系!在查看另一个答案(指向http://gitlab.itextsupport.com/itext7/samples/blob/develop/publications/highlevel/src/main/java/com/itextpdf/highlevel/chapter05/C05E03_CellAlignment.java)后,我们现在应该使用setTextAlignment
。如:
new Paragraph(String.format("(%d, %d)%s", y, x, x == 1 ? "\n\ntest" : ""))
.setFont(font)
.setFontSize(8)
.setTextAlignment(TextAlignment.CENTER)