下面列出了javax.swing.JComponent#print ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void writeToSVG(JComponent panel, File fileName) throws IOException {
// print the panel to pdf
int width = panel.getWidth();
int height = panel.getWidth();
logger.info(
() -> MessageFormat.format("Exporting panel to SVG file (width x height; {0} x {1}): {2}",
width, height, fileName.getAbsolutePath()));
// Get a DOMImplementation
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null);
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
svgGenerator.setSVGCanvasSize(new Dimension(width, height));
panel.print(svgGenerator);
boolean useCSS = true; // we want to use CSS style attribute
try (Writer out = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8")) {
svgGenerator.stream(out, useCSS);
}
}
public static void writeToSVG(JComponent panel, File fileName) throws IOException {
// print the panel to pdf
int width = panel.getWidth();
int height = panel.getWidth();
logger.info(
() -> MessageFormat.format("Exporting panel to SVG file (width x height; {0} x {1}): {2}",
width, height, fileName.getAbsolutePath()));
// Get a DOMImplementation
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null);
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
svgGenerator.setSVGCanvasSize(new Dimension(width, height));
panel.print(svgGenerator);
boolean useCSS = true; // we want to use CSS style attribute
try (Writer out = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8")) {
svgGenerator.stream(out, useCSS);
}
}
/**
* Writes swing to pdf
*
* @param panel
* @param fileName
* @throws DocumentException
* @throws Exception
*/
public static void writeToPDF(JComponent panel, File fileName)
throws IOException, DocumentException {
// print the panel to pdf
int width = panel.getWidth();
int height = panel.getHeight();
logger.info(
() -> MessageFormat.format("Exporting panel to PDF file (width x height; {0} x {1}): {2}",
width, height, fileName.getAbsolutePath()));
Document document = new Document(new Rectangle(width, height));
PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D g2 = new PdfGraphics2D(contentByte, width, height, new DefaultFontMapper());
panel.print(g2);
g2.dispose();
contentByte.addTemplate(template, 0, 0);
document.close();
writer.close();
} finally {
if (document.isOpen()) {
document.close();
}
}
}
/**
* Writes swing to EMF
*
* @param panel
* @param fileName
* @throws Exception
*/
public static void writeToEMF(JComponent panel, File fileName) throws IOException {
// print the panel to pdf
int width = panel.getWidth();
int height = panel.getWidth();
logger.info(
() -> MessageFormat.format("Exporting panel to EMF file (width x height; {0} x {1}): {2}",
width, height, fileName.getAbsolutePath()));
VectorGraphics g = new EMFGraphics2D(fileName, new Dimension(width, height));
g.startExport();
panel.print(g);
g.endExport();
}
@Override
public void print(Graphics g) {
for (JComponent component : myComponents) {
component.print(g);
// g.setColor(java.awt.Color.green);
// g.drawRect(0, 0, getWidth(component), getHeight(component));
g.translate(getWidth(component), 0);
}
}
/**
* Writes swing to pdf
*
* @param panel
* @param fileName
* @throws DocumentException
* @throws Exception
*/
public static void writeToPDF(JComponent panel, File fileName)
throws IOException, DocumentException {
// print the panel to pdf
int width = panel.getWidth();
int height = panel.getHeight();
logger.info(
() -> MessageFormat.format("Exporting panel to PDF file (width x height; {0} x {1}): {2}",
width, height, fileName.getAbsolutePath()));
Document document = new Document(new Rectangle(width, height));
PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D g2 = new PdfGraphics2D(contentByte, width, height, new DefaultFontMapper());
panel.print(g2);
g2.dispose();
contentByte.addTemplate(template, 0, 0);
document.close();
writer.close();
} finally {
if (document.isOpen()) {
document.close();
}
}
}
/**
* Writes swing to EMF
*
* @param panel
* @param fileName
* @throws Exception
*/
public static void writeToEMF(JComponent panel, File fileName) throws IOException {
// print the panel to pdf
int width = panel.getWidth();
int height = panel.getWidth();
logger.info(
() -> MessageFormat.format("Exporting panel to EMF file (width x height; {0} x {1}): {2}",
width, height, fileName.getAbsolutePath()));
VectorGraphics g = new EMFGraphics2D(fileName, new Dimension(width, height));
g.startExport();
panel.print(g);
g.endExport();
}
void printComponent(JComponent c, TestBufferedImage i) {
Graphics g = i.getGraphics();
g.setColor(c.getBackground());
g.fillRect(0, 0, i.getWidth(), i.getHeight());
c.print(g);
}
void printComponent(JComponent c, TestBufferedImage i) {
Graphics g = i.getGraphics();
g.setColor(c.getBackground());
g.fillRect(0, 0, i.getWidth(), i.getHeight());
c.print(g);
}
void printComponent(JComponent c, TestBufferedImage i) {
Graphics g = i.getGraphics();
g.setColor(c.getBackground());
g.fillRect(0, 0, i.getWidth(), i.getHeight());
c.print(g);
}
void printComponent(JComponent c, TestBufferedImage i) {
Graphics g = i.getGraphics();
g.setColor(c.getBackground());
g.fillRect(0, 0, i.getWidth(), i.getHeight());
c.print(g);
}
void printComponent(JComponent c, TestBufferedImage i) {
Graphics g = i.getGraphics();
g.setColor(c.getBackground());
g.fillRect(0, 0, i.getWidth(), i.getHeight());
c.print(g);
}
void printComponent(JComponent c, TestBufferedImage i) {
Graphics g = i.getGraphics();
g.setColor(c.getBackground());
g.fillRect(0, 0, i.getWidth(), i.getHeight());
c.print(g);
}
void printComponent(JComponent c, TestBufferedImage i) {
Graphics g = i.getGraphics();
g.setColor(c.getBackground());
g.fillRect(0, 0, i.getWidth(), i.getHeight());
c.print(g);
}
void printComponent(JComponent c, TestBufferedImage i) {
Graphics g = i.getGraphics();
g.setColor(c.getBackground());
g.fillRect(0, 0, i.getWidth(), i.getHeight());
c.print(g);
}
void printComponent(JComponent c, TestBufferedImage i) {
Graphics g = i.getGraphics();
g.setColor(c.getBackground());
g.fillRect(0, 0, i.getWidth(), i.getHeight());
c.print(g);
}
void printComponent(JComponent c, TestBufferedImage i) {
Graphics g = i.getGraphics();
g.setColor(c.getBackground());
g.fillRect(0, 0, i.getWidth(), i.getHeight());
c.print(g);
}
void printComponent(JComponent c, TestBufferedImage i) {
Graphics g = i.getGraphics();
g.setColor(c.getBackground());
g.fillRect(0, 0, i.getWidth(), i.getHeight());
c.print(g);
}
void printComponent(JComponent c, TestBufferedImage i) {
Graphics g = i.getGraphics();
g.setColor(c.getBackground());
g.fillRect(0, 0, i.getWidth(), i.getHeight());
c.print(g);
}
void printComponent(JComponent c, TestBufferedImage i) {
Graphics g = i.getGraphics();
g.setColor(c.getBackground());
g.fillRect(0, 0, i.getWidth(), i.getHeight());
c.print(g);
}