使用 PDFBox 将文本写入多边形
我发现了如何使用drawText将文本写入页面,并且使用drawPolygon将多边形显示在正确的位置。
问题是,当我只绘制文本时它可以工作,但是一旦我绘制多边形,文本就不再绘制。
如果我创建两个 PDPageContentStream 对象(一个用于文本,另一个用于多边形),则不再绘制多边形。
这是我的测试课。任何 PDF 都应该用于测试。
package ch.sertal.vision.server.helpers;
import ch.sertal.vision.BaseDaoTest;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.testng.annotations.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
/**
* Created by IntelliJ IDEA.
* User: micha.roon
* Date: 11/13/11
* Time: 11:22 PM
* To change this template use File | Settings | File Templates.
*/
public class WritePDFTest extends BaseDaoTest {
@Test
public void testTextPlacement() throws IOException, COSVisitorException, URISyntaxException {
File pdfFile = new File( this.getClass().getResource( "/META-INF/menge-regierapport.pdf" ).toURI() );
PDDocument doc = PDDocument.load( pdfFile );
PDPage page = null;
for ( Object kid : doc.getDocumentCatalog().getPages().getKids() ) {
if ( kid instanceof PDPage ) {
page = ( PDPage ) kid;
break;
}
}
PDPageContentStream rectContent = new PDPageContentStream( doc, page, true, false );
rectContent.setNonStrokingColor( Color.blue );
PDPageContentStream content = new PDPageContentStream( doc, page, true, false );
writeOnPage( content, String.valueOf( page.getArtBox().getHeight() ), 0, 0 );
writeOnPage( content, String.valueOf( page.getArtBox().getWidth() ), 200, 100 );
rectContent.fillRect( 100, 200, 100, 50 );
content.close();
doc.save( new FileOutputStream(
new File( this.getClass().getResource( "/META-INF/menge-regierapport.pdf" ).toURI() )) );
doc.close();
}
void writeOnPage(PDPageContentStream content, String text, int x, int y) throws IOException {
content.beginText();
content.setFont( PDType1Font.HELVETICA, 10 );
content.moveTextPositionByAmount( x, y );
content.drawString( text );
content.endText();
}
}
感谢您的帮助
I found out how to write text onto the page with drawText and the polygon appears at the right place with drawPolygon.
The issue is that when I just draw text it works but as soon as I draw the polygons, the text is not drawn anymore.
If I create two PDPageContentStream objects (one for the text and another for the polygon) the polygons are not drawn anymore.
Here is my test class. Any PDF should do for testing.
package ch.sertal.vision.server.helpers;
import ch.sertal.vision.BaseDaoTest;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.testng.annotations.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
/**
* Created by IntelliJ IDEA.
* User: micha.roon
* Date: 11/13/11
* Time: 11:22 PM
* To change this template use File | Settings | File Templates.
*/
public class WritePDFTest extends BaseDaoTest {
@Test
public void testTextPlacement() throws IOException, COSVisitorException, URISyntaxException {
File pdfFile = new File( this.getClass().getResource( "/META-INF/menge-regierapport.pdf" ).toURI() );
PDDocument doc = PDDocument.load( pdfFile );
PDPage page = null;
for ( Object kid : doc.getDocumentCatalog().getPages().getKids() ) {
if ( kid instanceof PDPage ) {
page = ( PDPage ) kid;
break;
}
}
PDPageContentStream rectContent = new PDPageContentStream( doc, page, true, false );
rectContent.setNonStrokingColor( Color.blue );
PDPageContentStream content = new PDPageContentStream( doc, page, true, false );
writeOnPage( content, String.valueOf( page.getArtBox().getHeight() ), 0, 0 );
writeOnPage( content, String.valueOf( page.getArtBox().getWidth() ), 200, 100 );
rectContent.fillRect( 100, 200, 100, 50 );
content.close();
doc.save( new FileOutputStream(
new File( this.getClass().getResource( "/META-INF/menge-regierapport.pdf" ).toURI() )) );
doc.close();
}
void writeOnPage(PDPageContentStream content, String text, int x, int y) throws IOException {
content.beginText();
content.setFont( PDType1Font.HELVETICA, 10 );
content.moveTextPositionByAmount( x, y );
content.drawString( text );
content.endText();
}
}
Thank you for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来
rectContent
和content
实际上是指向同一个“流”的指针,重新编写代码以解决 PDFBox 的实际工作方式可能是有意义的。这意味着 (a) 删除rectContent
并将其替换为content
。rectContent.close()
.It appears that
rectContent
andcontent
are actually pointers to the same "stream" and it may make sense to rework the code to address how PDFBox is actually working. This would mean (a) removingrectContent
and replacing it withcontent
.我认为当你绘制矩形和文本时
setNonStrokingColor
是相同的。所以矩形将隐藏文本。解决方案是调用setNonStrokingColor
并为 Rect 和文本指定不同的值。那么你应该能够看到他们两个。I think the
setNonStrokingColor
is the same when you draw the Rect and the text. So the Rect will hide the text. The solution is to callsetNonStrokingColor
with different values for the Rect and the text. Then you should be able to see both of them.