- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在所有页面的页面顶部添加一个矩形,但我不希望在最后一页上添加矩形。这是我的代码:
@Override
public void onStartPage(PdfWriter writer, Document output) {
Font bold = new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD);
bold.setStyle(Font.UNDERLINE);
bold.setColor(new BaseColor(171, 75, 15));
PdfContentByte cb = writer.getDirectContent();
// Bottom left coordinates x & y, followed by width, height and radius of corners.
cb.roundRectangle(100f, 1180f, 400f, 100f, 5f);//I dont want this on the ;ast page
cb.stroke();
try {
output.add(new Paragraph("STATEMENT OF ACCOUNT", bold));
output.add(new Paragraph(new Phrase(new Chunk(" "))));
output.add(new Paragraph(new Phrase(new Chunk(" "))));
output.add(new Paragraph(new Phrase(new Chunk(" "))));
output.add(new Paragraph(new Phrase(new Chunk(" "))));
Image logo = Image.getInstance(imagepath);
logo.setAbsolutePosition(780, 1230);
logo.scaleAbsolute(200, 180);
writer.getDirectContent().addImage(logo);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
有没有办法从文档的最后一页跳过或删除这个矩形?
最佳答案
首先,iText 开发人员经常强调,在 onStartPage
中,不得向 PDF 添加内容。原因是在某些情况下,会创建未使用的页面并为它们调用 onStartPage
但随后将其删除。但是,如果您在 onStartPage
中向它们添加内容,它们不会被删除,而是保留在您的文档中。
因此,请始终使用 onEndPage
将任何内容添加到页面。
在您的用例中,使用onEndPage
还有另一个原因:通常只有当最后一位内容添加到文档中时,才会清楚给定页面是最后一页。这通常发生在为页面调用 onStartPage
之后、onEndPage
之前。
因此,在将最后一位常规页面内容添加到文档后,您可以简单地在页面事件监听器中设置一个标志,表明当前页面是最终文档页面。现在,以下 onEndPage
调用知道它会处理最终页面,并且可以以不同的方式添加内容。
所以页面事件监听器看起来像这样
class MyPageEventListener extends PdfPageEventHelper {
public boolean lastPage = false;
@Override
public void onEndPage(PdfWriter writer, Document output) {
if (!lastPage) {
[add extra content for page before the last one]
} else {
[add extra content for last page]
}
}
...
}
并像这样使用
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, TARGET);
MyPageEventListener pageEventListener = new MyPageEventListener();
writer.setPageEvent(pageEventListener);
document.open();
[add all regular content to the document]
pageEventListener.lastPage = true;
document.close();
关于java - 在StartPage itextPdf上添加对象(最后一页除外),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52774562/
最近开始在iOS平台学习PhoneGap,遇到了一个问题。如何更改 startPage?我将 startPage 设置如下: self.viewController.startPage = @"htt
我正在 ASP.Net 应用程序中使用 JQuery Pagination。如果我使用像下面的代码块这样的数字设置起始页,我会得到正确的起始页。 $('#table_3_Pagination').tw
我创建了一个新的 Cordova 项目并将我的 URL 放在 AppDelegate.m 中: // Set your app's start page by setting the tag in
我在 Google 上搜索了一个 JavaScript,可以将当前页面设置为您的起始页。但我能找到的只是 IE 5 及更高版本的一些旧脚本。 它们似乎只在 IE 中工作,而且它们似乎也是内联的。
我正在处理一个特殊格式的文本文件以发送到打印机。我遇到了一种最容易处理的情况,方法是丢弃当前(不完整的)页面并开始一个新页面。因此,我调用了 StartPage() 并输出一行或多行输出,然后我意识到
Visual Studio Express 2013 for Windows Desktop 在 C:\Users\MYUSER\Documents\Visual Studio 2013 中放置了一组
我创建了一个名为“Admin”的区域。 在/Areas/Admin/Views/中,我有 _ViewStart.cshtml: @{ Layout = "~/Areas/Admin/Views
我在我的 phonegap 应用程序上用 javascript 在 html5 localstorage 上写了一些键/值,但我想在我的应用程序启动时访问它们,特别是从我的 objective-c +
我有一个基本的 MVC 项目,我为它添加了一个区域,用于管理类型的功能,称为......Admin。 然后因为我希望我的管理区域在某种程度上独立于站点的其余部分,因为用户不需要的管理程序集、功能等。
我正在为学校做一个项目。当我尝试将站点地图添加到我的站点时出现该错误。 堆栈跟踪: [InvalidCastException: Unable to cast object of type 'ASP.
我有为 iOS 开发的 phonegap 应用程序,我想在特定条件下设置 startPage,否则应用程序应该加载不同的另一个页面作为启动页。 我是 Objective C 的新手,对 AppDele
我是一名优秀的程序员,十分优秀!