What is the default Japanese font when using Apache POI HSLFSlide.draw? - java

I am using Apache POI to convert pptx slide to image. In the pptx slide, I have Japanese text in GE Inspira font that is not available in my system (comment out ge.registerFont(font) to simulate that). The generated image shows the Japanese text in a default font (see image here). What font is that and where is this default font set?
When I register the font, the Japanese text appears as boxes (see image here). This is because GE Inspira font does not support Japanese characters. Is there a way to force POI to use the default font for Japanese text?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTextBox;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
public class UnicodePPT {
public static void main(String[] args) throws Exception {
// create a sample pptx
XMLSlideShow ss = new XMLSlideShow();
Dimension pgsize = ss.getPageSize();
XSLFSlide slide = ss.createSlide();
XSLFTextBox tb = slide.createTextBox();
// tb.setShapeType(XSLFShapeType.HEART);
int shapeSize = 150;
tb.setAnchor(new Rectangle((int)(pgsize.getWidth() / 2 - shapeSize / 2), (int)(pgsize.getHeight()
/ 2
- shapeSize
/ 2), shapeSize, shapeSize));
tb.setLineWidth(2);
tb.setLineColor(Color.BLACK);
XSLFTextParagraph par = tb.addNewTextParagraph();
tb.setVerticalAlignment(VerticalAlignment.DISTRIBUTED);
par.setTextAlign(TextAlign.CENTER);
XSLFTextRun run = par.addNewTextRun();
run.setText("ゴミ箱");
run.setFontFamily("GE Inspira");
run.setFontSize(12.0);
// set the font
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
InputStream is = new FileInputStream("src/test/resources/GEInspRg.TTF");
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
is.close();
ge.registerFont(font);
// render it
double zoom = 2; // magnify it by 2
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);
BufferedImage img = new BufferedImage((int)Math.ceil(pgsize.width * zoom),
(int)Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setTransform(at);
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slide.draw(graphics);
FileOutputStream fos = new FileOutputStream("src/test/resources/unicodeppt.png");
javax.imageio.ImageIO.write(img, "png", fos);
fos.close();
}
}

As mentioned in the comments, the Inspira GE font is not an unicode font, therefore a fallback is needed. If no specific fallback font is specified, Font.SANS_SERIF will be used. For the logical fonts like SANS_SERIF there is java internal fallback configuration. Unfortunately the automatically added Lucida fonts do not support Chinese (Simplified), Chinese (Traditional), Japanese, and Korean.
So you should provide a unicode font, e.g. Mona, Code2000 and Arial Unicode MS are a good picks - wikipedia has also good collection.
The mapping is provided as POI-specific rendering hint in the form of Map<String,String>, where the key is original font family and value the substitution font. You can also specify "*" as a key, to catch-all fonts. Beside the FONT_FALLBACK, there's also a FONT_MAP hint to map all the occurrences, i.e. not only the missing glyphs.
This will be available in POI 3.16-beta2 (ETA February 2017) or you temporarily use the trunk - another option is to provide your own DrawTextParagraph via a customized DrawFactory
To find out, if your font is capable of rendering your chars/glpyhs, you need to open it in the Windows character map tool or some other font tool like FontForge and check if there's a glyph at the unicode block.
#Test
public void unicodeRendering() throws Exception {
// create a sample pptx
XMLSlideShow ss = createSamplePPT();
Dimension pgsize = ss.getPageSize();
// set the font
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
for (String s : new String[]{"GEInspRg.ttf","mona.ttf"}) {
Font font = Font.createFont(Font.TRUETYPE_FONT, new File(s));
ge.registerFont(font);
}
Map<String,String> fallbackMap = new HashMap<String,String>();
fallbackMap.put("GE Inspira", "Mona");
// render it
double zoom = 2; // magnify it by 2
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);
BufferedImage img = new BufferedImage((int)Math.ceil(pgsize.width * zoom),
(int)Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setRenderingHint(Drawable.FONT_FALLBACK, fallbackMap);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphics.setTransform(at);
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
ss.getSlides().get(0).draw(graphics);
javax.imageio.ImageIO.write(img, "png", new File("unicodeppt.png"));
ss.close();
}
private XMLSlideShow createSamplePPT() {
XMLSlideShow ss = new XMLSlideShow();
Dimension pgsize = ss.getPageSize();
XSLFSlide slide = ss.createSlide();
XSLFTextBox tb = slide.createTextBox();
// tb.setShapeType(XSLFShapeType.HEART);
int shapeSize = 150;
tb.setAnchor(new Rectangle((int)(pgsize.getWidth() / 2 - shapeSize / 2),
(int)(pgsize.getHeight() / 2 - shapeSize / 2), shapeSize, shapeSize));
tb.setLineWidth(2);
tb.setLineColor(Color.BLACK);
XSLFTextParagraph par = tb.addNewTextParagraph();
tb.setVerticalAlignment(VerticalAlignment.DISTRIBUTED);
par.setTextAlign(TextAlign.CENTER);
XSLFTextRun run = par.addNewTextRun();
run.setText("unicode ->\u30B4\u30DF\u7BB1<-");
run.setFontFamily("GE Inspira");
run.setFontSize(12.0);
return ss;
}

Related

Rotate a rectangle with paragraph or image as content in itext7 version 7.1.0 [duplicate]

I have to a rectangle with some context as text in it. I have to rotate the rectangle to some angle lets say 90' anti clockwise such that the content also gets rotated. However when i am writing this code, it only shows a single rectangle.
This is the code:
package main;
import java.io.FileNotFoundException;
import com.itextpdf.kernel.geom.AffineTransform;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.property.Property;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.VerticalAlignment;
public class main {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
String dest = "C:\\Users\\beast\\Desktop\\samplePdf";
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfPage page = pdf.addNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle rect1 = new Rectangle(183, 488, 180, 32);
// cordinates after rotation 90' counter clockwise
Rectangle rect2 = new Rectangle(151, 488, 32, 180);
pdfCanvas.rectangle(rect1);
pdfCanvas.stroke();
Canvas canvas = new Canvas(pdfCanvas, pdf, rect1);
Text title = new Text("Thbvhs ybhsvb");
Paragraph p = new Paragraph().add(title);
canvas.add(p);
AffineTransform transform=AffineTransform.getRotateInstance((float)(Math.PI/2));
pdfCanvas.concatMatrix(transform);
canvas.close();
canvas=new Canvas(pdfCanvas, pdf, rect2);
pdfCanvas.rectangle(rect2);
pdfCanvas.stroke();
canvas.add(p);
canvas.close();
pdf.close();
}
}
this is the output of the pdf:
With your introduction of the AffineTransform transform you already were on the right path. But you have to apply the rotation before adding the content you want to be rotated. Transformations here don't change the content already added, they change the coordinate system for the content to come.
Additionally you need to make transform rotate around a sensible rotation center. Your instance rotates around the origin of the coordinate system, i.e. the lower left corner of your page, but you apparently want to rotate around the lower left corner of your rectangle.
Thus:
try (PdfDocument pdf = new PdfDocument(new PdfWriter("RotatedForAnkushGupta.pdf"))) {
PdfPage page = pdf.addNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle rect1 = new Rectangle(183, 488, 180, 32);
AffineTransform transform = AffineTransform.getRotateInstance(Math.PI/2, rect1.getX(), rect1.getY());
pdfCanvas.concatMatrix(transform);
pdfCanvas.rectangle(rect1);
pdfCanvas.stroke();
try (Canvas canvas = new Canvas(pdfCanvas, rect1)) {
Text title = new Text("Thbvhs ybhsvb");
Paragraph p = new Paragraph().add(title);
canvas.add(p);
}
}
(RotateSomeContent test testForAnkushGupta)
You may have to add pdf as second parameter of the Canvas constructor. In iText 7.2.x (which I used here) that parameter has been removed while it appears to still be required in the version 7.1.0 you use.
The result:

Graphics2D - identical text rendering on all platforms

Is it possible to render pixel-by-pixel identical result with Graphics2D.drawString on all java platforms with same font? I tried to render text without antialiasing, but results differs on different machines. I use font from project resources, so font is system-independent.
Example of results of same code with same font on two different PCs:
I'm used very small font size (9 px) for clarity.
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class Test {
public static void main(String... args) throws FontFormatException, IOException {
int x = 0;
int y = 9;
int width = 80;
int height = 10;
float fontSizeInPixels = 9f;
String text = "PseudoText";
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
URL fontUrl = new URL(
"https://github.com/indvd00m/graphics2d-drawstring-test/blob/master/src/test/resources/fonts/DejaVuSansMono/DejaVuSansMono.ttf?raw=true");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
font = font.deriveFont(fontSizeInPixels);
Color fontColor = Color.BLACK;
Color backgroundColor = Color.WHITE;
graphics.setFont(font);
graphics.setColor(backgroundColor);
graphics.fillRect(0, 0, width, height);
graphics.setColor(fontColor);
graphics.drawString(text, x, y);
ImageIO.write(image, "png", new File("/tmp/test.png"));
}
}
I'm created project for test here:
https://github.com/indvd00m/graphics2d-drawstring-test
Failed build of this project:
https://travis-ci.org/indvd00m/graphics2d-drawstring-test/builds/178672466
Tests passed under openjdk6 and openjdk7 on linux but failed under oraclejdk7 and oraclejdk8 on linux and other OS and java versions.
Rendering text is extremely complex (evolving Opentype and Unicode specs, trying to fit small symbols on screens with too few pixels, work-arounding font bugs, etc).
It is so complex it is mostly system-dependent, with one remaining rendering engine per major OS. Those engines are continuously evolving to try to fix problems, support hardware changes and new spec revisions. They do not give the same results from system to system and OS version to OS versions. Sometimes apps like Oracle JDK add one more level of complexity by including their own rendering routines (for legacy compat, Open JDK uses the system rendering directly and gives better results on modern *nix systems).
You can try to erase some of the differences by rendering at very high pixel dimensions. That will remove all the lowdpi grid fitting black magic differences, and render close to the ideal high-quality paper printing. However the text will be very hard to read when scaled down to actual screen pixels.
In the meanwhile, differing interpretations on where and how put text pixels for better reading quality are a fact of life. You'd better not try to code anything that assumes otherwise. The text stack people will win, it's a very Darwinian process, the few text rendering engines that remain are survivors.
OK.. not sure this constitutes an 'answer' as such (more an experiment) but this is what I mean about scaling the text to fit the space. See comments in code.
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import javax.imageio.ImageIO;
public class TestFontWidthScaling {
private static Font font;
private static float fontSizeInPixels = 36f;
private static String text = "PseudoText";
private static int width = 320;
private static int height = 40;
private static BufferedImage scaleImageToFit() {
BufferedImage image = new BufferedImage(
width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
// graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
// graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
// we need line antialiasing here
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
FontRenderContext frc = graphics.getFontRenderContext();
// this is important for determining the *current* size of the
// text using this font.
Area area = new Area(font.
createGlyphVector(frc, text).
getOutline());
Rectangle2D bounds = area.getBounds2D();
double w = bounds.getWidth();
double h = bounds.getHeight();
double scaleW = width*.95 / w;
double scaleH = height*.9 / h;
AffineTransform scale = AffineTransform.getScaleInstance(scaleW, scaleH);
// we now have the shape of the text that will fill a fixed percentage
// of the width and height of the assigned space.
area = area.createTransformedArea(scale);
// now to center it
bounds = area.getBounds2D();
double moveX = bounds.getCenterX() - width/2;
double moveY = bounds.getCenterY() - height/2;
AffineTransform move = AffineTransform.getTranslateInstance(-moveX, -moveY);
// this should be both scaled to size AND centered in the space
area = area.createTransformedArea(move);
Color fontColor = Color.BLACK;
// changed to make image bounds more obvious on white BG
Color backgroundColor = Color.CYAN;
graphics.setFont(font);
graphics.setColor(backgroundColor);
graphics.fillRect(0, 0, width, height);
graphics.setColor(fontColor);
graphics.draw(area);
graphics.fill(area);
return image;
}
public static void main(String... args) throws FontFormatException, IOException {
int x = 0;
int y = 36;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
URL fontUrl = new URL(
"https://github.com/indvd00m/graphics2d-drawstring-test/blob/master/src/test/resources/fonts/DejaVuSansMono/DejaVuSansMono.ttf?raw=true");
font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
font = font.deriveFont(fontSizeInPixels);
Color fontColor = Color.BLACK;
Color backgroundColor = Color.WHITE;
graphics.setFont(font);
graphics.setColor(backgroundColor);
graphics.fillRect(0, 0, width, height);
graphics.setColor(fontColor);
graphics.drawString(text, x, y);
String userHome = System.getProperty("user.home");
File f = new File(userHome);
f = new File(f, "test.png");
ImageIO.write(image, "png", f);
Desktop.getDesktop().open(f);
BufferedImage scaledImage = scaleImageToFit();
f = new File(f.getParentFile(), "test-scaled.png");
ImageIO.write(scaledImage, "png", f);
Desktop.getDesktop().open(f);
}
}

Drawing vector images on PDF with PDFBox

I would like to draw a vector image on a PDF with Apache PDFBox.
This is the code I use to draw regular images
PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(1);
PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true);
BufferedImage _prevImage = ImageIO.read(new FileInputStream("path/to/image.png"));
PDPixelMap prevImage = new PDPixelMap(document, _prevImage);
contentStream.drawXObject(prevImage, prevX, prevY, imageWidth, imageHeight);
If I use a svg or wmf image instead of png, the resulting PDF document comes corrupted.
The main reason I want the image to be a vector image is that with PNG or JPG the image looks horrible, I think it gets somehow compressed so it looks bad. With vector images this shouldn't happen (well, when I export svg paths as PDF in Inkscape it doesn't happen, vector paths are preserved).
Is there a way to draw a svg or wmf (or other vector) to PDF using Apache PDFBox?
I'm currently using PDFBox 1.8, if that matters.
See the library pdfbox-graphics2d, touted in this Jira.
You can draw the SVG, via Batik or Salamander or whatever, onto the class PdfBoxGraphics2D, which is parallel to iText's template.createGraphics(). See the GitHub page for samples.
PDDocument document = ...;
PDPage page = ...; // page whereon to draw
String svgXML = "<svg>...</svg>";
double leftX = ...;
double bottomY = ...; // PDFBox coordinates are oriented bottom-up!
// I set these to the SVG size, which I calculated via Salamander.
// Maybe it doesn't matter, as long as the SVG fits on the graphic.
float graphicsWidth = ...;
float graphicsHeight = ...;
// Draw the SVG onto temporary graphics.
var graphics = new PdfBoxGraphics2D(document, graphicsWidth, graphicsHeight);
try {
int x = 0;
int y = 0;
drawSVG(svg, graphics, x, y); // with Batik, Salamander, or whatever you like
} finally {
graphics.dispose();
}
// Graphics are not visible till a PDFormXObject is added.
var xform = graphics.getXFormObject();
try (var contentWriter = new PDPageContentStream(document, page, AppendMode.APPEND, false)) { // false = don't compress
// XForm objects have to be placed via transform,
// since they cannot be placed via coordinates like images.
var transform = AffineTransform.getTranslateInstance(leftX, bottomY);
xform.setMatrix(transform);
// Now the graphics become visible.
contentWriter.drawForm(xform);
}
And ... in case you want also to scale the SVG graphics to 25% size:
// Way 1: Scale the SVG beforehand
svgXML = String.format("<svg transform=\"scale(%f)\">%s</svg>", .25, svgXML);
// Way 2: Scale in the transform (before calling xform.setMatrix())
transform.concatenate(AffineTransform.getScaleInstance(.25, .25));
I do this, but not directly.
In first transform your SVG documents in PDF documents with FOP librairy and Batik.
https://xmlgraphics.apache.org/fop/dev/design/svg.html.
In second times, you can use LayerUtility in pdfbox to transform your new pdf document in PDXObjectForm. After that, just needs to include PDXObjectForm in your final pdf documents.
The final working solution for me that loads an SVG file and overlays it on a PDF file (this renders the SVG in a 500x500 box at (0,0) coordinate which is bottom left of the PDF document):
package com.example.svgadder;
import java.io.*;
import java.nio.*;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D;
import java.awt.geom.AffineTransform;
import com.kitfox.svg.SVGDiagram;
import com.kitfox.svg.SVGException;
import com.kitfox.svg.SVGUniverse;
public class App
{
public static void main( String[] args ) throws Exception {
App app = new App();
}
public App() throws Exception {
// loading PDF and SVG files
File pdfFile = new File("input.pdf");
File svgFile = new File("input.svg");
PDDocument doc = PDDocument.load(pdfFile);
PDPage page = doc.getPage(0);
SVGUniverse svgUniverse = new SVGUniverse();
SVGDiagram diagram = svgUniverse.getDiagram(svgUniverse.loadSVG(f.toURL()));
PdfBoxGraphics2D graphics = new PdfBoxGraphics2D(doc, 500, 500);
try {
diagram.render(graphics);
} finally {
graphics.dispose();
}
PDFormXObject xform = graphics.getXFormObject();
try (PDPageContentStream contentWriter = new PDPageContentStream(doc, page, AppendMode.APPEND, false)) {
AffineTransform transform = AffineTransform.getTranslateInstance(0, 0);
xform.setMatrix(transform);
contentWriter.drawForm(xform);
}
doc.save("res.pdf");
doc.close();
}
}
Please use svgSalamander from here:
https://github.com/mgarin/svgSalamander
Please use what Coemgenus suggested for scaling your final overlaid SVG. I tried the 2nd option and it works well.
Nirmal

Writing image into pdf file in java

I'm writing a code to convert Microsoft power-point(ppt) slides into images and to write the generated images into pdf file. Following code generates and writes the images into pdf file but the problem i'm facing is, when i write image into pdf file it's size is exceeding the pdf page size and i can view only 75% of the image rest is invisible. One more thing to notice here is, written images in pdf file look like zoomed or expanded. Take a look at the following snippet of code:
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.white);
graphics.fill(new Rectangle(0, 0, pgsize.width, pgsize.height));
slide[i].draw(graphics);
fileName="C:/DATASTORE/slide-"+(i+1)+".png";
FileOutputStream out = new FileOutputStream(fileName);
javax.imageio.ImageIO.write(img, "png", out);
out.flush();
out.close();
com.lowagie.text.Image image =com.lowagie.text.Image.getInstance(fileName);
image.setWidthPercentage(40.0f);
doc.add((image));
}
doc.close();
} catch(DocumentException de) {
System.err.println(de.getMessage());
}
If anybody knows the solution please help me to rectify. Thank you.
Here is the code it accomplishes the task i wished. Now i'm getting the desired results after following Bruno Lowagie recommendations.
But, as Bruno Lowagie pointed out earlier, their is a problem in generated png image. The generated png image is not correct because shape or image in the slide overlaps with the texts of the slide. Can you help me to identify and rectify the error?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import com.itextpdf.text.Image;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
public class ConvertSlidesIntoImages {
public static void main(String[] args){
try {
FileInputStream is = new FileInputStream("C:/DATASTORE/testPPT.ppt");
SlideShow ppt = new SlideShow(is);
is.close();
String fileName;
Dimension pgsize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
Document doc=new Document();
PdfWriter.getInstance(doc, new FileOutputStream("c:/DATASTORE/convertPPTSlidesIntoPDFImages.pdf"));
doc.open();
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.white);
graphics.fill(new Rectangle(0, 0, pgsize.width, pgsize.height));
slide[i].draw(graphics);
fileName="C:/DATASTORE/slide-"+(i+1)+".png";
FileOutputStream out = new FileOutputStream(fileName);
javax.imageio.ImageIO.write(img, "png", out);
out.flush();
out.close();
com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance(fileName);
doc.setPageSize(new com.itextpdf.text.Rectangle(image.getScaledWidth(), image.getScaledHeight()));
doc.newPage();
image.setAbsolutePosition(0, 0);
doc.add(image);
}
doc.close();
}catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(Exception ex) {
ex.printStackTrace();
}
}
Thank you
First this: If the png stored as "C:/DATASTORE/slide-"+(i+1)+".png" isn't correct, the slide in the PDF won't be correct either.
And this: Your code snippet doesn't show us how you create the Document object. By default, the page size is A4 in portrait. It goes without saying that images that are bigger than 595 x 842 don't fit that page.
Now the answer: There are two ways to solve this.
Either you change the size of the image (not with setWidthPercentage() unless you've calculated the actual percentage) and you add it a the position (0, 0) so that it doesn't take into account the margins. For instance:
image.scaleToFit(595, 842);
image.setAbsolutePosition(0, 0);
doc.add(image);
doc.newPage();
A better solution would be to adapt the size of the page to the size of the image.
Document doc = new Document(new Rectangle(image.getScaledWidth(), image.getScaledHeight()));
// create a writer, open the document
image.setAbsolutePosition(0, 0);
doc.add(image);
doc.newPage();
If the size of the images varies, you can change the page size while adding images like this:
doc.setPageSize(new Rectangle(image.getScaledWidth(), image.getScaledHeight()));
doc.newPage();
image.setAbsolutePosition(0, 0);
doc.add(image);
It is important to understand that the new page size will only come into effect after doc.newPage();
CAVEAT 1:
If your PDF only holds the last slide, you're probably putting all the slides on the same page, and the last slide covers them all. You need to invoke the newPage() method each time you add an image (as done in a code snippet in my answer).
CAVEAT 2:
Your allegation is wrong. According to the API docs, there is a method setPageSize(Rectangle rect), maybe you're using the wrong Rectangle class. If you didn't follow my advice (which IMHO wouldn't be wise), you're probably looking for com.lowagie.text.Rectangle instead of java.awt.Rectangle.
CAVEAT 3:
This is similar to CAVEAT 2, there are indeed no such methods in the class java.awt.Image, but as documented in the API docs, the class com.itextpdf.text.Image has a getScaleWidth() method and a getScaledHeight() method.

How to increase Graphics2D text quality?

I have a question concerning printing additional information on barcodes. I am using http://barbecue.sourceforge.net/ to create my barcodes.
After I created my barcodes I want to add some additional information. At the moment i do this with the following way! For example:
Graphics2D g2d5 = container4Barcode.createGraphics();
g2d5.setBackground(Color.WHITE);
g2d5.clearRect(0, 33, 200, 200);
g2d5.setColor(Color.BLACK);
g2d5.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d5.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
g2d5.setFont(new Font("Arial", Font.PLAIN, 8));
g2d5.drawString(barcode, 8, 40);
g2d5.drawString(generateRandomNumber(ekPreis), 57, 40);
String datumResult = datum;
g2d5.drawString(location, 98, 40);
g2d5.drawString(datum.substring(2), 114, 40);
g2d5.dispose();
The output is in a pdf the following:
As you can see is the quality of my text (above and under the barcode) is really bad ... How can I increase the quality of the text to make the text more smoother and not that pixelated?!
(When I print my barcodes, the barcodes look very pixelated ...)
Any tips?
UPDATE:
So, I added here the a picture of my latest outcome ... When I print out these barcodes they look horrible! So here is the code what I did:
Graphics2D g2d6 = container4Barcode.createGraphics();
g2d6.setColor(Color.black);
g2d6.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d6.setFont(new Font("Verdana", Font.BOLD, 7));
g2d6.drawString("FLORETT", 9, 20);
g2d6.drawString("50-521-60", 57, 20);
Graphics2D g2d4 = container4Barcode.createGraphics();
g2d4.setColor(Color.black);
g2d4.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d4.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d4.setFont(new Font("Verdana", Font.BOLD, 11));
g2d4.drawString("SSYYS", 105, 19);
g2d4.dispose();
With that Code I get the best results! Of course I played with "Metrics, AA_GASP, LCS_HRGB, different fonts (Verdana is the best in my opinion) ..." and a lot more, but some of them I couldn't use, because then my barcode got blurred! So actioally I am forcing the problem that I am unable to improve the quality of my text-quality of the drawstring from graphics2d!
So, I want to ask if there is a possibility to let the "SSYYS" (Font Size 11) and the "FLORETT" (Font Size 7) look much nicer! Is there a possibility in JAVA to draw "smooth" text on an image with a font size less than "12" ? Is there a workaround to to that ? As you can see in the picture the letters "S and Y" look very awful...
2nd Update:
Some Example code so far... Please be sure that the following folder exists: C:\TestBarcodes\
Hope I reduced my code to the minimum that you can imagine what my problem is...
package generator;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import net.sourceforge.barbecue.Barcode;
import net.sourceforge.barbecue.BarcodeException;
import net.sourceforge.barbecue.BarcodeFactory;
import net.sourceforge.barbecue.output.OutputException;
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.graphics.xobject.PDJpeg;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
public class BarcodeGen {
// sets the picWidth
private static int picWidth = 149;
// sets the picHeigth
private static int picHeigth = 60;
public static void main(String[] args)
throws BarcodeException, OutputException, COSVisitorException, IOException {
generateBarcode("11138500");
}
public static void generateBarcode(String barcode)
throws IOException, COSVisitorException, BarcodeException, OutputException {
Barcode barcode2 = BarcodeFactory.createCode39(barcode, false);
int gw = barcode2.getWidth();
// change this to suit if you want higher, default 50
// barcode2.setBarWidth(50);
// this sets DPI
barcode2.setResolution(100);
// barcode2.setFont(font);
int gh = barcode2.getHeight();
// change this if you want a coloured background
// image = new BufferedImage(t, s, BufferedImage.TYPE_INT_RGB)
BufferedImage image = new BufferedImage(gw, gh, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) image.getGraphics();
// default is black so draw a white box first
// change type to INT_RGB if you want a coloured background
g2.setColor(Color.white);
g2.fillRect(0, 0, gw, gh);
barcode2.draw(g2, 0, 0);
// CREATE ADDITIONAL INFORMATION ON BARCODE
BufferedImage container4Barcode = new BufferedImage(
picWidth, picHeigth, image.getType());
Graphics2D g2d = container4Barcode.createGraphics();
g2d.setBackground(Color.WHITE);
g2d.clearRect(0, 0, picWidth, picHeigth);
g2d.setColor(Color.black);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
g2d.drawImage(image, 8, 21, 130, 18, null);
g2d.dispose();
Graphics2D g2d6 = container4Barcode.createGraphics();
g2d6.setColor(Color.black);
g2d6.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d6.setFont(new Font("Verdana", Font.BOLD, 7));
g2d6.drawString("FLORETT", 9, 20);
g2d6.drawString("50-521-60", 57, 20);
Graphics2D g2d4 = container4Barcode.createGraphics();
g2d4.setColor(Color.black);
g2d4.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d4.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d4.setFont(new Font("Verdana", Font.BOLD, 11));
g2d4.drawString("SSYYS", 105, 19);
g2d4.dispose();
// PRINT PDF
int ver = 782;
PDDocument doc = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
doc.addPage(page);
PDXObjectImage image2 = new PDJpeg(doc, container4Barcode);
PDPageContentStream content = new PDPageContentStream(doc, page);
content.drawImage(image2, 5, ver);
content.close();
doc.save(new FileOutputStream("C:\\TestBarcodes\\barcode.pdf"));
// opens the pdf file
Process p = Runtime
.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler C:\\TestBarcodes\\barcode.pdf");
try {
p.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
If someone wants to use pixel images in such cases, and not vector, then image should be upscaled for better printing quality:
static final int PIXELS_PER_POINT = 4; // 4x
Then define all dimensions in points, not in pixels:
// Image size in points
static final int IMAGE_WIDTH = 150;
static final int IMAGE_HEIGHT = 60;
// Font size in points
static final int FONT_SIZE = 11;
Now, when do any drawing, always use points converted to pixels:
static int toPixels(int value) {
return value * PIXELS_PER_POINT;
}
BufferedImage draw() {
BufferedImage image =
new BufferedImage(toPixels(IMAGE_WIDTH), toPixels(IMAGE_HEIGHT), TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
// <graphics init code goes here>
Font font = new Font("Arial", Font.PLAIN, toPixels(FONT_SIZE));
g.setFont(font);
g.drawString("Grapes", toPixels(5), toPixels(40)); // coordinates are in points
g.dispose()
return image;
}
So, with this approach you can operate with 'standard' dimentions. This approach works quite well for me for low- and medium complexity drawings.
You can go further and convert PIXELS_PER_POINT to a parameter: use 1x for images on web-pages with ordinary display, 2x for Retina displays and 4x for printing!
To fix the jagged edges in text or shapes in Graphics2d you need to set RenderingHint.
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
Explanation
A typical antialiasing algorithm works by blending the existing colors of the pixels along the boundary of a shape with the requested fill paint according to the estimated partial pixel coverage of the shape.
Text antialiasing hint key. The TEXT_ANTIALIASING hint can control the use of antialiasing algorithms for text independently of the choice used for shape rendering. Often an application may want to use antialiasing for text only and not for other shapes. Additionally, the algorithms for reducing the aliasing artifacts for text are often more sophisticated than those that have been developed for general rendering so this hint key provides additional values which can control the choices of some of those text-specific algorithms. If left in the DEFAULT state, this hint will generally defer to the value of the regular KEY_ANTIALIASING hint key.

Categories

Resources