Reputation: 4007
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
Upvotes: 0
Views: 3368
Reputation: 1180
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 call setNonStrokingColor
with different values for the Rect and the text. Then you should be able to see both of them.
Upvotes: 0
Reputation: 1087
rectContent.close()
.It appears that rectContent
and content
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) removing rectContent
and replacing it with content
.
PDPageContentStream content = new PDPageContentStream( doc, page, true, false );
content.setNonStrokingColor( Color.blue );
// moving this before content, so it's written "below" (I think)
content.fillRect( 100, 200, 100, 50 );
// resetting the color to black
content.setNonStrokingColor( Color.black );
writeOnPage( content, String.valueOf( page.getArtBox().getHeight() ), 0, 0 );
writeOnPage( content, String.valueOf( page.getArtBox().getWidth() ), 200, 100 );
content.close();
doc.save( new FileOutputStream(
new File( this.getClass().getResource( "/META-INF/menge-regierapport.pdf" ).toURI() )) );
doc.close();
Upvotes: 1