我正在尝试使用jsPDF创建PDF文档,有没有函数可以返回在将内容写入PDF文件时的当前光标位置?
var doc = new jsPDF()
var text = 'This is a text without real content but with 59 characters.'
var lineHeight = doc.getLineHeight(text) / doc.internal.scaleFactor
var splittedText = doc.splitTextToSize(text, 50)
var lines = splittedText.length // splitted text is a string array
var blockHeight = lines * lineHeight
var yPos = 10
var xPos = 10
doc.text(xPos, yPos, splittedText)
yPos += blockHeight
doc.text(xPos, yPos, '----- This text follows the previous text block.')
yPos += lineHeight
doc.text(xPos, yPos, '----- LineHeight=' + lineHeight + ' / blockHeight=' + blockHeight)
yPos += lineHeight
doc.text(xPos, yPos, '----- doc.internal.scaleFactor = ' + doc.internal.scaleFactor)
如果您更改splitTextTosize()函数中定义为第二个参数的宽度(例如从50更改为80),则下一行的y位置会相应地进行调整。
我曾面临同样的问题,搜索了一些方法或类似方法来获取当前位置,但没有找到合适的。
我的解决方案非常简单。我创建了本地变量x和y,设置它们一次,然后计算新位置并将值保存在相应的变量中。这样你就可以始终知道当前光标位置。