PdfContentByte canvas = writer.getDirectContent();
Rectangle rect = new Rectangle(0, 805, 594, 820);
rect.setBorder(Rectangle.BOX);
rect.setBorderWidth(1);
rect.setBackgroundColor(BaseColor.GRAY);
rect.setBorderColor(BaseColor.GREEN);
ColumnText ct = new ColumnText(canvas);
ct.setSimpleColumn(rect);
Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
ct.addElement(new Paragraph("Your Text Goes here!! ",catFont));
ct.go();
canvas.rectangle(rect);
document.newPage();
document.close();
This is my code, here I'm trying to add text in rectangle. It didn't work! the rectangle is created but the text is not dispalyed anywhere on the pdf page.
There are a couple of issues with your code causing the text not to show.
Firstly, you add the rectangle to the canvas AFTER you add the text. The gray background will go over any text that was drawn, hiding it.
Secondly, your font size is too big for column boundary, so no text is shown.
You can make your rectangle larger and the text will show or decrease the size of your font.
For example, the following should work as I have increased the rectangle height and moved the canvas.rectangle() call to before the ColumnText.go():
Rectangle rect = new Rectangle(0, 780, 494, 820);
rect.setBorder(Rectangle.BOX);
rect.setBorderWidth(1);
rect.setBackgroundColor(BaseColor.GRAY);
rect.setBorderColor(BaseColor.GREEN);
canvas.rectangle(rect);
ColumnText ct = new ColumnText(canvas);
ct.setSimpleColumn(rect);
Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
ct.addElement(new Paragraph("Your Text Goes here!! ", catFont));
ct.go();
Related
Now I used pdfCanvas to graph a rectangle, code like below:
PdfPage page = pdf.getFirstPage();
PdfCanvas canvas = new PdfCanvas(page, true);
float x = 35;
float y = 480;
canvas.rectangle(x, y, 30, 30).stroke();
In fact, I want to add the rectangle into a table
Table table = new Table(2);
table.setWidth(261.5f);
iText 5, the canvas can be set to an image object and add to table. But in iText 7 the function doesn't work. How can i draw a picture to a table in iText7?
You can create a PdfCanvas from a standalone XObject, transform that XObject into an Image and then add the Image to the Table:
Rectangle boundingBox = new Rectangle(20,470,50,50);
PdfFormXObject xObject = new PdfFormXObject(boundingBox);
xObject.makeIndirect(pdfDoc);//Make sure the XObject gets added to the document
PdfCanvas canvas = new PdfCanvas(xObject, pdfDoc);//Create a canvas from the XObject
canvas.setStrokeColor(Color.BLUE).setLineWidth(3f).rectangle(35, 480, 30, 30).stroke();
Image rect = new Image(xObject);
table.addCell(rect);
There is a comprehensive tutorial on the developers corner of the iText website, explaining (through various examples) how to add an image to a table.
Check out http://developers.itextpdf.com/examples/tables/clone-adding-images-table
I am trying to use Java with PDFBox to draw some text to a PDF file, and set a background color for the text. I know how to draw text and draw filled rectangles, but when I try to draw text in the same position as a rectangle, the text is never shown. Example:
//draw rectangle
content.setNonStrokingColor(200, 200, 200); //gray background
content.fillRect(cursorX, cursorY, 100, 50);
//draw text
content.setNonStrokingColor(0, 0, 0); //black text
content.beginText();
content.setFont(family, fontPt);
content.moveTextPositionByAmount(cursorX, cursorY);
content.drawString("Test Data");
content.endText();
The text never shows up. It is always covered by the rectangle. Any ideas for how to make the text draw on top of the rectangle?
EDIT: As Mkl mentioned in answer, the code I provided actually works. My problem ended up being that the code was in a loop, drawing the background for each line, but the background was drawing over the previous line, and not the current line, overwriting previous text. I just needed to alter the order of events in my looping. Should this question be deleted? It seems unlikely that anyone else would find it useful.
The code you show works.
I made it runnable like this:
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream content = new PDPageContentStream(document, page);
PDFont font = PDType1Font.HELVETICA_BOLD;
int cursorX = 70;
int cursorY = 500;
//draw rectangle
content.setNonStrokingColor(200, 200, 200); //gray background
content.fillRect(cursorX, cursorY, 100, 50);
//draw text
content.setNonStrokingColor(0, 0, 0); //black text
content.beginText();
content.setFont(font, 12);
content.moveTextPositionByAmount(cursorX, cursorY);
content.drawString("Test Data");
content.endText();
content.close();
document.save(new File("textOnBackground.pdf"));
document.close();
(DrawOnBackground.java)
And the result looks like this:
Thus, the cause for your issue lies beyond the code you provided.
PS: I use PDFBox 1.8.10.
I am generating charts using jfreechart.Now as per my requirement i need to export that chart into pdf using itext in java.Here i am able to export jfreechart to pdf but it is coming at the bottom of the pdf file whereas i want it to be top.
Here is my code..
PdfWriter writer = null;
Document document = new Document();
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(
fileName));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics2d = template.createGraphics(width, height,
new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
height);
chart.draw(graphics2d, rectangle2d);
graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);
How to set PdfWriter to write at the top of the pdf file.Please help me.
You are adding the template at the bottom of the page:
contentByte.addTemplate(template, 0, 0);
There are different options you can choose from.
You can add the template at another coordinate:
contentByte.addTemplate(template, 36, 400);
This will add the document at position x = 36 and y = 400. In this case, you need to do your math: instead of 400, you should take the top coordinate of the page (e.g. y = 842) minus a margin (e.g. 36) minus the height of the image.
If can be easier to choose a different option:
Image img = Image.getInstance(template);
document.add(img);
Now you're template is added in the flow of the document, just like all other high-level objects (just like Paragraphs and PdfPTables).
I am trying to draw characters from Wingding.ttf font with Java Graphics.DrawString. But resulting image contains only rectangle instead of char.
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
Graphics graphics = image.getGraphics();
Font font = new Font("Wingdings", Font.PLAIN, 20);
graphics.setFont(font);
graphics.setColor(Color.BLACK);
graphics.drawString("\u00d8", 10, 10); // THREE-D TOP-LIGHTED RIGHTWARDS ARROWHEAD char
ImageIO.write(image, "PNG", new File(TEST_DATA_DIR + "bullet_char.png"));
How can I do this?
I dont think wingdings is one of the "standard" fonts
Font font = null;
try {
font=Font.createFont( Font.TRUETYPE_FONT,
new FileInputStream(new File("/pathto/WINGDINGS.TTF")) );
} catch(Exception e) {
System.out.println(e);
}
font=font.deriveFont(Font.PLAIN, 200);
graphics.setFont(font);
once you load the font (its always PLANE 1 pnt) you can the derive the style and size you want....
As similar questions has been answered here: Drawing exotic fonts in a java applet, you need to pass \uF0d8 instead of \u00d8.
graphics.drawString("\uF0d8", 10, 10);
I would like to draw lines and polygons with transparent lines in PDFBox. Here is some sample code of how I am drawing a blue line, but I cannot figure out to change the alpha value of the color.
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.setStrokingColor(66, 177, 230);
contentStream.drawLine(100, 100, 200, 200);
As of PDFBox 2.0 appendRawCommands is deprecated.
float alpha = 0.5f;
PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
graphicsState.setStrokingAlphaConstant(alpha);
stream.setGraphicsStateParameters(graphicsState);
// draw line here
You can achieve this by using a custom extended graphics state:
PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
graphicsState.setStrokingAlphaConstant(0.5f);
COSName graphicsStateName = page.getResources().add(graphicsState);
try (PDPageContentStream cs = new PDPageContentStream(document, page, true, true, true)) {
cs.appendRawCommands("/" + graphicsStateName.getName() + " gs\n");
// draw your line here.
}
You cannot use the alpha value of the java.awt.Color as PDFBox only uses the RGB value. As per javadoc of public void setStrokingColor(Color color) it just:
Set the stroking color, specified as
RGB.
One option could be that you set the background color as the stroking color to make your line invisible.
NOTE - Invisible != Transparent (so you won't get the see through effect)