When I add 1 chart to the document there is no problem
When I add the second chart I get this error ...
Exception in thread "main" com.itextpdf.text.exceptions.IllegalPdfSyntaxException: Unbalanced save/restore state operators.
chart1 = SpecialJFreeChartBuilder.createChart1(
"Site and Number of Requests ", results);
document.newPage();
PdfContentByte Add_Chart_Content = writer.getDirectContent();
PdfTemplate template_Chart_Holder = Add_Chart_Content
.createTemplate(width, height);
Graphics2D Graphics_Chart = template_Chart_Holder.createGraphics(
width, height, new DefaultFontMapper());
Rectangle2D Chart_Region = new Rectangle2D.Double(0, 0, 540, 380);
chart1.draw(Graphics_Chart, Chart_Region);
Graphics_Chart.dispose();
Add_Chart_Content.addTemplate(template_Chart_Holder, 0, 0);
chart2 = SpecialJFreeChartBuilder.createChart2(
"Site and totalAmount ", results);
document.newPage();
PdfContentByte Add_Chart_Content1 = writer.getDirectContent();
PdfTemplate template_Chart_Holder1 = Add_Chart_Content1
.createTemplate(width, height);
Graphics2D Graphics_Chart1 = template_Chart_Holder1.createGraphics(
width, height, new DefaultFontMapper());
Rectangle2D Chart_Region1 = new Rectangle2D.Double(0, 0, 540, 380);
chart2.draw(Graphics_Chart1, Chart_Region1);
Graphics_Chart.dispose();
Add_Chart_Content.addTemplate(template_Chart_Holder1, 0, 0);
....
document.close();
Can anyone help me ....
Solved the issue ... Graphics_Chart.dispose(); must be : Graphics_Chart1.dispose(); Graphics_Chart2.dispose();
Related
In above code i want to use custom font (which is commented on code), when i use commented font1 then iam not getting output properly.
BufferedImage source = ImageIO.read(input_file);
File backImg = new File("C:\\Users\\saradhi\\Desktop\\water-lily-3784022__340.jpg");
BufferedImage backImgB = ImageIO.read(backImg);
final BufferedImage textImage = new BufferedImage(
backImgB.getWidth(),
backImgB.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = textImage.createGraphics();
FontRenderContext frc = g.getFontRenderContext();
/*
* Font font1 = Font.createFont(Font.TRUETYPE_FONT, new
* File("C:\\Users\\saradhi\\Desktop\\Pacifico.ttf")); font1.deriveFont(9f);
*/
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 120);
GlyphVector gv = font.createGlyphVector(frc, name);
Rectangle2D box = gv.getVisualBounds();
int xOff = 25 + (int) - box.getX();
int yOff = 80 + (int) - box.getY();
Shape shape = gv.getOutline(xOff, yOff);
g.setClip(shape);
g.drawImage(backImgB, 0, 0, null);
g.setClip(null);
g.setStroke(new BasicStroke(2 f));
g.setColor(Color.BLACK);
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.draw(shape);
g.dispose();
File file = new File("cat-text.png");
ImageIO.write(textImage, "png", new File("C:\\Users\\saradhi\\Desktop\\resultEX.jpeg"));
i get the custom font through this code
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("C:\\Pacifico.ttf"))
.deriveFont(48 f);
I work with the last version of itextPdf. And all used to work fine I don't know when it has begun to trouble.
I have that method to converts to pdf a GraphePinScene object.
public void toPdf(Scene scene, float clipWidth, float clipHeight, String path)
{
float pageWidth = (float) scene.getView().getWidth();
float pageHeight = (float) scene.getView().getHeight();
double xScale = 1.0;
double yScale = 1.0;
Document d = new Document();
PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(new File(path)));
d.open();
PdfContentByte cb = writer.getDirectContent();
//scaling
if (pageWidth > PDF_MAX_WIDTH) {
xScale = (double) (pageWidth / PDF_MAX_WIDTH);
pageWidth = PDF_MAX_WIDTH;
}
if (pageHeight > PDF_MAX_HEIGHT) {
xScale = (double) (pageHeight / PDF_MAX_HEIGHT);
pageHeight = PDF_MAX_HEIGHT;
}
Graphics2D g2d = new PdfGraphics2D(cb, pageWidth + clipWidth, pageHeight + clipHeight);
g2d.scale(xScale, yScale);
scene.paint(g2d);
g2d.dispose();
d.close();
}
And the scene object contains some Widgets. All widgets are included in the pdf only the ones that are created with GradientPaint are not imaged in the pdf file, this is one of the widgets:
public final class SecondaryStationWidget extends IconNodeWidget {
//Staff
#Override
protected void paintWidget() {
Graphics2D g = getGraphics();
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHints(rh);
g.setPaint(new GradientPaint(44, 29, new Color(154, 191, 239), 88, 58, new Color(17, 74, 148), true));
g.fill(new RoundRectangle2D.Double(1, 2,
88,
58,
10, 10));
}
}
The probleme has been resolved by changing (forcing) the background of those kind of widgets (in the constructor).
setBackground(new Color(0, 0, 0, 1)); //Remove
setBackground(Color.WHITE); //Add
I am using itext liberary for pdf generation in mine JavaFX project.Now i want's to print hindi , punjabi text from database inside report , How can i do this. However IText library is not recognise hindi and punjabi unicode ???
here is mine code
out = new ByteArrayOutputStream();
try {
// initialize document with page size and margins
document = new Document(PageSize.A4, 60, 40, 60, 60);
this.setAlignmentOfCompanyInfo(Common.comp
.getReportsHeaderAlignment());
writer = PdfWriter.getInstance(document, out);
writer.setPageEvent(new PageEventForAddressBookReport());
document.open();PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(w, h);
#SuppressWarnings("deprecation")
Graphics2D g2 = tp.createGraphicsShapes(w, h);
g2.drawString("वर्तमान शेष", 200, 100);
g2.dispose();
g2.setColor(Color.GREEN);
cb.addTemplate(tp, 50, 400);
BufferedImage bi = new BufferedImage(100, 20, BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();
ImageIO.write(bi, "PNG", new File("ourImageName.PNG"));
Image image = Image.getInstance(tp);
document.add(new Chunk(image,5,5));
document.close();
As itext is not supporting vernacular language, convert text to bitmap and set as image. Use below method for conversion:
public Bitmap textAsBitmap(String text, float textSize, float stroke,
int color) {
TextPaint paint = new TextPaint();
paint.setTextSize(textSize);
paint.setAntiAlias(true);
// paint.setTextAlign(Paint.Align.LEFT);
paint.setAntiAlias(true);
paint.setColor(color);
// paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(20f);
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
float baseline = (int) (-paint.ascent() + 3f); // ascent() is negative
StaticLayout staticLayout = new StaticLayout(text, 0, text.length(),
paint, 435, android.text.Layout.Alignment.ALIGN_NORMAL, 1.0f,
1.0f, false);
// int linecount = staticLayout.getLineCount();
//
// int height = (int) (baseline + paint.descent() + 3) * linecount + 10;
Bitmap image = Bitmap.createBitmap(staticLayout.getWidth(),
staticLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
// canvas.drawARGB(100, 100, 100, 100);
staticLayout.draw(canvas);
return image;
}
It's never a good idea to add the text in a notation that isn't Unicode, but that's not the main problem. The main problem is that you aren't providing the right font. You need a font that knows how to draw Indic glyphs. Arial Unicode MS is such a font:
String text = "\u0936\u093e\u0902\u0924\u093f";
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(100, 50);
Graphics2D g2 = tp.createGraphicsShapes(100, 50);
java.awt.Font font = new java.awt.Font(
"Arial Unicode MS", java.awt.Font.PLAIN, 12);
g2.setFont(font);
g2.drawString("Graphics2D: " + text, 0, 40);
g2.dispose();
cb.addTemplate(tp, 36, 750);
Note that this assumes that Java has access to the font arialuni.ttf.
i am using hindi text to be appear in PDf as below
BaseFont unicode = BaseFont.createFont("/home/mani/Documents/Back up (works)/Devanagari/Devanagari.ttf",
BaseFont.COURIER, BaseFont.EMBEDDED);
Font font=new Font(unicode,12,Font.NORMAL,new BaseColor(50,205,50));
table = new PdfPTable(new float[] { 10, 60, 30 });
table.setWidthPercentage(100);
PdfPCell customerLblCell = new PdfPCell(new Phrase("karent balance",
font));
Its working fine in mine case .
say in my program, i have this paint() method. my wish is to create an image of the rectangles that are drawn (with the for loop). I tried the method below and it did give me those rectangles (blue color), but the background is all black. When I run program without creating image, just drawing the rect on a JFrame, the background is white. How can i fix this. ?
public void paint(Graphics g) {
super.paint(g);
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = Image.getGraphics(); <<<----- is this correct?
g.setColor(Color.blue);
for ( ..... ) {
g.fillRect(X , Y, width , height);
....
}
try {
ImageIO.write(image, "jpg", new File("CustomImage.jpg"));
}catch (IOException e) {
e.printStackTrace();
}
}
The background is black in your image because you are not giving any pixels a value except those in the rectangles. The BufferedImage is starting out with every pixel having RGB of (0, 0, 0), which is black. To give the entire image a white background, simply fill the entire rectangle that is the image with white.
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = image.createGraphics(); // not sure on this line, but this seems more right
g.setColor(Color.white);
g.fillRect(0, 0, 100, 100); // give the whole image a white background
g.setColor(Color.blue);
for( ..... ){
g.fillRect(X , Y, width , height );
....
}
Note that my answer is about writing the image to a file with a white background, not about drawing to the JFrame with a black background. I'm not entirely sure which one you wanted.
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
Font font = new Font("Georgia", Font.BOLD, 18);
g2d.setFont(font);
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
GradientPaint gp = new GradientPaint(0, 0,
Color.red, 0, height/2, Color.black, true);
g2d.setPaint(gp);
g2d.fillRect(0, 0, width, height);
g2d.setColor(new Color(255, 153, 0));
Try this
public void paint(Graphics g) {
super.paint(g);
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = Image.createGraphics(); // it should be createGraphics
g.setBackground(Color.black);
g.setColor(Color.blue);
for( ..... ){
g.fillRect(X , Y, width , height );
....
}
try {
ImageIO.write(image, "jpg", new File("CustomImage.jpg"));
}catch (IOException e) {
e.printStackTrace();
}
}
It should be createGraphics.
http://docs.oracle.com/javase/tutorial/2d/images/drawonimage.html
..
In my application I have an image (world map) as background picture. Over this background picture, there is a polygon with a color gradient and transparent filling effect.
Here you find a code snippet of the overlay:
public void paint(Graphics g) {
//draw a polygon with a gradient filling effect
Graphics2D g2 = (Graphics2D)g;
GradientPaint gp = new GradientPaint(x1, y1, color1, x2, y2, color2, false);
g2.setPaint(gp);
g2.fill(polygon);
}
Does somebody know a method to get the color of one pixel of the overlay? I don't need the color, which can be seen on the screen including the background picture - just the color of the overlay.
Best regards,
Michael
This is somewhat ugly, but works:
GradientPaint gp = new GradientPaint(0, 0, new Color(255, 0, 0, 50),
10, 10, new Color(128, 255, 0, 150));
ColorModel cm = ColorModel.getRGBdefault();
Rectangle r = new Rectangle(0, 0, 10, 10);
Raster raster = gp.createContext(cm, r, r, new AffineTransform(), null)
.getRaster(0, 0, 10, 10);
int[] rgba = raster.getPixel(5, 5, (int[])null);
Alternately, you could just paint the overlay into a BufferedImage (which you had first cleared to transparent).