user9349457
user9349457

Reputation:

How to put text in rectangle in pdfbox in java?

Content stream through I wants to show "connect us" text in the rectangle.

contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_ROMAN, 10);
contentStream.newLineAtOffset(260, 240);            
contentStream.showText("Connect with Us:");
contentStream.endText();
contentStream.setNonStrokingColor(235,235,235);
contentStream.addRect(50, 200, 500, 100);
contentStream.fill();

I try to get the text in a rectangle in using this code but did not see text in the rectangle.

Upvotes: 0

Views: 2606

Answers (1)

mkl
mkl

Reputation: 95898

You first draw the text

contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_ROMAN, 10);
contentStream.newLineAtOffset(260, 240);            
contentStream.showText("Connect with Us:");
contentStream.endText();

and then fill the rectangle...

contentStream.setNonStrokingColor(235,235,235);
contentStream.addRect(50, 200, 500, 100);
contentStream.fill();

Well, what happens? Your rectangle covers the text!

To fix this, simple do it the other way around, draw the rectangle first, reset the non-stroking color, then write on it.

contentStream.setNonStrokingColor(235,235,235);
contentStream.addRect(50, 200, 500, 100);
contentStream.fill();
contentStream.setNonStrokingColor(0,0,0);
contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_ROMAN, 10);
contentStream.newLineAtOffset(260, 240);            
contentStream.showText("Connect with Us:");
contentStream.endText();

Upvotes: 5

Related Questions