2016-09-11 35 views
0

我正在嘗試將文本打印到pdf中,並且我擁有一切設置和工作到目前爲止。我有一條寬度爲6英寸的線,即設備像素爲(6 * 72)432。我正在嘗試的是將文本打印到此行中,居中,並填充雙方佔據該行的全部寬度。示例------你好,這是我的文字------。這裏是我的代碼片段,我試圖估計我的文本佔用的空間,所以我可以計算在前後插入多少字符。Itext7中心文本並填充兩邊

PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA); 
canvas.setFontAndSize(font, 12); 
System.out.println(font.getContentWidth(new PdfString("Hello there, this is my Text"))); 

這段代碼產生超過兩千這遠遠超過了432不知道什麼是返回的單位數。 我如何估計我的字符串和中心的長度 - 在432dp 之內?額外的空間必須填寫一個特殊字符。它與支票的印刷方式非常相似,如果有空間,用數字表示並用雙方填充。

我看着this postthis other post但我沒有得到任何與此。請指教。

回答

2

請查看您所參考文章的標籤: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(); 
    } 
} 
+0

爲什麼你會做數學題,你自己是否iText的可以爲你做了嗎? –