问题描述
我要在
我的代码如下:
File image = new File(fullFilePathNameHeader);
InputStream is = new FileInputStream(image);
ByteArrayOutputStream bis = new ByteArrayOutputStream();
int i;
byte[] data = new byte[1024];
while ((i = is.read(data,data.length)) != -1) {
bis.write(data,i);
}
bis.flush();
is.close();
ImageData imageData = ImageDataFactory.create(bis.toByteArray());
Image pdfImg = new Image(imageData);
PageSize ps = pdfDocument.getDefaultPageSize();
Document doc=new Document(pdfDocument);
pdfImg.setFixedPosition(doc.getLeftMargin(),doc.getBottomMargin(),ps.getWidth() - doc.getLeftMargin() - doc.getRightMargin());
//Tried above but this displays pdf at the bottom of the last page
doc.add(pdfImg);
doc.close();
感谢您的帮助。 谢谢。
更新: 请注意,我使用的是下面的库,而不是lowgie库(为了避免混淆,我在这里提到它)
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.2</version>
<type>pom</type>
</dependency>
我尝试在pdf文件中添加文本“ EndOfTheDocument”作为占位符,然后尝试对行进行计数,直到函数找到该单词为止,如下所示:
int numPages = pdfDocument.getNumberOfPages();
boolean found = false;
int totalLinesInDoc =0;
for (int k = 1; k <= numPages; k--) {
// String textOut = PdfTextExtractor.getTextFromPage(pdfDocument.getPage(k));
String textOut = PdfTextExtractor.getTextFromPage(pdfDocument.getPage(k),new LocationTextExtractionStrategy());
String[] lines = textOut.split("\\r?\n");
for (int j = 0; j < lines.length; j++) {
if ("end".equalsIgnoreCase(lines[j])) {
found = true;
System.out.println("matched line " + j);
break;
}
totalLinesInDoc++;
}
if(found){
break;
}
}
pdfImg.setPaddingTop(totalLinesInDoc);
pdfImg.setHeight(totalLinesInDoc);
pdfImg.setPageNumber(numPages);
pdf生成是动态的,我正在测试一个有两页的pdf。对于第一页,它抛出以下错误:
java.lang.RuntimeException: Determinant is zero. Cannot invert transformation
at com.itextpdf.kernel.pdf.canvas.parser.ParserGraphicsState.transformPoints(ParserGraphicsState.java:206)
第二页时,它为我提供了正确的行数。
关于此的任何建议,如果上面的代码中需要更正任何内容,为什么会给出错误和指针。使用这个库很难,因为这是我第一次:(
谢谢
更新3: 我在下面使用Itext librares。请让我知道它们是否相互兼容。谢谢。
<dependency>
<groupId>itext</groupId>
<artifactId>itext</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.12</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- iText pdfHTML add-on -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>2.0.2</version>
</dependency>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)