- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我修改了 TextToPDF 类来检测内容文件中的某些标签。当找到标签中心时,文本居中。
这不能正常工作,X 坐标似乎不受尊重
当我显示 x: 坐标时,我得到:0
0
18.861816
2.9138184
238.31181
9.933823
68.68582
10.347824
40.14981
在生成的 pdf 中,第 6 行应该在第 5 行之前开始,但它在第 5 行之后开始。
238.31181 > 9.933823 但 pdfbox 似乎说 9.933823 > 238.31181
生成文件的结果
该字符串被放入 createPDFFromText 参数的 StringReader 中。
String rawText = "Children's heart surgery has been suspended with immediate effect at a hospital which is embroiled in a long-running row over the future of paediatric cardiac services in England. \n "
+ "#CENTER#The decision to stop congenital heart surgery at Leeds General Infirmary comes just a day after the High Court quashed plans by the NHS to close its children's unit after ruling the consultation process was flawed.\n "
+ "It follows concerns raised about patients' care including allegations the hospital was avoiding referring children for complex and life-saving treatment at another centre in Newcastle.\n "
+ "Leeds Teaching Hospitals NHS Trust said the temporary measure was being taken to allow an internal review to be conducted following consultation with the Care Quality Commission (CQC).";
代码
public PDDocument createPDFFromText( Reader text ) throws IOException
{
PDDocument doc = null;
PDSimpleFont font = PDType1Font.TIMES_ROMAN;
int fontSize = 12;
boolean isCentered = false;
try
{
final int margin = 40;
float height = font.getFontDescriptor().getFontBoundingBox().getHeight()/1000;
//calculate font height and increase by 5 percent.
height = height*fontSize*1.05f;
doc = new PDDocument();
BufferedReader data = new BufferedReader( text );
String nextLine = null;
PDPage page = new PDPage();
PDPageContentStream contentStream = null;
float y = -1;
float maxStringLength = page.getMediaBox().getWidth() - 2*margin;
// There is a special case of creating a PDF document from an empty string.
boolean textIsEmpty = true;
while( (nextLine = data.readLine()) != null )
{
// The input text is nonEmpty. New pages will be created and added
// to the PDF document as they are needed, depending on the length of
// the text.
textIsEmpty = false;
String[] lineWords = nextLine.trim().split( " " );
int lineIndex = 0;
while( lineIndex < lineWords.length )
{
StringBuffer nextLineToDraw = new StringBuffer();
float lengthIfUsingNextWord = 0;
do
{
nextLineToDraw.append( lineWords[lineIndex] );
nextLineToDraw.append( " " );
lineIndex++;
if( lineIndex < lineWords.length )
{
String lineWithNextWord = nextLineToDraw.toString() + lineWords[lineIndex];
lengthIfUsingNextWord =
(font.getStringWidth( lineWithNextWord )/1000) * fontSize;
}
}
while( lineIndex < lineWords.length &&
lengthIfUsingNextWord < maxStringLength );
if( y < margin )
{
// We have crossed the end-of-page boundary and need to extend the
// document by another page.
page = new PDPage();
doc.addPage( page );
if( contentStream != null )
{
contentStream.endText();
contentStream.close();
}
contentStream = new PDPageContentStream(doc, page);
contentStream.setFont( font, fontSize );
contentStream.beginText();
y = page.getMediaBox().getHeight() - margin + height;
contentStream.moveTextPositionByAmount(
margin, y );
}
//System.out.println( "Drawing string at " + x + "," + y );
if( contentStream == null )
{
throw new IOException( "Error:Expected non-null content stream." );
}
String txt = nextLineToDraw.toString();
if ( txt.indexOf( "#CENTER#" ) != -1 || isCentered )
{
txt = nextLineToDraw.toString().replaceAll( "#CENTER#", "" );
PDRectangle pageSize = page.findMediaBox();
float stringWidth = font.getStringWidth( txt );
float xPosition = ( pageSize.getWidth() - ( 2 * margin ) - ( stringWidth * fontSize ) / 1000f ) / 2f;
System.out.println( xPosition );
contentStream.moveTextPositionByAmount( xPosition, -height );
isCentered = true;
}
else
{
System.out.println( 0);
contentStream.moveTextPositionByAmount( 0, -height );
}
y -= height;
contentStream.drawString( nextLineToDraw.toString() );
}
}
if (textIsEmpty)
{
doc.addPage(page);
}
if( contentStream != null )
{
contentStream.endText();
contentStream.close();
}
}
catch( IOException io )
{
if( doc != null )
{
doc.close();
}
throw io;
}
return doc;
}
文件结果可在此处获取:http://filebin.ca/br6slMSfOR6/testUnitairePdf.pdf正如您所看到的,文本未正确居中。
最佳答案
问题是(就像 @GaborSch 在他对答案的评论中猜测的那样)
contentStream.moveTextPositionByAmount()
相对于最后一行,而不是相对于某个固定边距。
详细信息:
PDPageContentStream
方法 moveTextPositionByAmount
记录为:
/**
* The Td operator.
* @param x The x coordinate.
* @param y The y coordinate.
* @throws IOException If there is an error writing to the stream.
*/
public void moveTextPositionByAmount( float x, float y ) throws IOException
Td 运算符又记录为:
tx ty Td Move to the start of the next line, offset from the start of the current line by (tx, ty). tx and ty shall denote numbers expressed in unscaled text space units. More precisely, this operator shall perform these assignments:
(参见 PDF 规范 ISO 32000-1:2008,第 9.4.2 节“文本定位运算符”)
因此,您必须记住使用的 xPosition
值,并且
紧接该行之后
contentStream.drawString( nextLineToDraw.toString() );
插入
contentStream.moveTextPositionByAmount( -xPosition, 0 );
如果线居中;
或者等到确定下一行文本的 moveTextPositionByAmount
参数,然后从新的 x 值中减去之前的 xPosition
值。
关于java - 文本居中,不考虑 x 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15701802/
我有一个点(粉色圆圈),它有一个已知的 X 坐标和一个已知的 Y 坐标,但 Y 坐标> 坐标不正确。它当前位于目标贝塞尔曲线(部分位于白色正方形中的曲线)所在的点(如果它是两点之间的一条线)。我需要为
有一个基于QML 和QWT 的代码,一种具有更多可能性的图形生成器。技术要求之一是根据某个 X 坐标获得绘图曲线的 Y 坐标。 有一种不准确的方法 - 获取 QwtPlotCurve 的 QPoint
我目前正在将对象的 3D 坐标转换为 2D 坐标,然后在其上绘制 2D 文本(目前是对象名称): public static int[] getScreenCoords(double x, doubl
首先,我创建一个元组列表(要绘制的点)。每个元组由 3 个数字组成(x - 坐标,y - 坐标,c - 点的颜色) import random import matplotlib.pyplot as
我正在制作一个 2 人 Java 游戏,但我需要确保坐标保留在板上。 addPiece(1, 1, "X"); addPiece(8, 8, "O"); showBoard(); Scanner my
我想检查我是否正确使用了 scipy 的 KD 树,因为它看起来比简单的暴力破解要慢。 关于这个我有三个问题: Q1. 如果我创建以下测试数据: nplen = 1000000 # WGS84 lat
我有一个 GeoJSON 文件,我正在尝试处理它以便在谷歌地图上绘制一些功能。然而,问题在于坐标不是传统的纬度/经度表示法,而是一些大的六位/七位数字。示例: { "type":
我在使用坐标时遇到格式化问题。 public class Coordinate { public int x; public int y; public Coordinate( int x
我正在尝试获取当前位置的经度和纬度坐标。这是到目前为止我的代码: public class MainActivity extends AppCompatActivity { @Override pro
基本上,我需要获取从 OpenGL 中的贝塞尔曲线实现绘制的所有坐标。具体来说,我需要坐标来沿着弯曲的轨迹路径移动场景中的球体对象(棒球)。这是我用来绘制曲线的: GL2 gl = drawable.
现在我用 JAVA 遇到了一些问题,但不记得如何获取坐标系之间的长度。 例如。A 点 (3,7)B点(7,59) 我想知道如何计算a点和b点之间的距离。非常感谢您的回答。 :-) 最佳答案 A = (
我正在用 Pi2Go 机器人制作一个小项目,它将从超声波传感器获取数据,然后如果它看到一个物体,则放置一个 X,并放置 O 它当前所在的位置,我有两个问题:如何在 tkinter 上设置坐标位置?例如
如何在 pygame 中存储对象的先前坐标?我的问题可能有点难以解释,但我会尽力,如果您自己尝试我的代码以理解我的意思可能会有所帮助。 这就是我的游戏的内容。我希望这能让我的问题更容易理解。 我正在创
如何存储用户的当前位置并在 map 上显示该位置? 我能够在 map 上显示预定义的坐标,只是不知道如何从设备接收信息。 此外,我知道我必须将一些项目添加到 Plist 中。我怎样才能做到这一点? 最
我在 android 应用程序开发方面不是很熟练,我正在开发一个测试应用程序。我检测到了脸和眼睛,现在我要根据眼睛的坐标在脸上画一些像粉刺或疤痕的东西(例如脸颊上的眼睛下方)。稍后,我会把眼镜或帽子放
所以我正在使用 API 来检测图像中的人脸,到目前为止它对我来说效果很好。然而,我一直无法弄清楚如何将图像裁剪到脸上。我知道如何裁剪位图,但它需要获取位图中脸部的左上角位置以及宽度和高度。当我使用 查
我有 2 个表。第一个表包含以下列:Start_latitude、start_longitude、end_latitude、end_longitude、sum。 sum 列为空,需要根据第二张表进行填
有没有办法给 Google Maps API 或类似的 API 一个城镇名称,并让它返回城镇内的随机地址?我希望能够将数据作为 JSON 获取,以便我可以在 XCode 中使用 SwiftyJSON
我将坐标保存在 numpy 数组 x 和 y 中。现在我想要的只是获得一个多边形(分别是点数组),它用给定的宽度参数定义周围区域。 我遇到的问题是我需要一个没有(!)交叉点的多边形。但是,当曲线很窄时
我正在开发井字游戏 (3x3),所以我有 9 个按钮,我想做的是获取用户按下的按钮的坐标,并在按钮的位置插入图像。 例子: @IBOutlet weak var button1Outlet: UIBu
我是一名优秀的程序员,十分优秀!