請查看您所參考文章的標籤:How can I find the maximum character limit for a text field?最後一個標籤是指iText 5,而您使用的是iText 7.換句話說:您正在查看錯誤的FAQ條目。該問題的iText 7 FAQ條目是:How can I find the maximum character limit for a text field?但這並不能回答你的問題。
你應該閱讀How to choose the optimal size for a font?
當你測量文本你得到的值表示在字形空間:
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
float glyphWidth = font.getContentWidth(new PdfString("Hello there, this is my Text"));
這是正常的,這些線是不足以讓用戶空間的寬度(其是你在找什麼)。爲什麼這是正常的?因爲font
對象不知道要使用的字體大小。
就你而言,你使用的是12pt的字體。因此,PdfString
的寬度是:
float width = glyphWidth * 0.001f * 12f;
這就是你正在尋找的寬度。
使用showTextAligned()
方法可以將文本置於絕對位置。有許多替代方法來實現這一點。這可能是另一個問題的主題,因爲你不需要做所有的數學。
你可能只是這樣做:
/*
* Example written in answer to a question on StackOverflow.
* http://stackoverflow.com/questions/39437838
*/
package com.itextpdf.sandbox.text;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Tab;
import com.itextpdf.layout.element.TabStop;
import com.itextpdf.layout.property.TabAlignment;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author bruno
*/
public class CenterText {
class MyLine implements ILineDrawer {
private float lineWidth = 1;
private float offset = 5;
private Color color = Color.BLACK;
@Override
public void draw(PdfCanvas canvas, Rectangle drawArea) {
canvas.saveState()
.setStrokeColor(color)
.setLineWidth(lineWidth)
.moveTo(drawArea.getX(), drawArea.getY() + lineWidth/2 + offset)
.lineTo(drawArea.getX() + drawArea.getWidth(), drawArea.getY() + lineWidth/2 + offset)
.stroke()
.restoreState();
}
@Override
public float getLineWidth() {
return lineWidth;
}
@Override
public void setLineWidth(float lineWidth) {
this.lineWidth = lineWidth;
}
@Override
public Color getColor() {
return color;
}
@Override
public void setColor(Color color) {
this.color = color;
}
public float getOffset() {
return offset;
}
public void setOffset(float poffset) {
this.offset = offset;
}
}
public static final String DEST = "results/text/center_text.pdf";
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CenterText().createPdf(DEST);
}
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PageSize pagesize = PageSize.A4;
Document document = new Document(pdf, pagesize);
float w = pagesize.getWidth() - document.getLeftMargin() - document.getRightMargin();
MyLine line = new MyLine();
List<TabStop> tabstops = new ArrayList();
tabstops.add(new TabStop(w/2, TabAlignment.CENTER, line));
tabstops.add(new TabStop(w, TabAlignment.LEFT, line));
Paragraph p = new Paragraph();
p.addTabStops(tabstops);
p.add(new Tab()).add("Text in the middle").add(new Tab());
document.add(p);
document.close();
}
}
爲什麼你會做數學題,你自己是否iText的可以爲你做了嗎? –