List of usage examples for com.itextpdf.text.pdf PdfContentByte addTemplate
public void addTemplate(final PdfTemplate template, final double x, final double y)
From source file:eyeofthetiger.utils.PDFDossardGenerator.java
public void createPdf(List<Participant> participants, OutputStream out) throws IOException, DocumentException { sortParticipants(participants);//from ww w . j av a 2 s .c om Document document = new Document(PageSize.A4.rotate()); float margin = CentimeterToUserSpace(marginCm); document.setMargins(margin, margin, margin, margin); PdfWriter writer = PdfWriter.getInstance(document, out); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfReader pdfBackgroundReader = null; PdfImportedPage backgroundPage = null; if (pdfBackground != null && (new File(pdfBackground)).exists() && (new File(pdfBackground)).isFile()) { pdfBackgroundReader = new PdfReader(pdfBackground); backgroundPage = writer.getImportedPage(pdfBackgroundReader, 1); } float documentTop = document.top(); float documentBottom = document.bottom(); float documentHeight = documentTop - documentBottom; float left = document.left(); float right = document.right(); float width = right - left; float height = documentTop - documentBottom; //cb.rectangle(left, documentBottom, width, documentHeight); //cb.stroke(); boolean logoLeftExist = (new File(logoLeft)).exists() && (new File(logoLeft)).isFile(); boolean logoRightExist = (new File(logoRight)).exists() && (new File(logoRight)).isFile(); float imgLeftRight = left; float imgLeftBottom = documentTop; float imgRightLeft = right; float imgRighBottom = documentTop; Image imgLeft = null; Image imgRight = null; if (exportLogos) { if (logoLeftExist) { imgLeft = Image.getInstance(logoLeft); float h = imgLeft.getHeight(); float w = imgLeft.getWidth(); float nw = width * logoLeftWidth; float nh = (h / w) * nw; imgLeft.scaleAbsolute(nw, nh); //img.scaleAbsoluteHeight(img.getScaledWidth() / xyRatio); imgLeft.setAbsolutePosition(left, documentTop - imgLeft.getScaledHeight()); //cb.addImage(img); imgLeftRight = imgLeft.getAbsoluteX() + imgLeft.getScaledWidth(); imgLeftBottom = imgLeft.getAbsoluteY(); } if (logoRightExist) { imgRight = Image.getInstance(logoRight); float h = imgRight.getHeight(); float w = imgRight.getWidth(); float nw = width * logoRightWidth; float nh = (h / w) * nw; imgRight.scaleAbsolute(nw, nh); imgRight.setAbsolutePosition(right - imgRight.getScaledWidth(), documentTop - imgRight.getScaledHeight()); //cb.addImage(imgRight); imgRightLeft = imgRight.getAbsoluteX(); imgRighBottom = imgRight.getAbsoluteY(); } } float nameHeightPercent = 0.35f; float groupHeightPercent = 0.25f; float nameTop = documentTop; float nameBottom = nameTop; if (exportName) { nameBottom = nameTop - (documentHeight * nameHeightPercent); } float groupeTop = nameBottom; float groupeBottom = nameBottom; if (exportGroup) { groupeBottom = groupeTop - (documentHeight * groupHeightPercent); } float barcodeTop = groupeBottom; float barcodeBottom = documentBottom; ColumnText columnText; for (Participant participant : participants) { if (backgroundPage != null) { //cb.addTemplate(backgroundPage, 1f, 0, 0, 1, 0, 0); //TODO cb.addTemplate(backgroundPage, 0, 0); } float nameFontSize = 65f; float groupFontSize = 45f; float renseignementFontSize = 35f; if (imgLeft != null) { cb.addImage(imgLeft); } if (imgRight != null) { cb.addImage(imgRight); } if (exportName) { columnText = new ColumnText(cb); columnText.setAlignment(Rectangle.ALIGN_CENTER); if (imgLeftRight != -1 && imgLeftBottom != -1) { float[] leftBorder = null; if (imgLeftBottom < nameBottom) { leftBorder = new float[] { imgLeftRight, nameTop, imgLeftRight, nameBottom, left, nameBottom }; } else { leftBorder = new float[] { imgLeftRight, nameTop, imgLeftRight, imgLeftBottom, left, imgLeftBottom, left, nameBottom }; } float[] rightBorder = null; if (imgRighBottom < nameBottom) { rightBorder = new float[] { imgRightLeft, nameTop, imgRightLeft, nameBottom, right, nameBottom }; } else { rightBorder = new float[] { imgRightLeft, nameTop, imgRightLeft, imgRighBottom, right, imgRighBottom, right, nameBottom }; } columnText.setColumns(leftBorder, rightBorder); } else { columnText.setSimpleColumn(left, nameTop, right, nameBottom); } //cb.rectangle(left, nameBottom, width, (nameTop - nameBottom)); //cb.stroke(); columnText.setExtraParagraphSpace(0f); columnText.setAdjustFirstLine(false); columnText.setIndent(0); String txt = participant.getNom().toUpperCase() + " " + participant.getPrenom(); float previousPos = columnText.getYLine(); columnText.setLeading(nameFontSize); columnText.setText(createCleanPhrase(txt, nameFontSize, true)); while (nameFontSize > 1 && ColumnText.hasMoreText(columnText.go(true))) { nameFontSize = nameFontSize - 0.5f; columnText.setLeading(nameFontSize); columnText.setText(createCleanPhrase(txt, nameFontSize, true)); columnText.setYLine(previousPos); } columnText.setLeading(nameFontSize); columnText.setText(createCleanPhrase(txt, nameFontSize, true)); columnText.setYLine(previousPos); columnText.go(false); } if (exportGroup) { columnText = new ColumnText(cb); columnText.setAlignment(Rectangle.ALIGN_CENTER); columnText.setSimpleColumn(document.left(), groupeTop, document.right(), groupeBottom); float groupeHeight = groupeTop - groupeBottom; //cb.rectangle(document.left(), groupeTop - groupeHeight, document.right() - document.left(), groupeHeight); //cb.stroke(); columnText.setExtraParagraphSpace(0f); columnText.setAdjustFirstLine(false); columnText.setIndent(0); columnText.setFollowingIndent(0); String txt1 = participant.getGroupe(); String txt2 = exportRenseignement ? "\n" + participant.getRenseignements() : null; float previousPos = columnText.getYLine(); columnText.setText(null); columnText.setLeading(groupFontSize); columnText.addText(createCleanPhrase(txt1, groupFontSize, true)); columnText.addText(createCleanPhrase(txt2, renseignementFontSize, false)); while (groupFontSize > 1 && ColumnText.hasMoreText(columnText.go(true))) { groupFontSize = groupFontSize - 0.5f; renseignementFontSize = renseignementFontSize - 0.5f; columnText.setText(null); columnText.setLeading(groupFontSize); columnText.addText(createCleanPhrase(txt1, groupFontSize, true)); columnText.addText(createCleanPhrase(txt2, renseignementFontSize, false)); columnText.setYLine(previousPos); } columnText.setText(null); columnText.setLeading(groupFontSize); columnText.addText(createCleanPhrase(txt1, groupFontSize, true)); columnText.addText(createCleanPhrase(txt2, renseignementFontSize, false)); columnText.setYLine(previousPos); columnText.go(false); } { columnText = new ColumnText(cb); float topMargin = 12f; columnText.setSimpleColumn(left, barcodeTop - topMargin, right, barcodeBottom); float barcodeHeight = (barcodeTop - topMargin) - barcodeBottom; //cb.rectangle(left, barcodeTop - barcodeHeight, width, barcodeHeight); //cb.stroke(); columnText.setExtraParagraphSpace(0f); columnText.setAdjustFirstLine(false); columnText.setIndent(0); float previousPos = columnText.getYLine(); columnText.setText(null); columnText.addElement(createCleanBarcode(cb, participant.getNumero(), width, barcodeHeight)); columnText.go(false); } document.newPage(); } document.close(); if (pdfBackgroundReader != null) { pdfBackgroundReader.close(); } }
From source file:figtree.application.FigTreeFrame.java
License:Open Source License
public final void doExportPDF() { FileDialog dialog = new FileDialog(this, "Export PDF Image...", FileDialog.SAVE); dialog.setVisible(true);//from ww w .j a va 2s . c o m if (dialog.getFile() != null) { File file = new File(dialog.getDirectory(), dialog.getFile()); Rectangle2D bounds = treeViewer.getContentPane().getBounds(); Document document = new Document( new com.itextpdf.text.Rectangle((float) bounds.getWidth(), (float) bounds.getHeight())); try { // step 2 PdfWriter writer; writer = PdfWriter.getInstance(document, new FileOutputStream(file)); // step 3 document.open(); // step 4 PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate((float) bounds.getWidth(), (float) bounds.getHeight()); Graphics2D g2d = tp.createGraphics((float) bounds.getWidth(), (float) bounds.getHeight(), new DefaultFontMapper()); treeViewer.getContentPane().print(g2d); g2d.dispose(); cb.addTemplate(tp, 0, 0); } catch (DocumentException de) { JOptionPane.showMessageDialog(this, "Error writing PDF file: " + de, "Export PDF Error", JOptionPane.ERROR_MESSAGE); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(this, "Error writing PDF file: " + e, "Export PDF Error", JOptionPane.ERROR_MESSAGE); } document.close(); } }
From source file:figtree.application.FigTreePDF.java
License:Open Source License
static public void createGraphic(int width, int height, String treeFileName, String graphicFileName) { try {//ww w .jav a2 s . c o m BufferedReader bufferedReader = new BufferedReader(new FileReader(treeFileName)); String line = bufferedReader.readLine(); while (line != null && line.length() == 0) { line = bufferedReader.readLine(); } bufferedReader.close(); boolean isNexus = (line != null && line.toUpperCase().contains("#NEXUS")); Reader reader = new FileReader(treeFileName); Map<String, Object> settings = new HashMap<String, Object>(); ExtendedTreeViewer treeViewer = new ExtendedTreeViewer(); ControlPalette controlPalette = new BasicControlPalette(200, BasicControlPalette.DisplayMode.ONLY_ONE_OPEN); FigTreePanel figTreePanel = new FigTreePanel(null, treeViewer, controlPalette); // First of all, fully populate the settings map so that // all the settings have defaults controlPalette.getSettings(settings); List<Tree> trees = new ArrayList<Tree>(); if (isNexus) { FigTreeNexusImporter importer = new FigTreeNexusImporter(reader); trees.add(importer.importNextTree()); // Try to find a figtree block and if found, parse the settings while (true) { try { importer.findNextBlock(); if (importer.getNextBlockName().equalsIgnoreCase("FIGTREE")) { importer.parseFigTreeBlock(settings); } } catch (EOFException ex) { break; } } } else { NewickImporter importer = new NewickImporter(reader, true); trees.add(importer.importNextTree()); } if (trees.size() == 0) { throw new ImportException("This file contained no trees."); } treeViewer.setTrees(trees); controlPalette.setSettings(settings); treeViewer.getContentPane().setSize(width, height); OutputStream stream; if (graphicFileName != null) { stream = new FileOutputStream(graphicFileName); } else { stream = System.out; } Document document = new Document(); document.setPageSize(new com.itextpdf.text.Rectangle(width, height)); try { PdfWriter writer = PdfWriter.getInstance(document, stream); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2 = tp.createGraphics(width, height); tp.setWidth(width); tp.setHeight(height); treeViewer.getContentPane().print(g2); g2.dispose(); tp.sanityCheck(); // all the g2 content is written to tp, not cb cb.addTemplate(tp, 0, 0); cb.sanityCheck(); } catch (Exception e) { System.err.println(e.getMessage()); } document.close(); } catch (ImportException ie) { throw new RuntimeException("Error writing graphic file: " + ie); } catch (IOException ioe) { throw new RuntimeException("Error writing graphic file: " + ioe); } }
From source file:fll.util.SimpleFooterHandler.java
License:Open Source License
@Override public void onEndPage(final PdfWriter writer, final Document document) { final PdfContentByte cb = writer.getDirectContent(); cb.saveState();/*from w w w. j a v a 2 s .c o m*/ // compose the footer final String text = "Page " + writer.getPageNumber() + " of "; final float textSize = _headerFooterFont.getWidthPoint(text, 12); final float textBase = document.bottom() - 20; cb.beginText(); cb.setFontAndSize(_headerFooterFont, 12); final float adjust = _headerFooterFont.getWidthPoint("0", 12); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); cb.endText(); cb.addTemplate(_tpl, document.right() - adjust, textBase); cb.restoreState(); }
From source file:GeMSE.Popups.PopupMenuType.java
License:Open Source License
private void SaveAsPDF(java.awt.event.ActionEvent e, File file) { Document document = new Document(new Rectangle(_component.getSize().width, _component.getSize().height)); try {//from w ww . j a v a2s .c o m PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(_component.getSize().width, _component.getSize().height); Graphics graphics = new PdfGraphics2D(template, _component.getSize().width, _component.getSize().height); _component.print(graphics); graphics.dispose(); contentByte.addTemplate(template, 0, 0); } catch (DocumentException | FileNotFoundException e2) { } finally { if (document.isOpen()) document.close(); } }
From source file:GeMSE.Visualization.Graph.GraphVis.java
License:Open Source License
private void SaveAsPDF(File file) { Document document = new Document(new Rectangle(graphPanel.getSize().width, graphPanel.getSize().height)); try {/*from w w w. j a v a 2 s. c o m*/ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(graphPanel.getSize().width, graphPanel.getSize().height); Graphics graphics = new PdfGraphics2D(template, graphPanel.getSize().width, graphPanel.getSize().height); graphPanel.print(graphics); graphics.dispose(); contentByte.addTemplate(template, 0, 0); } catch (DocumentException | FileNotFoundException e2) { } finally { if (document.isOpen()) document.close(); } }
From source file:gravabncertificado.GerandoArquivoCarimbado_1.java
public static void GerandoArquivoCarimbadoPDF(String caminhoarquivo, String BN) throws InvalidPdfException { //Copiando arquivo informado. try {//from ww w . j a v a 2 s .c o m // Adicionado parametro para nao retornar erro quando o documento for protegido. PdfReader.unethicalreading = true; //Cria o reader para o primeiro PDF PdfReader reader = new PdfReader(caminhoarquivo); // n recebe o numero total de paginas int n = reader.getNumberOfPages(); //Tamanho da primeira Pagina ; //Cria Segundo PDF Document document = new Document(PageSize.A4, 36, 36, 36, 36); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-C.pdf")); document.open(); // Adiciona conteudo ao PDF Carimbado. PdfContentByte cb = writer.getDirectContent(); int i = 0; int p = 0; String caminhodestino = (caminhoarquivo.substring(0, caminhoarquivo.length() - BN.length())); // DESABILITADO PORQUE O MOVER NAO ESTAVA FUNCIONANDO. File destinooriginal = new File(caminhodestino + "ORIGINAL\\"); if (!destinooriginal.exists()) { destinooriginal.mkdir(); } caminhodestino = (caminhodestino + "ORIGINAL\\" + BN); //TESTANDO NOVA FORMA DE COPIAR File origem = new File(caminhoarquivo); File destino = new File(caminhodestino); FileInputStream fis = new FileInputStream(origem); FileOutputStream fos = new FileOutputStream(destino); FileChannel inChannel = fis.getChannel(); FileChannel outChannel = fos.getChannel(); long transferFrom = outChannel.transferFrom(inChannel, 0, inChannel.size()); fis.close(); fos.close(); inChannel.close(); outChannel.close(); // Thread.sleep(10); BN = BN.substring(0, BN.length() - 4); while (i < n) { document.newPage(); p++; i++; PdfContentByte under = writer.getDirectContentUnder(); PdfImportedPage page1 = writer.getImportedPage(reader, i); cb.addTemplate(page1, 0, i * 0.2f); // Page 1: a rectangle /* RETANGULO DESATIVADO drawRectangle(under, 100, 50); under.setRGBColorFill(255, 220, 220); under.rectangle(width /50, 5, 118, 40); under.fill(); */ //CARIMBO DA BN BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(bf, 14); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 44, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 32, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 22, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " NR", width / 6, 28, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " " + BN, width / 6, 16, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 12, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 14, 0); cb.endText(); } document.close(); writer.close(); reader.close(); origem.delete(); /* PdfContentByte under = writer.getDirectContentUnder(); // Page 1: a rectangle drawRectangle(under, 20, 20); under.setRGBColorFill(0xFF, 0xD7, 0x00); under.rectangle(5, 5, 15, 15); under.fill(); */ //document.newPage(); //COPIANDO ARQUIVO CARIMBADO /* DESABILITADO PORQUE NAO ESTAVA FUNCIONANDO. boolean bool; //caminhoarquivo = caminhoarquivo.replace("\\", "\\\\"); //caminhodestino = caminhodestino.replace("\\", "\\\\"); File origem = new File(caminhoarquivo); File destino = new File(caminhodestino); bool = origem.renameTo(destino); System.out.println(origem); System.out.println(caminhodestino); */ } catch (IOException | DocumentException ex) { } }
From source file:gravabncertificado007.CarimboCertificado.java
public void aplicaCarimboBin(String BN, String caminhoarquivo) throws DocumentException, IOException, RuntimeException { PdfReader.unethicalreading = true;//from w w w.ja v a 2s . co m //Cria o reader para o primeiro PDF PdfReader reader = new PdfReader(caminhoarquivo); Rectangle psize = reader.getPageSize(1); float width = psize.getWidth(); float height = psize.getHeight(); Document document = new Document(new Rectangle(width, height)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-C.pdf")); document.open(); int i = 0; BN = BN.substring(BN.length() - 13, BN.length() - 4); PdfContentByte cb = writer.getDirectContent(); while (i < reader.getNumberOfPages()) { i++; document.newPage(); PdfContentByte under = writer.getDirectContentUnder(); PdfImportedPage page1 = writer.getImportedPage(reader, i); cb.addTemplate(page1, 0, i * 0.2f); //CARIMBO DA BN BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(bf, 14); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 44, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 32, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 22, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " NR", width / 6, 28, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " " + BN, width / 6, 16, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 12, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 14, 0); cb.endText(); } document.close(); writer.close(); reader.close(); }
From source file:gravabncertificado007.CarimboCertificado.java
public void aplicaCariboGedi(String BN, String caminhoarquivo) throws DocumentException, IOException, RuntimeException { PdfReader.unethicalreading = true;//from w w w . j ava 2 s .com //Cria o reader para o primeiro PDF PdfReader reader = new PdfReader(caminhoarquivo); Rectangle psize = reader.getPageSize(1); float width = psize.getWidth(); float height = psize.getHeight(); Document document = new Document(new Rectangle(width, height)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-G.pdf")); document.open(); int i = 0; BN = BN.substring(BN.length() - 13, BN.length() - 4); PdfContentByte cb = writer.getDirectContent(); while (i < reader.getNumberOfPages()) { i++; document.newPage(); PdfContentByte under = writer.getDirectContentUnder(); PdfImportedPage page1 = writer.getImportedPage(reader, i); cb.addTemplate(page1, 0, i * 0.2f); BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(bf, 14); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " _________________ ", width / 6, 44, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 32, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 22, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |Copia Controlada |", width / 6, 28, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 16, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 12, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |_________________|", width / 6, 14, 0); cb.endText(); } document.close(); writer.close(); reader.close(); }
From source file:info.sarihh.unimodeling.gui.DynamicBPEstimateFrame.java
public static void writeChartAsPDF(OutputStream out, JFreeChart chart, int width, int height, FontMapper mapper) throws IOException { Rectangle pagesize = new Rectangle(width, height); Document document = new Document(pagesize, 50, 50, 50, 50); try {/* w ww .j a va 2 s. c o m*/ PdfWriter writer = PdfWriter.getInstance(document, out); document.addAuthor("JFreeChart"); document.addSubject("Demonstration"); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2 = tp.createGraphics(width, height, mapper); Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2, r2D, null); g2.dispose(); cb.addTemplate(tp, 0, 0); } catch (DocumentException de) { System.err.println(de.getMessage()); } document.close(); }