use of com.itextpdf.text.pdf.PdfReader in project portal by ixinportal.
the class PDFUtils method readPDF2.
public static ByteArrayOutputStream readPDF2(InputStream is, String enterpriseName, String keySn, String certSn, String mPhone) throws Exception {
// 模版文件目录
PdfReader reader = new PdfReader(is);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper ps = new PdfStamper(reader, bos);
AcroFields s = ps.getAcroFields();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
s.setFieldProperty("enterpriseName", "textfont", bfChinese, null);
s.setFieldProperty("mPhone", "textfont", bfChinese, null);
s.setFieldProperty("keySn", "textfont", bfChinese, null);
s.setFieldProperty("certSn", "textfont", bfChinese, null);
s.setFieldProperty("nowdate", "textfont", bfChinese, null);
s.setField("enterpriseName", enterpriseName);
s.setField("keySn", keySn);
s.setField("certSn", certSn);
s.setField("mPhone", mPhone);
s.setField("nowdate", format.format(new Date()));
// 这句不能少
ps.setFormFlattening(true);
ps.close();
reader.close();
return bos;
}
use of com.itextpdf.text.pdf.PdfReader 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.pdf.PdfReader 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.pdf.PdfReader in project ASCIIGenome by dariober.
the class PdfTest method canAppendToPdf.
@Test
public void canAppendToPdf() throws IOException, DocumentException, InvalidColourException {
File tmp = new File("append.pdf");
tmp.delete();
tmp.deleteOnExit();
String ansiInput = FileUtils.readFileToString(new File("test_data/ansicolor.txt"));
Pdf pdf = new Pdf(ansiInput);
// Append to file not yet created:
pdf.convert(tmp, 10, true);
PdfReader p = new PdfReader(tmp.getAbsolutePath());
assertEquals(1, p.getNumberOfPages());
// Append another file
pdf.convert(tmp, 10, true);
p = new PdfReader(tmp.getAbsolutePath());
// We got two pages right?
assertEquals(2, p.getNumberOfPages());
}
use of com.itextpdf.text.pdf.PdfReader in project ASCIIGenome by dariober.
the class PdfTest method canPrintPdfFromAnsiFile.
@Test
public void canPrintPdfFromAnsiFile() throws DocumentException, IOException, InvalidColourException {
String ansiInput = FileUtils.readFileToString(new File("test_data/ansicolor.txt"));
File tmp = new File("test_data/deleteme.pdf");
tmp.deleteOnExit();
Pdf pdf = new Pdf(ansiInput);
pdf.convert(tmp, 10, false);
// Check file size is about right.
assertTrue(tmp.length() > 1000);
PdfReader p = new PdfReader(tmp.getAbsolutePath());
assertEquals(1, p.getNumberOfPages());
}
Aggregations