在 itext 中,我有一个块/短语/段落(我不介意哪个),我想在页面上的其他位置放置一些位置,例如 300 x 200。我该怎么做?
问问题
38285 次
5 回答
27
最后我写了自己的方法来做到这一点。
private void PlaceChunck(String text, int x, int y) {
PdfContentByte cb = writer.DirectContent;
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SaveState();
cb.BeginText();
cb.MoveText(x, y);
cb.SetFontAndSize(bf, 12);
cb.ShowText(text);
cb.EndText();
cb.RestoreState();
}
于 2009-10-27T16:15:19.287 回答
19
这是一个包含所有正确大小写和 try/catch 块的版本:
private static void absText(String text, int x, int y) {
try {
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.saveState();
cb.beginText();
cb.moveText(x, y);
cb.setFontAndSize(bf, 12);
cb.showText(text);
cb.endText();
cb.restoreState();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
于 2011-08-12T20:25:11.493 回答
2
我按照这些思路做了一些事情,也许它会帮助其他人:
ColumnText ct = new ColumnText(writer.getDirectContent());
ct.setSimpleColumn(left,bottom,right,top);
ct.setText(new Phrase("String"));
ct.go();
于 2015-12-15T21:32:07.347 回答
0
就我而言,只有这个解决方案可以正常工作。
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(FILE));
添加方法
private void addTextData(Document document, String text) {
PdfContentByte cb = pdfWriter.getDirectContent();
cb.beginText();
try {
BaseFont f_cn = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.setFontAndSize(f_cn, 40);
float requiredX = 50;
float requiredY = 50;
Paint paint = new Paint();
paint.setTextSize(40);
Typeface typeface=Typeface.createFromAsset(getAssets(), "Helvetica.ttf");
paint.setTypeface(typeface);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
Rect result = new Rect();
paint.getTextBounds(text, 0, text.length(), result);
Log.i("Text dimensions", "Width: "+result.width()+"-Height: "+result.height());
float calculatedY = document.getPageSize().getHeight() - result.height() - requiredY;
cb.setTextMatrix(requiredX, calculatedY);
cb.showText(text);
cb.endText();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
这肯定会帮助你。
于 2016-12-09T11:11:25.020 回答
0
希望这对你有帮助!这是我的代码...
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, output);
document.open();
FixText("Add Your Text",400,700,writer,14);
document.close();
添加功能:
private static void FixText(String text, int x, int y,PdfWriter writer,int size) {
try {
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.saveState();
cb.beginText();
cb.moveText(x, y);
cb.setFontAndSize(bf, size);
cb.showText(text);
cb.endText();
cb.restoreState();
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
}
于 2018-09-17T06:58:52.373 回答