use of com.itextpdf.awt.DefaultFontMapper in project clusterMaker2 by RBVI.
the class GraphicsExportPanel method pdfSave.
private void pdfSave(String format) {
com.itextpdf.text.Rectangle pageSize = PageSize.LETTER;
Document document = new Document(pageSize);
try {
OutputStream output = new BufferedOutputStream(new FileOutputStream(getFile()));
PdfWriter writer = PdfWriter.getInstance(document, output);
document.open();
PdfContentByte cb = writer.getDirectContent();
Graphics2D g = cb.createGraphics(pageSize.getWidth(), pageSize.getHeight(), new DefaultFontMapper());
double imageScale = Math.min(pageSize.getWidth() / ((double) estimateWidth() + getBorderPixels()), pageSize.getHeight() / ((double) estimateHeight() + getBorderPixels()));
g.scale(imageScale, imageScale);
drawAll(g, 1.0);
g.dispose();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, new JTextArea("Dendrogram export had problem " + e));
// logger.error("Exception " + e);
// e.printStackTrace();
}
document.close();
}
use of com.itextpdf.awt.DefaultFontMapper in project propane by ruby-processing.
the class PGraphicsPDF method getMapper.
protected static DefaultFontMapper getMapper() {
if (mapper == null) {
mapper = new DefaultFontMapper();
switch(PApplet.platform) {
case PConstants.MACOS:
try {
String homeLibraryFonts = System.getProperty("user.home") + "/Library/Fonts";
mapper.insertDirectory(homeLibraryFonts);
} catch (Exception e) {
// might be a security issue with getProperty() and user.home
// if this sketch is running from the web
}
// add the system font paths
mapper.insertDirectory("/System/Library/Fonts");
mapper.insertDirectory("/Library/Fonts");
break;
case PConstants.WINDOWS:
// how to get the windows fonts directory?
// could be c:\winnt\fonts or c:\windows\fonts or not even c:
// maybe do a Runtime.exec() on echo %WINDIR% ?
// Runtime.exec solution might be a mess on systems where the
// the backslash/colon characters not really used (i.e. JP)
// find the windows fonts folder
File[] roots = File.listRoots();
for (File root : roots) {
if (root.toString().startsWith("A:")) {
// If not, will need to use the other fileExists() code below.
continue;
}
File folder = new File(root, "WINDOWS/Fonts");
if (folder.exists()) {
mapper.insertDirectory(folder.getAbsolutePath());
break;
}
folder = new File(root, "WINNT/Fonts");
if (folder.exists()) {
mapper.insertDirectory(folder.getAbsolutePath());
break;
}
}
break;
case PConstants.LINUX:
checkDir("/usr/share/fonts/", mapper);
checkDir("/usr/local/share/fonts/", mapper);
checkDir(System.getProperty("user.home") + "/.fonts", mapper);
break;
default:
break;
}
}
return mapper;
}
use of com.itextpdf.awt.DefaultFontMapper in project mzmine2 by mzmine.
the class ChartExportUtil method writeChartToPDF.
/**
* This method saves a chart as a PDF with given dimensions
*
* @param chart
* @param width
* @param height
* @param fileName is a full path
*/
public static void writeChartToPDF(JFreeChart chart, int width, int height, File fileName) throws Exception {
PdfWriter writer = null;
Document document = new Document(new Rectangle(width, height));
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height);
chart.draw(graphics2d, rectangle2d);
graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
document.close();
}
}
use of com.itextpdf.awt.DefaultFontMapper in project mzmine2 by mzmine.
the class SwingExportUtil method writeToPDF.
/**
* 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();
}
}
}
Aggregations