use of com.itextpdf.text.Document in project MtgDesktopCompanion by nicho92.
the class PDFExport method export.
@Override
public void export(MagicDeck deck, File f) throws IOException {
PdfPTable table = new PdfPTable(3);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
try {
document = new Document(PageSize.A4, 5, 5, 10, 5);
document.addAuthor(getString("AUTHOR"));
document.addCreationDate();
document.addCreator("Magic Desktop Companion");
document.addTitle(deck.getName());
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(f));
document.open();
int i = 0;
for (MagicCard card : deck.getAsList()) {
table.addCell(getCells(card));
setChanged();
notifyObservers(i++);
}
document.add(table);
document.close();
writer.close();
} catch (Exception e) {
logger.error("Error in pdf creation " + f, e);
}
}
use of com.itextpdf.text.Document 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