我将不得不去检索我的一些代码,但我似乎记得必须通过测量当前 X / Y 位置来做事情,根据我使用的任何边距计算它,然后确定是否有更多信息适合或如果我需要一个新页面。我的项目是对长文本块进行自动换行,这很相似,但不太相似。我很快就会在这里更新一些代码。
def newline(self, options, text = ''):
if getattr(self, 'lpp', None) == self.lines[self.pages]:
self.newpage()
if getattr(self, 'y', None) > self.h - self.bm * inch:
self.newpage()
在这种情况下,我可能已经设置了 lpp(每页行数)的属性,所以我首先检查该值是否存在,如果存在,我是否处于当前页面的行数。如果对每页的总行数没有限制,那么我测试了我的 Y 位置和下边距。如有必要,请在页面中添加补丁。这里有一些遗漏,但这是一个普遍的想法。
def newline(self, options, text = ''):
if getattr(self, 'lpp', None) == self.lines[self.pages]:
self.newpage()
if getattr(self, 'y', None) > self.h - self.bm * inch:
self.newpage()
self.addLine()
self.putText(self.x, self.h - self.y, text)
def putText(self, x, y, text):
# If we actually place some text then we want to record that.
if len(text.strip()) > 0 and not self.hasText[self.pages]:
self.hasText[self.pages] = True
# Something here to handle word wrap.
if self.wrap:
lines = self._breakScan(text)
if len(lines) > 1:
self.c.drawString(x, y, lines[0])
self.newline('', ' '.join(lines[1:]))
elif lines:
self.c.drawString(x, y, lines[0])
else:
self.c.drawString(x, y, text)
这里,self.c
是我的画布。我正在跟踪我在页面上放置了多少行,因为有时我们会重新包装可能包含分页符的文档,所有这些都在我们的自定义标记中。