List of usage examples for com.itextpdf.text Font Font
public Font(final FontFamily family, final float size)
From source file:de.beimax.talenttree.PageGeneratorSimple.java
License:Open Source License
/** * Create basic header/*from ww w . j ava 2 s . c o m*/ * @throws Exception */ protected void addHeader() throws Exception { Font fontHeader = new Font(generator.getFontBold(), 18); document.add(new Paragraph( getMappedLocalizedString("header") + ": " + getMappedLocalizedString("subheader"), fontHeader)); }
From source file:de.knurt.heinzelmann.util.itext.TextBlock.java
License:Creative Commons License
public TextBlock(float size, BaseFont baseFont, String content) { this(size, baseFont, new Font(baseFont, size), content); }
From source file:englishrusbook.Writer.java
public void write(Document document, HashMap<String, String> wordTranslateMap) { document.open();/* w w w. j av a2s. com*/ String delimiter = " : "; try { BaseFont helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); Font font = new Font(helvetica, 12); for (Entry<String, String> entry : wordTranslateMap.entrySet()) { document.add(new Paragraph(entry.getKey() + delimiter + entry.getValue(), font)); } } catch (DocumentException | IOException e) { System.out.println("ERROR in writing: " + e); } finally { document.close(); } }
From source file:EplanPrinter.PDFPrint.java
License:Open Source License
private void insertPinnedComment(float xCoord, float yCoord, String id, String comment, int pageNum, int masterHeight, int masterWidth) throws DocumentException, IOException { int standardHeight = 38; int standardWidth = 130; int lineHeight = 7; if (fontSize == 8) { standardHeight = 48;//w w w . j a va2 s . c om standardWidth = 180; lineHeight = 10; } else if (fontSize == 10) { standardHeight = 70; standardWidth = 210; lineHeight = 12; } // comment box position defines lower-left corner xCoord += 41; // x-coordinate of comment text box yCoord -= 7; // lower offset moves box up float f = (float) 0.4; PdfGState gs1 = new PdfGState(); gs1.setFillOpacity(f); PdfContentByte fg = pds.getOverContent(pageNum); fg.setGState(gs1); fg.setLineWidth(.5f * getRatio(masterHeight, masterWidth, pageNum)); // Calculate additional needed height for the comment, based on length of comment. List<String> lines = createComment(cleanupComment(comment)); int numLines = lines.size(); int commentHeight = (numLines - 1) * lineHeight; // Draw the comment box String color = "0xF2F3E4"; Color c = Color.decode(color); color = "0xFAFAF4"; Color c2 = Color.decode(color); fg.setLineWidth(1f); fg.setColorStroke(new BaseColor(c.getRGB())); fg.setColorFill(new BaseColor(c2.getRGB())); float newCoords[] = pinnedCoords(xCoord, yCoord, standardWidth, standardHeight + commentHeight); fg.roundRectangle(xCoord, newCoords[1], standardWidth, standardHeight + commentHeight, 2f); fg.fillStroke(); //BaseFont bf = BaseFont.createFont(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, true); BaseFont bfb = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, true); fg.setColorFill(BaseColor.RED); gs1.setFillOpacity(0.8f); fg.setGState(gs1); // Adding comment title text Phrase phrase = new Phrase("Comment", new Font(bfb, fontSize)); ColumnText.showTextAligned(fg, Element.ALIGN_LEFT, phrase, xCoord + 3, yCoord - 7, 0); // Adding comment number phrase = new Phrase(id, new Font(bf, fontSize)); float offset = xCoord + 35; if (fontSize == 8) offset = xCoord + 45; else if (fontSize == 10) offset = xCoord + 55; ColumnText.showTextAligned(fg, Element.ALIGN_LEFT, phrase, offset, yCoord - 7, 0); /* Remove 10/27/2015 Jon Changkachith // Adding 'comment' label text phrase = new Phrase("Comment", new Font(bfb, fontSize)); ColumnText.showTextAligned(fg, Element.ALIGN_LEFT, phrase, xCoord + 3, yCoord - 20f, 0); */ // Adding comment text int commentYOffset = 32; for (int i = 0; i < lines.size(); i++) { //phrase = new Phrase(lines.get(i), new Font(bf, fontSize)); phrase = composePhrase(lines.get(i), bf, bfb); ColumnText.showTextAligned(fg, Element.ALIGN_LEFT, phrase, xCoord + 3, yCoord - commentYOffset, 0); commentYOffset += lineHeight; } }
From source file:EplanPrinter.PDFPrint.java
License:Open Source License
public Phrase composePhrase(String str, BaseFont normalFont, BaseFont boldFont) throws DocumentException, IOException { ArrayList<Chunk> chunks = new ArrayList(); // unescape all HTML encode i.e < = < str = HtmlDecoder.unescapeHTML(str); // replace all <li> with bullet symbol str = Pattern.compile("\\<li*?\\>", Pattern.CASE_INSENSITIVE).matcher(str) .replaceAll(new Character('\u2022') + " "); String words[] = str.split(" "); for (int i = 0; i < words.length; i++) { String noHtml = words[i]; BaseFont font = boldFont;//from www . j av a2 s. co m if (hasHtmlTag(noHtml, REGEX_B)) { noHtml = removeHtmlTag(words[i]); } else if (hasHtmlTag(noHtml, REGEX_I)) { noHtml = removeHtmlTag(words[i]); } else if (hasHtmlTag(noHtml, REGEX_EM)) { noHtml = removeHtmlTag(words[i]); } else if (hasHtmlTag(noHtml, REGEX_STRONG)) { noHtml = removeHtmlTag(words[i]); } else if (hasHtmlTag(noHtml, REGEX_LI)) { noHtml = new Character('\u2022') + " " + removeHtmlTag(words[i]); font = normalFont; } else if (hasHtmlTag(noHtml, REGEX_DT)) { noHtml = new Character('\u2022') + " " + removeHtmlTag(words[i]); font = normalFont; } else if (hasHtmlTag(noHtml, REGEX_DD)) { noHtml = new Character('\u2022') + " " + removeHtmlTag(words[i]); font = normalFont; } else { noHtml = removeHtmlTag(words[i]); font = normalFont; } chunks.add(new Chunk(noHtml + " ", new Font(font, fontSize))); } Phrase phrase = new Phrase(); for (Chunk chunk : chunks) { phrase.add(chunk); } return phrase; }
From source file:es.baudlord.pcpartpicker.controller.BuildViewController.java
License:Open Source License
@FXML public void onBillAction(ActionEvent event) { if (build.isComplete()) { try {// w w w . j ava 2 s .c o m FileChooser chooser = new FileChooser(); chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF", "*.pdf")); File file = chooser.showSaveDialog(((Node) event.getSource()).getScene().getWindow()); if (file == null) return; Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); // Begin header generation PdfPTable table = new PdfPTable(2); table.setWidths(new int[] { 3, 7 }); com.itextpdf.text.Image image = com.itextpdf.text.Image .getInstance(App.class.getResource("img/logo.png")); image.scaleToFit(100f, 100f); PdfPCell cell = new PdfPCell(image); cell.setBorder(0); PdfPCell cell2 = new PdfPCell( new Paragraph("Hipster PC Store", new Font(Font.FontFamily.HELVETICA, 30))); cell2.setBorder(0); cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); table.addCell(cell2); document.add(table); // End header generation document.add(build.createPdfPTable(new Font(Font.FontFamily.COURIER, 7))); // Begin generate date warning Calendar cal = Calendar.getInstance(); Instant now = cal.toInstant(); cal.add(Calendar.DAY_OF_MONTH, 7); Instant end = cal.toInstant(); DateTimeFormatter localFormat = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT) .withZone(ZoneId.systemDefault()); Paragraph p1 = new Paragraph(GENERAL_BUNDLE.getString("bill.date.warning"), new Font(Font.FontFamily.COURIER, 10, Font.BOLD)); Paragraph p2 = new Paragraph( GENERAL_BUNDLE.getString("bill.date.emitted") + " " + localFormat.format(now) + "\n" + GENERAL_BUNDLE.getString("bill.date.valid") + " " + localFormat.format(end), new Font(Font.FontFamily.COURIER, 10)); p1.setAlignment(Element.ALIGN_CENTER); p2.setAlignment(Element.ALIGN_CENTER); document.add(p1); document.add(p2); // End generate date warning document.close(); Alert dialog = new Alert(Alert.AlertType.CONFIRMATION); dialog.setTitle(GENERAL_BUNDLE.getString("bill.generated")); // TODO: translate dialog.setHeaderText(GENERAL_BUNDLE.getString("bill.validity")); dialog.getButtonTypes().setAll(ButtonType.CLOSE, new ButtonType(GENERAL_BUNDLE.getString("button.Print"), ButtonBar.ButtonData.FINISH), new ButtonType(GENERAL_BUNDLE.getString("button.ShowPDF"), ButtonBar.ButtonData.OK_DONE)); dialog.showAndWait(); if (dialog.getResult() != null) { ButtonBar.ButtonData result = dialog.getResult().getButtonData(); if (result.equals(ButtonBar.ButtonData.FINISH)) { Desktop.getDesktop().print(file); } else if (result.equals(ButtonBar.ButtonData.OK_DONE)) { Desktop.getDesktop().open(file); } } } catch (DocumentException | IOException e) { e.printStackTrace(); } } else { Alert dialog = new Alert(Alert.AlertType.WARNING); dialog.getButtonTypes().setAll( new ButtonType(GENERAL_BUNDLE.getString("button.tryAgain"), ButtonBar.ButtonData.OK_DONE), ButtonType.CANCEL); dialog.setTitle(GENERAL_BUNDLE.getString("dialog.build.incomplete")); List<Product.Category> missing = build.getMissingMandatoryParts(); dialog.setHeaderText(GENERAL_BUNDLE.getString("dialog.build.needed")); final HBox content = new HBox(10); missing.forEach(category -> { Button button = new Button(App.PART_NAMES_BUNDLE.getString(category.toString())); button.setGraphic(Category.getGraphic(category, 30)); button.setContentDisplay(ContentDisplay.TOP); button.setId(category.toString()); button.setOnAction(actionEvent -> { try { Part part = pickNewPart(category); if (part != null) { saved.set(false); build.add(part); displayBuild(); if (Category.isStorage(category)) { content.getChildren() .removeIf(node -> node.getId().equals(Product.Category.HDD_SSD.toString()) || node.getId().equals(Product.Category.HDD.toString())); content.autosize(); } else { content.getChildren().remove(button); } dialog.getDialogPane().getScene().getWindow().sizeToScene(); if (content.getChildren().isEmpty()) { buttonBill.fire(); dialog.close(); } } } catch (IOException e) { e.printStackTrace(); } }); content.getChildren().add(button); }); dialog.getDialogPane().setContent(content); dialog.showAndWait(); ButtonType result = dialog.getResult(); if (result != null && result.getButtonData().equals(ButtonBar.ButtonData.OK_DONE)) { dialog.hide(); onBillAction(event); } } }
From source file:essencialles.modelos.relatorio.tipos.MaisVendidos.java
@Override public void run() { try {// ww w . j a va 2 s.c o m //criao do PDF Document doc = new Document(PageSize.A4, 72, 72, 72, 72); OutputStream saida = new FileOutputStream(this.caminho); PdfWriter.getInstance(doc, saida); doc.open(); try { //logomarca Image img = Image.getInstance("src/views/img/logomarca.png"); img.setAlignment(Element.ALIGN_CENTER); doc.add(img); } catch (BadElementException | IOException ex) { throw new RuntimeException(ex); } //paragrafo Paragraph paragrafo = new Paragraph("Relatrio de Produtos Mais Vendidos"); paragrafo.setSpacingAfter(50); paragrafo.setSpacingBefore(15); paragrafo.setFont(new Font(Font.FontFamily.COURIER, Font.BOLD)); paragrafo.setAlignment(Element.ALIGN_CENTER); doc.add(paragrafo); /*tabela PdfPTable tabela = new PdfPTable(9); tabela.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell header = new PdfPCell(new Paragraph("Todos os Produtos")); header.setColspan(9); tabela.addCell(header); tabela.addCell("Codigo"); tabela.addCell("Nome"); tabela.addCell("Marca"); tabela.addCell("Preo"); tabela.addCell("Custo"); tabela.addCell("Cor"); tabela.addCell("Descrio"); tabela.addCell("Peso"); tabela.addCell("Disponibilidade"); tabela.addCell("Validade"); adicionar linhas da tabela. for (Produto p : produtos) { String cod = p.getCod_produto() + ""; String nome = p.getNome(); String marca = p.getMarca(); String preco = p.getPreco() + ""; String custo = p.getCusto() + ""; String cor = p.getCor(); String descricao = p.getDescricao(); String peso = p.getPeso(); String disponibilidade = p.getDisponibilidade(); String validade = p.getValidade() + ""; }*/ try { saida.flush(); } catch (IOException ex) { throw new RuntimeException(ex); } doc.close(); try { saida.close(); } catch (IOException ex) { throw new RuntimeException(ex); } } catch (FileNotFoundException | DocumentException ex) { throw new RuntimeException(ex); } }
From source file:essencialles.modelos.relatorio.tipos.ProdutosTodos.java
@Override public void run() { try {//from w w w .j a v a 2 s . c om //criao do PDF Document doc = new Document(PageSize.A4, 72, 72, 72, 72); OutputStream saida = new FileOutputStream(this.caminho); PdfWriter.getInstance(doc, saida); doc.open(); try { //logomarca Image img = Image.getInstance("src/views/img/logomarca.png"); img.setAlignment(Element.ALIGN_CENTER); doc.add(img); } catch (BadElementException | IOException ex) { throw new RuntimeException(ex); } //paragrafo Paragraph paragrafo = new Paragraph("Relatrio de Produtos"); paragrafo.setSpacingAfter(50); paragrafo.setSpacingBefore(15); paragrafo.setFont(new Font(Font.FontFamily.COURIER, Font.BOLD)); paragrafo.setAlignment(Element.ALIGN_CENTER); doc.add(paragrafo); /*tabela PdfPTable tabela = new PdfPTable(9); tabela.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell header = new PdfPCell(new Paragraph("Todos os Produtos")); header.setColspan(9); tabela.addCell(header); tabela.addCell("Codigo"); tabela.addCell("Nome"); tabela.addCell("Marca"); tabela.addCell("Preo"); tabela.addCell("Custo"); tabela.addCell("Cor"); tabela.addCell("Descrio"); tabela.addCell("Peso"); tabela.addCell("Disponibilidade"); tabela.addCell("Validade"); adicionar linhas da tabela. for (ProdutoModelo p : produtos) { String cod = p.getCod_produto() + ""; String nome = p.getNome(); String marca = p.getMarca(); String preco = p.getPreco() + ""; String custo = p.getCusto() + ""; String cor = p.getCor(); String descricao = p.getDescricao(); String peso = p.getPeso(); String disponibilidade = p.getDisponibilidade(); String validade = p.getValidade() + ""; }*/ try { saida.flush(); } catch (IOException ex) { throw new RuntimeException(ex); } doc.close(); try { saida.close(); } catch (IOException ex) { throw new RuntimeException(ex); } } catch (FileNotFoundException | DocumentException ex) { throw new RuntimeException(ex); } }
From source file:ExternalNonFormClasses.PDFEnator.java
public void createPDF() { try {//from w w w . j av a2 s.c om Rectangle one = new Rectangle(PageSize.LETTER); Document doc = new Document(one); PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(getDirectory() + "\\" + data_title + ".pdf")); //doc.setMargins(1, 1, 1, 1); doc.open(); PdfContentByte canvas = writer.getDirectContent(); Font font = new Font(Font.FontFamily.TIMES_ROMAN, 11); x = one.getWidth(); y = one.getHeight(); tableX = (x / 2) - 235; tableY = y - 180; getHeader(); Paragraph para = new Paragraph("Province of " + formHeaderValues[0], font); para.setAlignment(Element.ALIGN_CENTER); doc.add(para); Paragraph para2 = new Paragraph("City/Municipality of " + formHeaderValues[1], font); para2.setAlignment(Element.ALIGN_CENTER); doc.add(para2); Paragraph para3 = new Paragraph("Barangay " + formHeaderValues[2], font); para3.setAlignment(Element.ALIGN_CENTER); doc.add(para3); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); Paragraph para4 = new Paragraph(data_title, font); para4.setAlignment(Element.ALIGN_CENTER); doc.add(para4); PdfPTable pdftable; pdftable = writeHeaders(font); //pdftable.writeSelectedRows(0, -1, (x / 2) - 235, y - 180, canvas); int counter = 1, rowCounter = 0, columnCounter = 0; int yIncrementor = 16; boolean test = true; while (counter <= this.totalDataSize) { //System.out.println("data#: " + counter + "@ (" + rowCounter + "," + (columnCounter) + ") = " + this.table.getValueAt(rowCounter, columnCounter).toString()); PdfPCell cell1 = new PdfPCell( new Paragraph(this.table.getValueAt(rowCounter, columnCounter++).toString(), font)); cell1.setPaddingBottom(5); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); pdftable.addCell(cell1); if (counter > 0 && (counter % column) == 0) { //if there are 4 columns //pdftable.writeSelectedRows(0, -1, (x / 2) - 235, (y - 180) - yIncrementor, canvas); //print row of 4 columns yIncrementor += 16; //move to next row coordinate, so y coordinate plus plus rowCounter++; //move to next row columnCounter = 0; //back to start column boolean newPage = false; if ((tableY - ((yIncrementor - 16) + 80)) <= 72) { //if it exceeds 1 inch in footer, new page // System.out.println("(" + tableY + "-((" + yIncrementor + "-16)+" + 16 + "))"); // System.out.println("Nanobra? YES; " + (tableY - ((yIncrementor - 16) + 16))); newPage = true; } else if ((tableY - (yIncrementor + 80)) <= 72) { // System.out.println("(" + tableY + "-(" + yIncrementor + "+" + 16 + "))"); // System.out.println("Nanobra? YES; " + (tableY - (yIncrementor + 16))); newPage = true; } if (newPage) { pdftable.completeRow(); pdftable.writeSelectedRows(0, -1, tableX, tableY, canvas); //print the data for the current page doc.newPage(); //create new page pdftable = writeHeaders(font); //write headers to new page yIncrementor = 16; //restore default } } // if (counter == this.totalDataSize) { //para lng mudouble ang data // if (test) { // test = false; // counter = 0; // rowCounter = 0; // columnCounter = 0; // } // } else if (counter == this.totalDataSize) { // if (!test) { // break; // } // } counter++; } pdftable.completeRow(); pdftable.writeSelectedRows(0, -1, tableX, tableY, canvas); doc.close(); } catch (DocumentException | FileNotFoundException ex) { Logger.getLogger(PDFEnator.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ExternalNonFormClasses.PDFEnator.java
public void createProfilePDF(String id, ArrayList<String> data) { this.data_title = "Citizen Profile"; this.table = null; this.caller = 4; this.row = 17; this.column = 2; this.personID = id; this.totalDataSize = row * column; tableHeaderData = new ArrayList<>(); tableHeaderData.add("Label"); tableHeaderData.add("Information"); ArrayList<String> dataAll = data; try {//from ww w. j a va 2 s . co m getHeader(); Document doc = new Document(); PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(getDirectory() + "\\" + data_title + ".pdf")); Rectangle one = new Rectangle(PageSize.LETTER); doc.setPageSize(one); //doc.setMargins(1, 1, 1, 1); doc.open(); PdfContentByte canvas = writer.getDirectContent(); Font font = new Font(Font.FontFamily.TIMES_ROMAN, 11); x = one.getWidth(); y = one.getHeight(); tableX = (x / 2) - 235; tableY = y - 180; Paragraph para = new Paragraph("Province of " + formHeaderValues[0], font); para.setAlignment(Element.ALIGN_CENTER); doc.add(para); Paragraph para2 = new Paragraph("City/Municipality of " + formHeaderValues[1], font); para2.setAlignment(Element.ALIGN_CENTER); doc.add(para2); Paragraph para3 = new Paragraph("Barangay " + formHeaderValues[2], font); para3.setAlignment(Element.ALIGN_CENTER); doc.add(para3); Paragraph para4 = new Paragraph(data_title, font); para4.setAlignment(Element.ALIGN_CENTER); doc.add(para4); PdfPTable pdftable; pdftable = writeHeaders(font); int counter = 1, rowCounter = 0, columnCounter = 0; int yIncrementor = 16; String label = null, value = null; System.out.println(dataAll.size()); while (counter <= this.row) { switch (counter - 1) { case 0: label = "Citizen ID"; value = id; break; case 1: label = "Last Name"; value = dataAll.get(counter - 2); break; case 2: label = "First Name"; value = dataAll.get(counter - 2); break; case 3: label = "Middle Name"; value = dataAll.get(counter - 2); break; case 4: label = "Name Suffix"; value = dataAll.get(counter - 2); break; case 5: label = "Date of Birth"; value = dataAll.get(counter - 2); break; case 6: label = "Gender"; value = dataAll.get(counter - 2); break; case 7: label = "Address"; value = dataAll.get(counter - 2); break; case 8: label = "Age"; value = dataAll.get(counter - 2); break; case 9: label = "Place of Birth"; value = dataAll.get(counter - 2); break; case 10: label = "Civil Status"; value = dataAll.get(counter - 2); break; case 11: label = "Contact"; value = dataAll.get(counter - 2); break; case 12: label = "Zip Code"; value = dataAll.get(counter - 2); break; case 13: label = "Precinct Number"; value = dataAll.get(counter - 2); break; case 14: label = "Occupation"; value = dataAll.get(counter - 2); break; case 15: label = "Email Address"; value = dataAll.get(counter - 2); break; case 16: label = "Religion"; value = dataAll.get(counter - 2); break; } if (value.length() <= 0) { value = "N/A"; } System.out.println(label + ": " + value); PdfPCell cell1 = new PdfPCell(new Paragraph(label, font)); cell1.setPaddingBottom(5); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); pdftable.addCell(cell1); PdfPCell cell2 = new PdfPCell(new Paragraph(value, font)); cell2.setPaddingBottom(5); cell2.setHorizontalAlignment(Element.ALIGN_LEFT); pdftable.addCell(cell2); if (counter > 0 && (counter % column) == 0) { //if there are 4 columns yIncrementor += 16; //move to next row coordinate, so y coordinate plus plus rowCounter++; //move to next row columnCounter = 0; //back to start column boolean newPage = false; if ((tableY - ((yIncrementor - 16) + 80)) <= 72 || (tableY - (yIncrementor + 80)) <= 72) { //if it exceeds 1 inch in footer, new page newPage = true; } if (newPage) { pdftable.completeRow(); pdftable.writeSelectedRows(0, -1, tableX, tableY, canvas); //print the data for the current page doc.newPage(); //create new page pdftable = writeHeaders(font); //write headers to new page yIncrementor = 16; //restore default } } counter++; } pdftable.completeRow(); pdftable.writeSelectedRows(0, -1, tableX, tableY, canvas); doc.close(); } catch (DocumentException | FileNotFoundException ex) { Logger.getLogger(PDFEnator.class.getName()).log(Level.SEVERE, null, ex); } }