You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by chitgoks <ch...@gmail.com> on 2017/03/09 07:56:58 UTC
watermark position
Attached is my current code for watermarking text
I can't figure out which class is needed to ensure that the next call of
showText() will place it at the bottom instead of after the first line of
text.
what i wish to accomplish is to have 1 , 2 or 3 lines of showText() shown
vertically centered, and also a diagonal one like 45 or -45 degrees.
for the diagonal, i tried to use Matrix.getRotateInstance() but its
rotation does not base the point at the center.
Any ideas on what I did wrong?
Please advise. thank you
public class WatermarkTool {
public static void createWaterMarkPDF(PDDocument pdfDocument) throws
Exception {
PDFont font = PDType1Font.HELVETICA;
float fontSize = 80;
PDPageTree pages = pdfDocument.getPages();
for (int i=0; i<pages.getCount(); i++) {
PDPage page = pages.get(i);
PDPageContentStream cs = new PDPageContentStream(pdfDocument,
page, AppendMode.APPEND, true, true);
String ts = "Test Line 1";
Point2D.Float center = new Point2D.Float(0, 0);
Point2D.Float offset = center;
Point2D.Float pageCenter = getCenter(page);
PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
gs.setNonStrokingAlphaConstant(0.5f);
cs.setGraphicsStateParameters(gs);
cs.setNonStrokingColor(255,0,0);
cs.setFont(font, fontSize);
cs.beginText();
//
http://stackoverflow.com/questions/6507124/how-to-center-a-text-using-pdfbox
float stringWidth = getStringWidth(ts, font, fontSize);
float textX = pageCenter.x - stringWidth / 2F + offset.x;
float textY = pageCenter.y + offset.y;
// double theta = Math.toRadians(-90);
// cs.setTextMatrix(Matrix.getRotateInstance(theta,
pageCenter.x, pageCenter.y));
// cs.setTextMatrix(Matrix.getRotateInstance(theta, textX,
textY));
Matrix matrix = Matrix.getTranslateInstance(textX, textY);
cs.setTextMatrix(matrix);
cs.showText(ts);
cs.showText(ts);
cs.showText(ts);
cs.endText();
cs.close();
}
}
private static Point2D.Float getCenter(PDPage page) {
PDRectangle pageSize = page.getMediaBox();
return new Point2D.Float(pageSize.getWidth() / 2F,
pageSize.getHeight() / 2F);
}
private static float getStringWidth(String text, PDFont font, float
fontSize) throws IOException {
return font.getStringWidth(text) * fontSize / 1000F;
}
}
Re: watermark position
Posted by chitgoks <ch...@gmail.com>.
hi tilman
okay. thought there would be some way to auto set positioning
calculating for the height is the tricky part but i managed to get that. i
only use helveticA.
the center diagonal may be the tricky part. ive managed to make the upper
left and right work with 1-3 lines
On Fri, 10 Mar 2017 at 1:39 AM Tilman Hausherr <TH...@t-online.de>
wrote:
> Am 09.03.2017 um 08:56 schrieb chitgoks:
> > Attached is my current code for watermarking text
> >
> > I can't figure out which class is needed to ensure that the next call of
> > showText() will place it at the bottom instead of after the first line of
> > text.
> >
> > what i wish to accomplish is to have 1 , 2 or 3 lines of showText() shown
> > vertically centered, and also a diagonal one like 45 or -45 degrees.
> >
> > for the diagonal, i tried to use Matrix.getRotateInstance() but its
> > rotation does not base the point at the center.
> >
> > Any ideas on what I did wrong?
> >
> > Please advise. thank you
>
>
> I have not understood the first part of the question, only the second
> part. Rotation is around the (0,0) axis. To show a diagonal watermark,
> use this code:
>
> cs.setTextMatrix(Matrix.getRotateInstance(Math.toRadians(45), 0, 0));
> cs.showText(ts);
>
> However it won't be centered, and stuff on the left won't be visible
> fully. To acomplish this, you'll have to use some math to calculate the
> length of your diagonal line. Then you'll need the length of your
> string, etc. You may also need the height, see the
> DrawPrintTextLocations example, this can be tricky and is different for
> each font type.
>
> Note that the two right parameters of Matrix.getRotateInstance()
> (translations) are not the same as AffineTransform.getRotateInstance()
> (anchors).
>
> Tilman
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
> --
Website/Java Games: http://www.chitgoks.com
My Blogs:
http://tech.chitgoks.com
http://wuhtevah.chitgoks.com
http://disneyusa.blogspot.com
Re: watermark position
Posted by Tilman Hausherr <TH...@t-online.de>.
Am 09.03.2017 um 08:56 schrieb chitgoks:
> Attached is my current code for watermarking text
>
> I can't figure out which class is needed to ensure that the next call of
> showText() will place it at the bottom instead of after the first line of
> text.
>
> what i wish to accomplish is to have 1 , 2 or 3 lines of showText() shown
> vertically centered, and also a diagonal one like 45 or -45 degrees.
>
> for the diagonal, i tried to use Matrix.getRotateInstance() but its
> rotation does not base the point at the center.
>
> Any ideas on what I did wrong?
>
> Please advise. thank you
I have not understood the first part of the question, only the second
part. Rotation is around the (0,0) axis. To show a diagonal watermark,
use this code:
cs.setTextMatrix(Matrix.getRotateInstance(Math.toRadians(45), 0, 0));
cs.showText(ts);
However it won't be centered, and stuff on the left won't be visible
fully. To acomplish this, you'll have to use some math to calculate the
length of your diagonal line. Then you'll need the length of your
string, etc. You may also need the height, see the
DrawPrintTextLocations example, this can be tricky and is different for
each font type.
Note that the two right parameters of Matrix.getRotateInstance()
(translations) are not the same as AffineTransform.getRotateInstance()
(anchors).
Tilman
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org