Hello,
I am trying to associate a PdfAction with a rectangular area in PdfContentByte object
using setAction() method. I made a small test case that draws a number of text strings
using Graphics2D object created by PdfContentByte. For easy identification, the
activated area is painted yellow. The problem is that the action does not seem to take
hold: even the mouse cursor fails to turn into the "pointing finger" when flown over
the area. Could someone have a look at the code and tell me where I am going wrong?
The code is ready to compile and is listed below. It creates Pears.pdf file that
demonstrates the problem.
TIA
Alex Molochnikov
Gestalt Corporation
import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class ActionTest
{
public static void main(String[] args)
{
int w = 150;
int h = 500;
Document document = new Document(new com.lowagie.text.Rectangle(300, 500));
try
{
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream("Pears.pdf"));
document.open();
DefaultFontMapper mapper = new DefaultFontMapper();
mapper.insertDirectory("c:\\windows\\fonts");
PdfContentByte cb = writer.getDirectContent();
Graphics2D g2 = cb.createGraphics(w, h, mapper);
g2.setColor(Color.black);
java.awt.Font thisFont = new java.awt.Font("Arial", java.awt.Font.PLAIN,
18);
g2.setFont(thisFont);
String pear = "Pear";
FontMetrics metrics = g2.getFontMetrics();
int width = metrics.stringWidth(pear);
/*
** Create 10 text strings "PearN"
*/
for (int i = 0; i < 10; i++)
{
int y = i * 30;
if (i == 5)
{
/*
** "Assign" the action to Pear5
*/
g2.setColor(Color.blue);
PdfAction action = new PdfAction("c:\\windows\\notepad.exe", null,
null, null);
int height = 20;
int xOrigin = (w - width) / 2 - 2;
int yOrigin = (y + 30) - height + 3;
int stringWidth = metrics.stringWidth(pear + " " + i) + 5;
cb.setAction(action, xOrigin, yOrigin + height, xOrigin +
stringWidth, yOrigin);
/*
** Draw the highlighting rectangle around Pear5
*/
g2.setColor(Color.yellow);
g2.fillRect(xOrigin, yOrigin, stringWidth, height);
g2.setColor(Color.black);
g2.drawRect(xOrigin, yOrigin, stringWidth, height);
}
g2.drawString(pear + " " + i, (w - width) / 2, y + 30);
g2.setColor(Color.black);
}
g2.dispose();
}
catch(DocumentException de) { System.err.println(de.getMessage()); }
catch(IOException ioe) { System.err.println(ioe.getMessage()); }
document.close();
}
}
N�HSDM隊X���'���u����-y�&Rw^��Z��h��)��^rܨ��.��&z�)z�(��b��+��b�v�jezg��)�I"�*.�
Zr�y�����
X���z� z�.�TD8Z�׀��!xk�u�m���i�,���y�+��� �����������m��4�o^�$��ڮz�b�{&j)b�
b�ؓ{j��-���+-��.�ǟ����a��l��b��,���y�+���b��?�+-�w��+^�ڮz�b�{