On Fri, Jun 22, 2007 at 06:38:21PM +0100, Mark Hall wrote:
> On Wednesday 20 June 2007, Chad Loder wrote:
> > I am confused with the way image scaling works in PDF vs. RTF. I have
> > read the May 15th thread between Thomas Bickel and Mark Hall where
> > this is discussed:
>
> If you are using the last release, then image should be displayed at 72dpi in
> both PDF and RTF. This means that if you look at the properties of the image
> in Word, it should say 130% size increased. In older releases the RTF will
> display them at 96dpi.
Mark,
I have attached a test case to this email. I realize now after developing this
case that the problem lies with differences in how tables are rendered between
PDF and RTF. The PDF table cells are wide enough to show the image, while the
RTF table is not wide enough to show the image (even after the images have
been scaled). Oddly, the RTF table cells are the correct height, it's just the
width that is the problem.
When I set the table width larger than the default 80% in RTF, the images do
begin to fit horizontally. Can someone explain the difference here and maybe
suggest a way around this?
PS: You will need JFreeChart to compile and run the example code.
Thanks,
c
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.*;
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.io.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class ITextImageTest
{
/**
* Usage: ITextImageTest <-pdf|-rtf> <filename>
*/
public static void main(String[] args)
{
try
{
Document doc = new Document(PageSize.LETTER);
if (args[0].equals("-pdf"))
pdf = true;
else if (args[0].equals("-rtf"))
pdf = false;
else
throw new IllegalArgumentException("invalid argument: " + args[0]);
FileOutputStream fOut = new FileOutputStream(new File(args[1]));
try
{
DocWriter docWriter = createDocWriter(doc, fOut);
doc.open();
fillDoc(doc);
doc.close();
docWriter.close();
fOut.flush();
}
finally
{
fOut.close();
}
}
catch (Throwable t)
{
t.printStackTrace();
System.exit(1);
}
}
private static DocWriter createDocWriter(Document doc, OutputStream out)
throws DocumentException
{
DocWriter dw = null;
if (pdf)
{
dw = PdfWriter.getInstance(doc, out);
}
else
{
RtfWriter2 rw = RtfWriter2.getInstance(doc, out);
// the values of these settings seem to make little difference in RTF
output
//rw.getDocumentSettings().setImagePDFConformance(true);
//rw.getDocumentSettings().setWriteImageScalingInformation(false);
dw = rw;
}
dw.setPageSize(PageSize.LETTER);
return dw;
}
private static void fillDoc(Document doc)
throws DocumentException, IOException
{
doc.newPage();
Paragraph p = new Paragraph("First section");
Section s = new Chapter(p, 1);
p = new Paragraph("Here is a paragraph");
s.add(p);
Table table = new Table(2, 1);
s.add(table);
Cell c = new Cell();
c.add(createChartImage());
table.addCell(c);
c = new Cell();
c.add(createChartImage());
table.addCell(c);
doc.add(s);
}
private static Image createChartImage()
throws DocumentException, IOException
{
BufferedImage bi = createChart().createBufferedImage(320, 180);
ByteArrayOutputStream out = new ByteArrayOutputStream();
javax.imageio.ImageIO.write(bi, "jpg", out);
Image image = Image.getInstance(out.toByteArray());
image.setAlignment(com.lowagie.text.Image.MIDDLE);
scaleImage(image);
return image;
}
private static JFreeChart createChart()
{
DefaultCategoryDataset chartData = new DefaultCategoryDataset();
chartData.addValue((double)10, "First", "First");
chartData.addValue((double)50, "Second", "Second");
chartData.addValue((double)100, "Third", "Third");
JFreeChart chart = ChartFactory.createBarChart3D(
"Test chart", "Columns", "Values", chartData,
PlotOrientation.VERTICAL,
false, false, false);
chart.setAntiAlias(true);
chart.setBorderVisible(true);
chart.setBackgroundPaint(Color.WHITE);
return chart;
}
protected static void scaleImage(Image image)
{
if (pdf)
{
// we need a 72 dpi image for PDF
final int dpiX = image.getDpiX();
final int dpiY = image.getDpiY();
if (dpiX > 0 && dpiY > 0)
{
System.out.println("Scaling image from " + dpiX + "/" + dpiY);
if (dpiX != 72 || dpiY != 72)
{ // this is 24 for 24%, thus we use 7200 rather than 72
image.scalePercent((7200 / dpiX), (7200 / dpiY));
}
}
}
else
{
// if RTF images supposedly look 130% too large, this should work,
right?
// but they still look too wide.
float rtfPercent = (72.0f/96.0f) * 100.0f;
image.scalePercent(rtfPercent, rtfPercent);
}
}
private static boolean pdf = true;
}-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/