简体   繁体   English

获取异常-java.lang.IllegalStateException:错误:文本块内不允许使用drawImage

[英]Getting exception -java.lang.IllegalStateException: Error: drawImage is not allowed within a text block

Getting error while using DrawImage() method to add image to exiting PDF file on each page (PDFBox api) to achieve it but getting below exception 使用DrawImage()方法将图像添加到每页上退出的PDF文件(PDFBox api)以实现它时出现错误,但出现以下异常

java.lang.IllegalStateException: Error: drawImage is not allowed within a text block. java.lang.IllegalStateException:错误:文本块内不允许使用drawImage。

Method: contents.drawImage(ximage,80,80); 方法:contents.drawImage(ximage,80,80);

I am not sure about existing PDF file it may contain data or image on each page. 我不确定现有的PDF文件是否在每页上都包含数据或图像。 I would like to add small image on each page. 我想在每页上添加小图像。 It is fine it overlap exiting data. 可以重叠现有数据。

org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject  ximage = null;
org.apache.pdfbox.rendering.PDFRenderer pdfRenderer = 
  new org.apache.pdfbox.rendering.PDFRenderer(doc1);

for(int c=0; c<=doc1.getNumberOfPages()-1;c++){
  java.awt.image.BufferedImage bim =
  pdfRenderer.renderImageWithDPI(c, 300, 
  org.apache.pdfbox.rendering.ImageType.RGB);
  ximage = org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory.createFromImage(doc1, bim);
 }
for (i = 0; i <= pageCount - 1; i++) {

// get all pages
PDPage = pd1.getDocumentCatalog().getPages().get(i);

org.apache.pdfbox.pdmodel.PDPageContentStream contents = new org.apache.pdfbox.pdmodel.PDPageContentStream(pd1, PDPage, org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode.APPEND, true);

//Begin the Content stream 
contents.beginText();

contents.setFont(font, fontSize);


contents.setLeading(14.5 f);

contents.newLine();

contents.drawImage(ximage, 80, 80);
// contents.drawInlineImage(ximage,400,400);
contents.endText();

contents.close();
}

Want image to be present on each page of PDF. 希望图像出现在PDF的每一页上。 Please suggest the best way to to place an image on the PDF page. 请提出将图像放置在PDF页面上的最佳方法。

The exception text "drawImage is not allowed within a text block" tells it: "beginText()" and "endText()" make a text block. 异常文本“不允许在文本块内使用DrawImage”告诉它:“ beginText()”和“ endText()”组成一个文本块。 So move "contents.endText();" 因此移动“ contents.endText();” before "drawImage". 在“ drawImage”之前。 (Btw you are not showing any text?!) (顺便说一句,您没有显示任何文字吗?!)

Bonus hint: add a fifth "true" parameter to the "PDPageContentStream" constructor. 额外的提示:向“ PDPageContentStream”构造函数添加第五个“ true”参数。 This will prevent weird effects with some PDFs (see the javadoc). 这将防止某些PDF产生怪异的效果(请参阅javadoc)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException错误 - java.lang.IllegalStateException error 获取 java.lang.IllegalStateException: Logback 配置错误检测到错误 - Getting java.lang.IllegalStateException: Logback configuration error detected error 收到此错误:消息:java.lang.IllegalStateException:无法从数字单元格获取文本值 - Getting this error: Message: java.lang.IllegalStateException: Cannot get a text value from a numeric cell 在线程“主要” java.lang.IllegalStateException中获取错误接收 - Getting error xception in thread “main” java.lang.IllegalStateException java.lang.IllegalStateException:isHexDigit异常 - java.lang.IllegalStateException: isHexDigit exception 致命异常(java.lang.IllegalStateException) - Fatal exception (java.lang.IllegalStateException) 致命异常:java.lang.IllegalStateException没有活动 - Fatal Exception: java.lang.IllegalStateException No activity 异常:java.lang.IllegalStateException:已连接 - Exception : java.lang.IllegalStateException: Already connected 线程“ AWT-EventQueue-0”中的异常java.lang.IllegalStateException:不允许在事件调度线程中进行阻止 - Exception in thread “AWT-EventQueue-0” java.lang.IllegalStateException: Blocking in Event Dispatch Thread not allowed 获取java.lang.IllegalStateException:用于子视图 - Getting java.lang.IllegalStateException: for child view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM