use of com.itextpdf.text.Document in project MtgDesktopCompanion by nicho92.
the class DCIDeckSheetExport method export.
@Override
public void export(MagicDeck deck, File dest) throws IOException {
PdfReader reader = new PdfReader(new URL(getString("PDF_URL")));
Document document = new Document(reader.getPageSize(1));
PdfWriter writer;
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
} catch (DocumentException e) {
throw new IOException(e.getMessage());
}
document.open();
PdfContentByte cb = writer.getDirectContent();
// copy first page to new pdf file
PdfImportedPage page = writer.getImportedPage(reader, 1);
document.newPage();
cb.addTemplate(page, 0, 0);
Font helvetica = new Font(FontFamily.HELVETICA, 12);
BaseFont bfHelv = helvetica.getCalculatedBaseFont(false);
cb.beginText();
cb.setFontAndSize(bfHelv, 11);
// HEADER
cb.setTextMatrix(page.getWidth() - 51f, page.getHeight() - 49);
cb.showText(getString("LAST_NAME").substring(0, 1).toUpperCase());
cb.setTextMatrix(page.getWidth() / 3.2f, page.getHeight() - 73);
if (!getString("FORCED_DATE").equalsIgnoreCase(""))
cb.showText(getString("FORCED_DATE"));
else
cb.showText(new SimpleDateFormat(getString("DATE_FORMAT")).format(new Date()));
cb.setTextMatrix(page.getWidth() / 1.48f, page.getHeight() - 73);
cb.showText(getString("EVENT_NAME"));
cb.setTextMatrix(page.getWidth() / 3.2f, page.getHeight() - 96);
cb.showText(getString("LOCATION"));
cb.setTextMatrix(page.getWidth() / 1.48f, page.getHeight() - 96);
cb.showText(deck.getName());
cb.setTextMatrix(page.getWidth() / 1.48f, page.getHeight() - 119);
if (getString("DECK_DESIGNER").equals(""))
cb.showText(getString("LAST_NAME") + " " + getString("FIRST_NAME"));
else
cb.showText(getString("DECK_DESIGNER"));
// MAIN DECK
int count = 0;
for (MagicCard mc : deck.getMap().keySet()) {
cb.setTextMatrix(page.getWidth() / 6.4f, page.getHeight() - 185 - count);
cb.showText(deck.getMap().get(mc) + space + mc.getName());
count += 18;
}
// CONTINUED and BASIC LAND
if (getString("FILL_CONTINUED_LANDS").equalsIgnoreCase("true")) {
count = 0;
for (MagicCard mc : deck.getMap().keySet()) {
if (mc.getTypes().contains("Land")) {
cb.setTextMatrix(page.getWidth() / 1.7f, page.getHeight() - 185 - count);
cb.showText(deck.getMap().get(mc) + space + mc.getName());
count += 18;
}
}
}
// SIDEBOARD
count = 0;
for (MagicCard mc : deck.getMapSideBoard().keySet()) {
cb.setTextMatrix(page.getWidth() / 1.7f, page.getHeight() - 418 - count);
cb.showText(deck.getMapSideBoard().get(mc) + space + mc.getName());
count += 18;
}
// BOTTOM card count
cb.setTextMatrix((page.getWidth() / 2f) - 30, 45);
cb.showText(String.valueOf(deck.getAsList().size()));
cb.setTextMatrix(page.getWidth() - 70, 100);
cb.showText(String.valueOf(deck.getSideAsList().size()));
// LEFT TEXT
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, getString("LAST_NAME"), 52, 90, 90);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, getString("FIRST_NAME"), 52, 295, 90);
String dci = getString("DCI_NUMBER");
count = 0;
for (int i = 0; i < dci.length(); i++) {
char c = dci.charAt(i);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, String.valueOf(c), 52, (428 + count), 90);
count += 22;
}
cb.endText();
document.close();
}
use of com.itextpdf.text.Document in project ASCIIGenome by dariober.
the class Pdf method appendPdf.
/**
* Append pdf file `in` to pdf file `dest`
* @throws IOException
* @throws DocumentException
*/
private void appendPdf(File in, File dest) throws IOException, DocumentException {
if (!dest.exists()) {
Files.move(Paths.get(in.getAbsolutePath()), Paths.get(dest.getAbsolutePath()));
return;
}
File template = Utils.createTempFile(".template.", ".pdf");
;
template.deleteOnExit();
Files.copy(Paths.get(dest.getAbsolutePath()), Paths.get(template.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
Document document = new Document();
FileOutputStream outputStream = new FileOutputStream(template);
PdfCopy copy = new PdfSmartCopy(document, outputStream);
document.open();
PdfReader reader0 = new PdfReader(dest.getAbsolutePath());
copy.addDocument(reader0);
reader0.close();
PdfReader reader = new PdfReader(in.getAbsolutePath());
copy.addDocument(reader);
reader.close();
document.close();
Files.move(Paths.get(template.getAbsolutePath()), Paths.get(dest.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
}
use of com.itextpdf.text.Document in project ASCIIGenome by dariober.
the class Pdf method convert.
/* M e t h o d s */
public void convert(File pdfOut, float fontSize, boolean append) throws IOException, DocumentException, InvalidColourException {
// First we write to tmp file then we copy to given destination, possibly appending
File tmpPdf = Utils.createTempFile(pdfOut.getName(), ".pdf");
tmpPdf.deleteOnExit();
List<Paragraph> pdfLines = this.ansiFileToPdfParagraphs(fontSize);
Rectangle pageSize = new Rectangle((float) (this.getMaxWidth() * 1.01), (float) (this.getMaxHeight()));
int background256 = Config.get256Color(ConfigKey.background);
Color pageColor = Xterm256.xterm256ToColor(background256);
pageSize.setBackgroundColor(new BaseColor(pageColor.getRed(), pageColor.getGreen(), pageColor.getBlue()));
Document document = new Document(pageSize, 5f, 0f, 0f, 0f);
// Document document = new Document(new Rectangle((float) (this.getMaxWidth() * 1.01), (float) (this.getMaxHeight())), 5f, 0f, 0f, 0f);
PdfWriter.getInstance(document, new FileOutputStream(tmpPdf));
document.open();
for (Paragraph line : pdfLines) {
document.add(line);
}
document.close();
if (append) {
this.appendPdf(tmpPdf, pdfOut);
} else {
Files.move(Paths.get(tmpPdf.getAbsolutePath()), Paths.get(pdfOut.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
}
}
use of com.itextpdf.text.Document in project Java-Matrix-Benchmark by lessthanoptimal.
the class UtilPlotPdf method saveAsPdf.
public static void saveAsPdf(JFreeChart chart, String FILENAME, int width, int height) {
File parent = new File(new File(FILENAME).getParent());
if (!parent.exists()) {
if (!parent.mkdirs())
throw new RuntimeException("Can't make directory path");
}
Document document = new Document(new Rectangle(width, height));
try {
FileOutputStream file = new FileOutputStream(FILENAME);
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(width, height);
Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
chart.draw(g2d, r2d);
g2d.dispose();
cb.addTemplate(tp, 0, 0);
document.close();
g2d.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.itextpdf.text.Document in project Zong by Xenoage.
the class PdfPrinter method print.
/**
* Prints the given {@link Layout} into the given PDF output stream.
*/
public static void print(Layout layout, OutputStream out) {
Document document = new Document();
PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(document, out);
} catch (Exception e) {
handle(warning(Voc.ErrorWhilePrinting));
}
document.open();
PdfContentByte cb = writer.getDirectContent();
It<Page> pages = it(layout.getPages());
for (Page page : pages) {
// create PDF page
Size2f pageSize = page.getFormat().getSize();
float width = Units.mmToPx(pageSize.width, 1);
float height = Units.mmToPx(pageSize.height, 1);
document.newPage();
PdfTemplate tp = cb.createTemplate(width, height);
// fill PDF page
Graphics2D g2d = new PdfGraphics2D(cb, width, height);
INSTANCE.log(Companion.remark("Printing page " + pages.getIndex() + "..."));
LayoutRenderer.paintToCanvas(layout, pages.getIndex(), new AwtCanvas(g2d, pageSize, CanvasFormat.Vector, CanvasDecoration.None, CanvasIntegrity.Perfect));
// finish page
g2d.dispose();
cb.addTemplate(tp, 0, 0);
}
document.close();
}
Aggregations