I'm trying to create a section of a page in EPS and then have that portion of EPS be imported into my PS document at a specific location using Tools for Adobe Postscript: https://xmlgraphics.apache.org/commons/postscript.html
So far I've got a basic generation for the postscript file, and I want to add EPS to it. Here's a pseudo version of what I'd like to do.
OutputStream out = new java.io.FileOutputStream(outputFile);
out = new java.io.BufferedOutputStream(out);
try {
//Instantiate the PSDocumentGraphics2D instance
PSDocumentGraphics2D g2d = new PSDocumentGraphics2D(false);
g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
EPSDocumentGraphics2D eg2d = new EPSDocumentGraphics2D(false);
eg2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
//Set up the document size
g2d.setupDocument(out, pageWidthPT, pageHeightPT);
eg2d.setupDocument(out, 400, 200); //400pt x 200pt
g2d.setFont(new Font(font, Font.PLAIN, fontSize));
//Paint a bounding box
eg2d.drawRect(0, 0, 400, 200);
//A few rectangles rotated and with different color
Graphics2D copy = (Graphics2D)eg2d.create();
int c = 12;
for (int i = 0; i < c; i++) {
float f = ((i + 1) / (float)c);
Color col = new Color(0.0f, 1 - f, 0.0f);
copy.setColor(col);
copy.fillRect(70, 90, 50, 50);
copy.rotate(-2 * Math.PI / (double)c, 70, 90);
}
copy.dispose();
//Some text
eg2d.rotate(-0.25);
eg2d.setColor(Color.RED);
eg2d.setFont(new Font("sans-serif", Font.PLAIN, 36));
eg2d.drawString("Hello world!", 140, 140);
eg2d.setColor(Color.RED.darker());
//Cleanup
eg2d.finish();
g2d.insertEPS(eg2d, 0,0); // something like this?
g2d.nextPage();
g2d.drawString("Hello World!", 10, 20);
g2d.finish();//Cleanup
} finally {
IOUtils.closeQuietly(out);
}
If you know how to accomplish this goal I would appreciate some enlightenment. Thanks.
Related
I'm trying to perform a signing and want to generate the same multiple appearances. Is there a way to achieve a custom appearance as we can generate in normal cases?
PdfSignatureFormField sigField = (PdfSignatureFormField) acroForm.getField(fieldName);
sigField.addKid(
createSubField(pdfPage, 40, 700, 100, 40, "sig1", signatureDictionary, document));
sigField.addKid(
createSubField(pdfPage, 400, 600, 100, 40, "sig2", signatureDictionary, document));
The following is the method which is generating subfields.
public static PdfFormField createSubField(PdfPage page, float x, float y, float width,
float height, String name, PdfDictionary signatureDictionary, PdfDocument pdfDocument) {
PdfFormXObject appearance = new PdfFormXObject(new Rectangle(width, height));
PdfCanvas pdfCanvas = new PdfCanvas(appearance, pdfDocument);
try (Canvas canvas = new Canvas(pdfCanvas, new Rectangle(width, height))) {
canvas.showTextAligned(name, 2, 2, TextAlignment.LEFT);
}
PdfDictionary ap = new PdfDictionary();
ap.put(PdfName.N, appearance.getPdfObject());
PdfWidgetAnnotation widget = new PdfWidgetAnnotation(new Rectangle(x, y, width, height));
widget.setFlags(PdfAnnotation.PRINT | PdfAnnotation.LOCKED);
widget.setPage(page);
widget.put(PdfName.AP, ap);
PdfSignatureFormField sigField = PdfFormField.createSignature(pdfDocument);
sigField.setFieldName(name);
sigField.addKid(widget);
sigField.put(PdfName.V, signatureDictionary);
page.addAnnotation(widget);
return sigField;
}
"I am trying to draw a QuadCurve line in Java. I am able to do this when I hard code the x and y values in each line, but when I try to draw the lines from a loop I get an unwanted line on the top. I want to have it in a loop so that I can create the values in another location and feed the values to my Drawing class. I think it may be a looping issue. I "stepped into" the code and the values in the array are all correct. I am in college so any help is appreciated. Thank you!
This code works:"
Graphics2D g2 = (Graphics2D) g.create();
g2.setPaint(Color.BLUE);
Shape drawLine1A = new QuadCurve2D.Float(40, 450, 100, 300, 210, 180);
Shape drawLine2A = new QuadCurve2D.Float(210, 180, 315, 150, 390, 240);
Shape drawLine3A = new QuadCurve2D.Float(390, 240, 430, 242, 480, 245);
g2.draw(drawLine1A);
g2.draw(drawLine2A);
g2.draw(drawLine3A);
"This code does not work correctly:"
Graphics2D g2 = (Graphics2D) g.create();
g2.setPaint(Color.BLUE);
Shape line;
int[] x = {40, 100, 210, 210, 315, 390, 390, 430, 480};
int[] y = {450, 300, 180, 180, 150, 240, 240, 242, 245};
int k;
int h;
for(int i = 0; i < 7; i++)
{
k = i + 1;
h = k + 1;
line = new QuadCurve2D.Float(x[i], y[i], x[k], y[k], x[h], y[h]);
g2.draw(line);
}
"I figured this out after. i had to increment i by 3 in order to have the coordinates in the right spots. Simple fix. I was looking at it for too long!"
I have this piece of code that make me somehow a latex formula in a image and display to a pdf.The first thing is i have a error when i open the pdf file 109. and the first page with the formulas is not displayed at all.
the next ones is ok.
Any ideas? Thanks
document.open();
Paragraph preface = new Paragraph();
// We add one empty line
addEmptyLine(preface, 1);
// Lets write a big header
preface.add(new Paragraph("Test Generat", catFont));
addEmptyLine(preface, 1);
document.add(preface);
// Start a new page
// document.newPage();
for (int i = 0; i < intrebari.size(); i++) {
TeXFormula formula = new TeXFormula("x=\\frac{-b \\pm \\sqrt {b^2-4ac}}{2a}");
TeXIcon icon = formula
.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);
// insert a border
icon.setInsets(new Insets(5, 5, 5, 5));
// now create an actual image of the rendered equation
BufferedImage image = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
JLabel jl = new JLabel();
jl.setForeground(new Color(0, 0, 0));
icon.paintIcon(jl, g2, 0, 0);
document.setPageSize(PageSize.A4);
PdfContentByte pdfCB = new PdfContentByte(writer);
try {
Image image1 = Image.getInstance(pdfCB, image, 1);
document.newPage();
Phrase p = new Phrase("quick brown fox jumps over the lazy dog.");
p.add(new Chunk(image1, 0, 0, true));
p.add(" ");
p.add(new Chunk(image1, 0, 0, true));
ColumnText ct = new ColumnText(writer.getDirectContent());
ct.setSimpleColumn(new Rectangle(20, document.getPageSize().getWidth(), 400, 800));
ct.addText(p);
ct.go();
document.newPage();
} catch (BadElementException | IOException ex) {
Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
}
}
document.newPage();
}
I am doing this small race between two cars, in a java applet.
Just two pictures moving at random speed. I am calculating the distance between current position and the finish line, and you are suppose to be able to see the distance in the upper corner.
The thing is I am not able to refresh the text field, instead it just applies a new layer on top of the old number so it is almost impossible to read.
Here are pictures to demonstrate my problem.
I thought I would be able to solve it by creating the blue rectangle at the start of each loop but that does not seem to solve it.
public void action(){
Random rand = new Random();
boolean race = true;
int x1 =500, y1 = 233;
int x2 = 500, y2 = 333;
int speed1 = rand.nextInt(15) + -16;
int speed2 = rand.nextInt(15) + -16;
int finishline = 30;
Text winnerBlue = new Text("Winner: BLUE",new Font("SansSerif",Font.BOLD,20), Color.blue,Color.white);
Text winnerRed = new Text("Winner: RED",new Font("SansSerif",Font.BOLD,20), Color.red,Color.white);
//background
Text text =null;
Text text2 = null;
window.fillRect(0, 0, 600, 400, Color.GREEN);
//track 1
window.fillRect(20, 330, 550, 39, Color.gray);
//track2
window.fillRect(20, 230, 550, 39, Color.gray);
//Finish line
window.fillRect(40, 210, 10, 180, Color.BLACK);
while(race){
text = new Text(Integer.toString(x1),new Font("Courier",Font.BOLD,20), Color.WHITE);
text2 = new Text(Integer.toString(x2),new Font("SansSerif",Font.BOLD,20), Color.WHITE);
window.fillRect(0, 0, 70, 50, Color.blue);
window.fillRect(70, 0, 70, 50, Color.red);
window.showImage(text, 0, 0);
window.showImage(text2, 70, 0);
window.showImage(car1.getImage(), x1, y1);
window.showImage(car2.getImage(), x2, y2);
car1.moveTo(x1 += speed1, y1);
car2.moveTo(x2 += speed2, y2);
window.pause(50);
if(x1 <= (finishline ) ){
speed1 = 0;
speed2 = 0;
window.showImage(winnerBlue, 200, 200);
race = false;
}
if(x2 <= (finishline)){
speed2 = 0;
speed1 = 0;
window.showImage(winnerRed, 200, 200);
race = false;
}
}
}
}
For the two screen shots and supplied code snippt, it's clear that you don't understand how painting works in Swing/AWT.
Do NOT ever, maintain any kind of refernce to the Graphics context out side of the paintXxx methods.
The paint methods perform a number of very important steps to prepare the Graphics context for painting
Start by taking a look through Performing Custom Painting
I am dragging and dropping the jTable cell from one jTable to another jTable.For Now it is showing me default drag drop icon.
I am using TransferHandler class to implement this.
I Override getDragImage(image) to put my customize image But it is not working.
This way i implemented my code Implementation
I tried this code into this method.
File newFile = new File("./dragImage.jpeg");
Font font = new Font("Tahoma", Font.PLAIN, 11);
FontRenderContext frc = new FontRenderContext(null, true, true);
Rectangle2D bounds = font.getStringBounds(l_value, frc);
int w = (int) bounds.getWidth();
int h = (int) bounds.getHeight();
BufferedImage image = new BufferedImage(10,10, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 10, 10);
g.setColor(Color.BLACK);
g.setFont(font);
g.drawString(l_value, (float) bounds.getX(), (float) -bounds.getY());
g.dispose();
return image;
This code is working in my main method but here in this function it is not working.