List of usage examples for com.itextpdf.text.pdf BaseFont createFont
public static BaseFont createFont(String name, String encoding, boolean embedded) throws DocumentException, IOException
From source file:PdfFormFiller.WrongParamsExeption.java
License:GNU General Public License
public static void fillPDFFile(String pdf_filename_in, String pdf_filename_out, String fields_filename, String font_file, String op, Boolean flatten, Boolean verbose) { OutputStream os;/*from w w w . ja v a 2 s . c o m*/ PdfStamper stamp; try { PdfReader reader = new PdfReader(pdf_filename_in); if (pdf_filename_out != null) { os = new FileOutputStream(pdf_filename_out); } else { os = System.out; } stamp = new PdfStamper(reader, os, '\0'); AcroFields form = stamp.getAcroFields(); if (op.equals("list")) { formList(form); } else { if (font_file != null) { BaseFont bf = BaseFont.createFont(font_file, BaseFont.IDENTITY_H, true); form.addSubstitutionFont(bf); } Map<String, String> fields = readFile(fields_filename); for (Map.Entry<String, String> entry : fields.entrySet()) { if (verbose) System.out.println("Field name = '" + entry.getKey() + "', New field value: '" + entry.getValue() + "'"); form.setField(entry.getKey(), entry.getValue()); } stamp.setFormFlattening(flatten); stamp.close(); } } catch (FileNotFoundException e) { System.err.println("FileNotFoundException: " + e.getMessage()); System.exit(2); } catch (IOException e) { System.err.println("Input output error: " + e.getMessage()); System.exit(3); } catch (DocumentException e) { System.err.println("Error while processing document: " + e.getMessage()); System.exit(4); } }
From source file:pdfMaker.MakePdfFile.java
public void createPdf(String mainTitle, String subTitle, String url, String userName, //String scanType, String comment,/*from ww w . java 2 s. c o m*/ //String thisPassCode, String passCodeA, String passCodeB, String fileDir, Boolean noBarCodePrint) throws IOException, DocumentException, RuntimeException { Document document = null; try { // step 1 document = new Document(PageSize.A4, 60, 50, 50, 35); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileDir)); // step 3 document.open(); // step 4 PdfContentByte cb = writer.getDirectContent(); /* Properties props = new Properties(); String jarPath = System.getProperty("java.class.path"); String dirPath = jarPath.substring(0, jarPath.lastIndexOf(File.separator)+1); FontFactory.registerDirectory("/res"); FontFactory.register("ipag.ttf"); Font ipaGothic = FontFactory.getFont("ipag", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10); //10 is the size InputStream is = getClass().getResourceAsStream("/res/ipag.ttf"); */ Properties props = new Properties(); String jarPath = System.getProperty("java.class.path"); String dirPath = jarPath.substring(0, jarPath.lastIndexOf(File.separator) + 1); System.out.println(jarPath); System.out.println(dirPath); System.out.println(System.getProperty("user.dir")); Font ipaGothic = new Font(BaseFont.createFont(System.getProperty("user.dir") + "\\res\\ipag.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 11); //?(2) PdfPTable pdfPTable = new PdfPTable(2); pdfPTable.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); pdfPTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); pdfPTable.getDefaultCell().setFixedHeight(150); pdfPTable.setWidthPercentage(100f); int pdfPTableWidth[] = { 10, 90 }; pdfPTable.setWidths(pdfPTableWidth); PdfPCell cell_1_1 = new PdfPCell(new Paragraph("??", ipaGothic)); cell_1_1.setVerticalAlignment(Element.ALIGN_MIDDLE); cell_1_1.setHorizontalAlignment(Element.ALIGN_CENTER); cell_1_1.setFixedHeight(50); PdfPCell cell_1_2 = new PdfPCell(new Paragraph(mainTitle, ipaGothic)); cell_1_2.setVerticalAlignment(Element.ALIGN_MIDDLE); cell_1_2.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cell_2_1 = new PdfPCell(new Paragraph("", ipaGothic)); cell_2_1.setVerticalAlignment(Element.ALIGN_MIDDLE); cell_2_1.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cell_2_2 = new PdfPCell(new Paragraph(subTitle, ipaGothic)); cell_2_2.setVerticalAlignment(Element.ALIGN_MIDDLE); cell_2_2.setHorizontalAlignment(Element.ALIGN_CENTER); cell_2_2.setFixedHeight(50); pdfPTable.addCell(cell_1_1); pdfPTable.addCell(cell_1_2); pdfPTable.addCell(cell_2_1); pdfPTable.addCell(cell_2_2); PdfPCell cellUrlKey = new PdfPCell(new Paragraph("?", ipaGothic)); cellUrlKey.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUrlKey.setHorizontalAlignment(Element.ALIGN_CENTER); cellUrlKey.setRowspan(2); pdfPTable.addCell(cellUrlKey); PdfPCell cellUrlValue = new PdfPCell(new Paragraph(url, ipaGothic)); cellUrlValue.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUrlValue.setHorizontalAlignment(Element.ALIGN_CENTER); cellUrlValue.setFixedHeight(50); pdfPTable.addCell(cellUrlValue); /* */ //cellUrlValue.getImage(); writer.getDirectContent().addImage(cellUrlValue.getImage(), 100, 100, 100, 100, 100, 100); if (url.length() != 0 && !noBarCodePrint) { /* ? BarcodeQRCode qr = new BarcodeQRCode(url, 50, 50, null); PdfPCell cellUrlValueQr = new PdfPCell(qr.getImage()); cellUrlValueQr.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUrlValueQr.setHorizontalAlignment(Element.ALIGN_CENTER); cellUrlValueQr.setFixedHeight(80); pdfPTable.addCell(cellUrlValueQr); */ Image image = ZxingUti.getQRCode(url); // SHIFT_JIS com.itextpdf.text.Image iTextImage = com.itextpdf.text.Image.getInstance(image, null); PdfPCell cell = new PdfPCell(iTextImage); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setFixedHeight(100); pdfPTable.addCell(cell); // SIFT_JIS } else { PdfPCell cellUrlValueQr = new PdfPCell(new Paragraph("", ipaGothic)); cellUrlValueQr.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUrlValueQr.setHorizontalAlignment(Element.ALIGN_CENTER); cellUrlValueQr.setFixedHeight(80); pdfPTable.addCell(cellUrlValueQr); } PdfPCell cellUserNameKey = new PdfPCell(new Paragraph("", ipaGothic)); cellUserNameKey.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUserNameKey.setHorizontalAlignment(Element.ALIGN_CENTER); cellUserNameKey.setRowspan(2); pdfPTable.addCell(cellUserNameKey); PdfPCell cellUserNameValue = new PdfPCell(new Paragraph(userName, ipaGothic)); cellUserNameValue.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUserNameValue.setHorizontalAlignment(Element.ALIGN_CENTER); cellUserNameValue.setFixedHeight(30); pdfPTable.addCell(cellUserNameValue); if (userName.length() != 0 && !noBarCodePrint) { Barcode128 code128 = new Barcode128(); code128.setCode(userName); code128.setFont(ipaGothic.getBaseFont()); code128.setBarHeight(40f); PdfPCell cellUserNameValueBc = new PdfPCell(code128.createImageWithBarcode(cb, null, null)); cellUserNameValueBc.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUserNameValueBc.setHorizontalAlignment(Element.ALIGN_CENTER); cellUserNameValueBc.setFixedHeight(80); pdfPTable.addCell(cellUserNameValueBc); } else { PdfPCell cellUserNameValueBc = new PdfPCell(new Paragraph("---", ipaGothic)); cellUserNameValueBc.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUserNameValueBc.setHorizontalAlignment(Element.ALIGN_CENTER); cellUserNameValueBc.setFixedHeight(80); pdfPTable.addCell(cellUserNameValueBc); } PdfPCell cellPassCodeKey = new PdfPCell(new Paragraph("?", ipaGothic)); cellPassCodeKey.setVerticalAlignment(Element.ALIGN_MIDDLE); cellPassCodeKey.setHorizontalAlignment(Element.ALIGN_CENTER); cellPassCodeKey.setRowspan(3); pdfPTable.addCell(cellPassCodeKey); PdfPCell cellPassCodeValue = new PdfPCell(new Paragraph(passCodeA + passCodeB, ipaGothic)); cellPassCodeValue.setVerticalAlignment(Element.ALIGN_MIDDLE); cellPassCodeValue.setHorizontalAlignment(Element.ALIGN_CENTER); cellPassCodeValue.setFixedHeight(30); pdfPTable.addCell(cellPassCodeValue); if (passCodeA.length() != 0 && !noBarCodePrint) { Barcode128 code128 = new Barcode128(); code128.setCode(passCodeA); code128.setFont(ipaGothic.getBaseFont()); code128.setBarHeight(40f); PdfPCell cellPassCodeA_Bc = new PdfPCell(code128.createImageWithBarcode(cb, null, null)); cellPassCodeA_Bc.setVerticalAlignment(Element.ALIGN_MIDDLE); cellPassCodeA_Bc.setHorizontalAlignment(Element.ALIGN_CENTER); cellPassCodeA_Bc.setFixedHeight(80); pdfPTable.addCell(cellPassCodeA_Bc); } else { PdfPCell cellPassCodeA_Bc = new PdfPCell(new Paragraph("---", ipaGothic)); cellPassCodeA_Bc.setVerticalAlignment(Element.ALIGN_MIDDLE); cellPassCodeA_Bc.setHorizontalAlignment(Element.ALIGN_CENTER); cellPassCodeA_Bc.setFixedHeight(80); pdfPTable.addCell(cellPassCodeA_Bc); } if (passCodeB.length() != 0 && !noBarCodePrint) { Barcode128 code128 = new Barcode128(); code128.setCode(passCodeB); code128.setFont(ipaGothic.getBaseFont()); code128.setBarHeight(40f); PdfPCell cellPassCodeB_Bc = new PdfPCell(code128.createImageWithBarcode(cb, null, null)); cellPassCodeB_Bc.setVerticalAlignment(Element.ALIGN_MIDDLE); cellPassCodeB_Bc.setHorizontalAlignment(Element.ALIGN_CENTER); cellPassCodeB_Bc.setFixedHeight(80); pdfPTable.addCell(cellPassCodeB_Bc); } else { PdfPCell cellPassCodeB_Bc = new PdfPCell(new Paragraph("---", ipaGothic)); cellPassCodeB_Bc.setVerticalAlignment(Element.ALIGN_MIDDLE); cellPassCodeB_Bc.setHorizontalAlignment(Element.ALIGN_CENTER); cellPassCodeB_Bc.setFixedHeight(80); pdfPTable.addCell(cellPassCodeB_Bc); } PdfPCell cellCommentKey = new PdfPCell(new Paragraph("?", ipaGothic)); cellCommentKey.setVerticalAlignment(Element.ALIGN_MIDDLE); cellCommentKey.setHorizontalAlignment(Element.ALIGN_CENTER); pdfPTable.addCell(cellCommentKey); PdfPCell cellCommentValue = new PdfPCell(new Paragraph(comment, ipaGothic)); cellCommentValue.setVerticalAlignment(Element.ALIGN_TOP); cellCommentValue.setHorizontalAlignment(Element.ALIGN_LEFT); cellCommentValue.setFixedHeight(150); pdfPTable.addCell(cellCommentValue); PdfPCell cellIssueKey = new PdfPCell(new Paragraph("", ipaGothic)); cellIssueKey.setVerticalAlignment(Element.ALIGN_MIDDLE); cellIssueKey.setHorizontalAlignment(Element.ALIGN_CENTER); pdfPTable.addCell(cellIssueKey); Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); String strDate = sdf.format(cal.getTime()); PdfPCell cellIssueValue = new PdfPCell(new Paragraph(strDate, ipaGothic)); cellIssueValue.setVerticalAlignment(Element.ALIGN_MIDDLE); cellIssueValue.setHorizontalAlignment(Element.ALIGN_CENTER); cellIssueValue.setFixedHeight(20); pdfPTable.addCell(cellIssueValue); //?? document.add(pdfPTable); /* // CODE 128 document.add(new Paragraph("?? : " + mainTitle, ipaGothic)); document.add(new Paragraph(" : " + subTitle, ipaGothic)); document.add(new Paragraph("-------------------------------------------------------")); document.add(new Paragraph(" " + strDate)); document.add(new Paragraph("-------------------------------------------------------")); BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.EMBEDDED); Font font = new Font(bf, 12); document.add(new Paragraph("", ipaGothic)); document.add(new Paragraph(url, ipaGothic)); code128.setCode(url); code128.setFont(bf); code128.setX(1); //document.add(code128.createImageWithBarcode(cb, null, null)); document.add(new Paragraph("USER", ipaGothic)); if (userName.length() != 0) { document.add(new Paragraph(userName, ipaGothic)); code128.setCode(userName); code128.setFont(bf); code128.setBarHeight(40f); document.add(code128.createImageWithBarcode(cb, null, null)); } document.add(new Paragraph("CODE", ipaGothic)); if (passCode.length() != 0) { document.add(new Paragraph(passCode, ipaGothic)); code128.setCode(passCode); code128.setFont(bf); document.add(code128.createImageWithBarcode(cb, null, null)); } document.add(new Paragraph("?", ipaGothic)); document.add(new Paragraph(comment, ipaGothic)); */ // step 5 document.close(); } catch (RuntimeException ex) { document.close(); throw ex; } }
From source file:pl.marcinmilkowski.hocrtopdf.Main.java
License:Open Source License
/** * @param args/*from ww w. ja v a 2 s. c o m*/ */ public static void main(String[] args) { try { if (args.length < 1 || args[0] == "--help" || args[0] == "-h") { System.out.print("Usage: java pl.marcinmilkowski.hocrtopdf.Main INPUTURL.html OUTPUTURL.pdf\n" + "\n" + "Converts hOCR files into PDF\n" + "\n" + "Example: java pl.marcinmilkowski.hocrtopdf.Main hocr.html output.pdf\n"); if (args.length < 1) System.exit(-1); else System.exit(0); } URL inputHOCRFile = null; FileOutputStream outputPDFStream = null; try { File file = new File(args[0]); inputHOCRFile = file.toURI().toURL(); } catch (MalformedURLException e) { System.out.println("The first parameter has to be a valid file."); System.out.println("We got an error: " + e.getMessage()); System.exit(-1); } try { outputPDFStream = new FileOutputStream(args[1]); } catch (FileNotFoundException e) { System.out.println("The second parameter has to be a valid URL"); System.exit(-1); } // The resolution of a PDF file (using iText) is 72pt per inch float pointsPerInch = 72.0f; // Using the jericho library to parse the HTML file Source source = new Source(inputHOCRFile); int pageCounter = 1; Document pdfDocument = null; PdfWriter pdfWriter = null; PdfContentByte cb = null; RandomAccessFileOrArray ra = null; // Find the tag of class ocr_page in order to load the scanned image StartTag pageTag = source.getNextStartTag(0, "class", OCRPAGE); while (pageTag != null) { int prevPos = pageTag.getEnd(); Pattern imagePattern = Pattern.compile("image\\s+([^;]+)"); Matcher imageMatcher = imagePattern.matcher(pageTag.getElement().getAttributeValue("title")); if (!imageMatcher.find()) { System.out.println("Could not find a tag of class \"ocr_page\", aborting."); System.exit(-1); } // Load the image Image pageImage = null; try { File file = new File(imageMatcher.group(1)); pageImage = Image.getInstance(file.toURI().toURL()); } catch (MalformedURLException e) { System.out.println("Could not load the scanned image from: " + "file://" + imageMatcher.group(1) + ", aborting."); System.exit(-1); } if (pageImage.getOriginalType() == Image.ORIGINAL_TIFF) { // this might // be // multipage // tiff! File file = new File(imageMatcher.group(1)); if (pageCounter == 1 || ra == null) { ra = new RandomAccessFileOrArray(file.toURI().toURL()); } int nPages = TiffImage.getNumberOfPages(ra); if (nPages > 0 && pageCounter <= nPages) { pageImage = TiffImage.getTiffImage(ra, pageCounter); } } int dpiX = pageImage.getDpiX(); if (dpiX == 0) { // for images that don't set the resolution we assume // 300 dpi dpiX = 300; } int dpiY = pageImage.getDpiY(); if (dpiY == 0) { // as above for dpiX dpiY = 300; } float dotsPerPointX = dpiX / pointsPerInch; float dotsPerPointY = dpiY / pointsPerInch; float pageImagePixelHeight = pageImage.getHeight(); if (pdfDocument == null) { pdfDocument = new Document(new Rectangle(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY)); pdfWriter = PdfWriter.getInstance(pdfDocument, outputPDFStream); pdfDocument.open(); // Put the text behind the picture (reverse for debugging) // cb = pdfWriter.getDirectContentUnder(); cb = pdfWriter.getDirectContent(); } else { pdfDocument.setPageSize(new Rectangle(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY)); pdfDocument.newPage(); } // first define a standard font for our text BaseFont base = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED); Font defaultFont = new Font(base, 8); // FontFactory.getFont(FontFactory.HELVETICA, 8, Font.BOLD, // CMYKColor.BLACK); cb.setHorizontalScaling(1.0f); pageImage.scaleToFit(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY); pageImage.setAbsolutePosition(0, 0); // Put the image in front of the text (reverse for debugging) // pdfWriter.getDirectContent().addImage(pageImage); pdfWriter.getDirectContentUnder().addImage(pageImage); // In order to place text behind the recognised text snippets we are // interested in the bbox property Pattern bboxPattern = Pattern.compile("bbox(\\s+\\d+){4}"); // This pattern separates the coordinates of the bbox property Pattern bboxCoordinatePattern = Pattern.compile("(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)"); // Only tags of the ocr_line class are interesting StartTag ocrTag = source.getNextStartTag(prevPos, "class", OCRPAGEORLINE); while (ocrTag != null) { prevPos = ocrTag.getEnd(); if ("ocrx_word".equalsIgnoreCase(ocrTag.getAttributeValue("class"))) { net.htmlparser.jericho.Element lineElement = ocrTag.getElement(); Matcher bboxMatcher = bboxPattern.matcher(lineElement.getAttributeValue("title")); if (bboxMatcher.find()) { // We found a tag of the ocr_line class containing a bbox property Matcher bboxCoordinateMatcher = bboxCoordinatePattern.matcher(bboxMatcher.group()); bboxCoordinateMatcher.find(); int[] coordinates = { Integer.parseInt((bboxCoordinateMatcher.group(1))), Integer.parseInt((bboxCoordinateMatcher.group(2))), Integer.parseInt((bboxCoordinateMatcher.group(3))), Integer.parseInt((bboxCoordinateMatcher.group(4))) }; String line = lineElement.getContent().getTextExtractor().toString(); float bboxWidthPt = (coordinates[2] - coordinates[0]) / dotsPerPointX; float bboxHeightPt = (coordinates[3] - coordinates[1]) / dotsPerPointY; // Put the text into the PDF cb.beginText(); // Comment the next line to debug the PDF output (visible Text) cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE); // height cb.setFontAndSize(defaultFont.getBaseFont(), Math.max(Math.round(bboxHeightPt), 1)); // width cb.setHorizontalScaling(bboxWidthPt / cb.getEffectiveStringWidth(line, false)); cb.moveText((coordinates[0] / dotsPerPointX), ((pageImagePixelHeight - coordinates[3]) / dotsPerPointY)); cb.showText(line); cb.endText(); cb.setHorizontalScaling(1.0f); } } else { if ("ocr_page".equalsIgnoreCase(ocrTag.getAttributeValue("class"))) { pageCounter++; pageTag = ocrTag; break; } } ocrTag = source.getNextStartTag(prevPos, "class", OCRPAGEORLINE); } if (ocrTag == null) { pdfDocument.close(); break; } } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:platnosci.WyszukPlatnosciController.java
public void akcjaDrukuj() { Drukowanie drukPanel = new Drukowanie(null, true); drukPanel.setLocationRelativeTo(null); drukPanel.setVisible(true);//from w ww . j av a 2s . c o m if (!drukPanel.isDruk()) { return; } FileOutputStream file = null; File druk = null; try { Document document = new Document(); String userPath = System.getProperty("user.home"); druk = new File(userPath + "/Baks wydruki"); if (!druk.exists()) { druk.mkdirs(); } SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); String strdate = ""; Date calendardate = new Date(); strdate = sdf.format(calendardate.getTime()); File wydruk = new File(druk + "/Platnosci - " + strdate + ".pdf"); if (!wydruk.exists()) { try { wydruk.createNewFile(); } catch (IOException ex) { Logger.getLogger(WyszukPlatnosciController.class.getName()).log(Level.SEVERE, null, ex); } } file = new FileOutputStream(wydruk); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(wydruk)); document.open(); PdfPTable table = new PdfPTable(4); // 3 columns. table.setWidthPercentage(100); //Width 100% table.setSpacingBefore(10f); //Space before table table.setSpacingAfter(10f); //Space after table //Set Column widths float[] columnWidths = { 1f, 1f, 1f, 1f }; table.setWidths(columnWidths); BaseFont bf = BaseFont.createFont("c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); PdfPCell cell1 = new PdfPCell(new Paragraph("Lp", new com.itextpdf.text.Font(bf, 16))); cell1.setPaddingLeft(10); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setVerticalAlignment(Element.ALIGN_LEFT); PdfPCell cell2 = new PdfPCell(new Paragraph("Firma", new com.itextpdf.text.Font(bf, 16))); cell2.setPaddingLeft(10); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setVerticalAlignment(Element.ALIGN_LEFT); PdfPCell cell3 = new PdfPCell( new Paragraph("Data patnoci", new com.itextpdf.text.Font(bf, 16))); cell3.setPaddingLeft(10); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setVerticalAlignment(Element.ALIGN_LEFT); PdfPCell cell4 = new PdfPCell(new Paragraph("Kwota", new com.itextpdf.text.Font(bf, 16))); cell4.setPaddingLeft(10); cell4.setHorizontalAlignment(Element.ALIGN_CENTER); cell4.setVerticalAlignment(Element.ALIGN_LEFT); //To avoid having the cell border and the content overlap, if you are having thick cell borders cell1.setBorder(PdfPCell.NO_BORDER); cell2.setBorder(PdfPCell.NO_BORDER); cell3.setBorder(PdfPCell.NO_BORDER); cell4.setBorder(PdfPCell.NO_BORDER); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); TO_Invoice kryt = new TO_Invoice(); kryt.setDataDo(drukPanel.getDateDo()); kryt.setDataOd(drukPanel.getDateOd()); kryt.setStatus(TO_InvoiceStatus.ZAPLACONA); List<TO_Invoice> listaFaktur = getDaoFactory().getDaoInvoice().getListaInvoiceDruk(getConnection(), kryt); Integer i = 1; for (TO_Invoice item : listaFaktur) { table.addCell(getNewCell(i.toString() + ".")); table.addCell(getNewCell(item.getPaymentCompany().getName())); table.addCell(getNewCell(item.getDataZaplacenia().toString())); table.addCell(getNewCell(TO_Invoice.getWynikSumaKoszt(item.getKoszt()) + " z")); i++; } document.add(table); document.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } catch (FileNotFoundException ex) { Logger.getLogger(WyszukPlatnosciController.class.getName()).log(Level.SEVERE, null, ex); BaksSessionBean.getInstance().fireMessage(widok, "Wydruk", "Pdf do ktrego chcesz zapisa wynik jest otwarty!\n Zamknij i sprbuj jeszcze raz."); } finally { try { file.close(); } catch (IOException ex) { Logger.getLogger(WyszukPlatnosciController.class.getName()).log(Level.SEVERE, null, ex); } } Desktop desktop = null; if (Desktop.isDesktopSupported()) { desktop = Desktop.getDesktop(); try { desktop.open(druk); } catch (IOException ex) { Logger.getLogger(WyszukPlatnosciController.class.getName()).log(Level.SEVERE, null, ex); } } BaksSessionBean.getInstance().fireMessage(widok, "Zapis", "Wydruk zapisany w folderze: " + System.getProperty("user.home") + "/Baks wydruki"); }
From source file:platnosci.WyszukPlatnosciController.java
public PdfPCell getNewCell(String nazwa) { BaseFont bf = null;//from w w w . java2s . com try { bf = BaseFont.createFont("c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); } catch (DocumentException ex) { Logger.getLogger(WyszukPlatnosciController.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(WyszukPlatnosciController.class.getName()).log(Level.SEVERE, null, ex); } PdfPCell cell = new PdfPCell(new Paragraph(nazwa, new com.itextpdf.text.Font(bf, 12))); cell.setPaddingLeft(10); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_LEFT); return cell; }
From source file:printInv.GenerateInvoice.java
public void initializeFonts() { try {/*from w w w.j av a2s . c o m*/ bfBold = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:psManage.StructSheet.java
public void createPdf(String mainTitle, String subTitle, String url, String userName, //String scanType, String comment,/*from w w w. j a v a 2 s. c o m*/ //String thisPassCode, String passCodeA, String passCodeB, String fileDir, Boolean noBarCodePrint) throws IOException, DocumentException, RuntimeException { Document document = null; try { // step 1 document = new Document(PageSize.A4, 60, 50, 50, 35); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileDir)); // step 3 document.open(); // step 4 PdfContentByte cb = writer.getDirectContent(); /* Properties props = new Properties(); String jarPath = System.getProperty("java.class.path"); String dirPath = jarPath.substring(0, jarPath.lastIndexOf(File.separator)+1); FontFactory.registerDirectory("/res"); FontFactory.register("ipag.ttf"); Font ipaGothic = FontFactory.getFont("ipag", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10); //10 is the size InputStream is = getClass().getResourceAsStream("/res/ipag.ttf"); */ Properties props = new Properties(); String jarPath = System.getProperty("java.class.path"); String dirPath = jarPath.substring(0, jarPath.lastIndexOf(File.separator) + 1); System.out.println(jarPath); System.out.println(dirPath); System.out.println(System.getProperty("user.dir")); Font ipaGothic = new Font(BaseFont.createFont(System.getProperty("user.dir") + "\\res\\ipag.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 11); Font ipaGothic14 = new Font(BaseFont.createFont(System.getProperty("user.dir") + "\\res\\ipag.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 14); //?(2) PdfPTable pdfPTable = new PdfPTable(2); pdfPTable.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); pdfPTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); pdfPTable.getDefaultCell().setFixedHeight(150); pdfPTable.setWidthPercentage(100f); int pdfPTableWidth[] = { 10, 90 }; pdfPTable.setWidths(pdfPTableWidth); PdfPCell cell_1_1 = new PdfPCell(new Paragraph("??", ipaGothic)); cell_1_1.setVerticalAlignment(Element.ALIGN_MIDDLE); cell_1_1.setHorizontalAlignment(Element.ALIGN_CENTER); cell_1_1.setFixedHeight(50); PdfPCell cell_1_2 = new PdfPCell(new Paragraph(mainTitle, ipaGothic)); cell_1_2.setVerticalAlignment(Element.ALIGN_MIDDLE); cell_1_2.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cell_2_1 = new PdfPCell(new Paragraph("", ipaGothic)); cell_2_1.setVerticalAlignment(Element.ALIGN_MIDDLE); cell_2_1.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cell_2_2 = new PdfPCell(new Paragraph(subTitle, ipaGothic)); cell_2_2.setVerticalAlignment(Element.ALIGN_MIDDLE); cell_2_2.setHorizontalAlignment(Element.ALIGN_CENTER); cell_2_2.setFixedHeight(50); pdfPTable.addCell(cell_1_1); pdfPTable.addCell(cell_1_2); pdfPTable.addCell(cell_2_1); pdfPTable.addCell(cell_2_2); PdfPCell cellUrlKey = new PdfPCell(new Paragraph("?", ipaGothic)); cellUrlKey.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUrlKey.setHorizontalAlignment(Element.ALIGN_CENTER); cellUrlKey.setRowspan(2); pdfPTable.addCell(cellUrlKey); PdfPCell cellUrlValue = new PdfPCell(new Paragraph(url, ipaGothic)); Chunk chunk = new Chunk(url, ipaGothic); // ?????????????? System.out.println("??" + chunk.getWidthPoint()); cellUrlValue.setVerticalAlignment(Element.ALIGN_MIDDLE); if (chunk.getWidthPoint() > 410) { // ?????????? cellUrlValue.setHorizontalAlignment(Element.ALIGN_LEFT); } else { // ????????? cellUrlValue.setHorizontalAlignment(Element.ALIGN_CENTER); } cellUrlValue.setFixedHeight(50); pdfPTable.addCell(cellUrlValue); if (url.length() != 0 && !noBarCodePrint) { /* ? BarcodeQRCode qr = new BarcodeQRCode(url, 50, 50, null); PdfPCell cellUrlValueQr = new PdfPCell(qr.getImage()); cellUrlValueQr.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUrlValueQr.setHorizontalAlignment(Element.ALIGN_CENTER); cellUrlValueQr.setFixedHeight(80); pdfPTable.addCell(cellUrlValueQr); */ Image image = ZxingUti.getQRCode(url); // SHIFT_JIS com.itextpdf.text.Image iTextImage = com.itextpdf.text.Image.getInstance(image, null); PdfPCell cell = new PdfPCell(iTextImage); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setFixedHeight(100); pdfPTable.addCell(cell); // SIFT_JIS } else { PdfPCell cellUrlValueQr = new PdfPCell(new Paragraph("", ipaGothic)); cellUrlValueQr.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUrlValueQr.setHorizontalAlignment(Element.ALIGN_CENTER); cellUrlValueQr.setFixedHeight(80); pdfPTable.addCell(cellUrlValueQr); } PdfPCell cellUserNameKey = new PdfPCell(new Paragraph("", ipaGothic)); cellUserNameKey.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUserNameKey.setHorizontalAlignment(Element.ALIGN_CENTER); cellUserNameKey.setRowspan(2); pdfPTable.addCell(cellUserNameKey); PdfPCell cellUserNameValue = new PdfPCell(new Paragraph(userName, ipaGothic14)); cellUserNameValue.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUserNameValue.setHorizontalAlignment(Element.ALIGN_CENTER); cellUserNameValue.setFixedHeight(30); pdfPTable.addCell(cellUserNameValue); if (userName.length() != 0 && !noBarCodePrint) { Barcode128 code128 = new Barcode128(); code128.setCode(userName); code128.setFont(ipaGothic.getBaseFont()); code128.setBarHeight(40f); PdfPCell cellUserNameValueBc = new PdfPCell(code128.createImageWithBarcode(cb, null, null)); cellUserNameValueBc.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUserNameValueBc.setHorizontalAlignment(Element.ALIGN_CENTER); cellUserNameValueBc.setFixedHeight(80); pdfPTable.addCell(cellUserNameValueBc); } else { PdfPCell cellUserNameValueBc = new PdfPCell(new Paragraph("---", ipaGothic)); cellUserNameValueBc.setVerticalAlignment(Element.ALIGN_MIDDLE); cellUserNameValueBc.setHorizontalAlignment(Element.ALIGN_CENTER); cellUserNameValueBc.setFixedHeight(80); pdfPTable.addCell(cellUserNameValueBc); } PdfPCell cellPassCodeKey = new PdfPCell(new Paragraph("?", ipaGothic)); cellPassCodeKey.setVerticalAlignment(Element.ALIGN_MIDDLE); cellPassCodeKey.setHorizontalAlignment(Element.ALIGN_CENTER); cellPassCodeKey.setRowspan(3); pdfPTable.addCell(cellPassCodeKey); PdfPCell cellPassCodeValue = new PdfPCell(new Paragraph(passCodeA + passCodeB, ipaGothic14)); cellPassCodeValue.setVerticalAlignment(Element.ALIGN_MIDDLE); cellPassCodeValue.setHorizontalAlignment(Element.ALIGN_CENTER); cellPassCodeValue.setFixedHeight(30); pdfPTable.addCell(cellPassCodeValue); if (passCodeA.length() != 0 && !noBarCodePrint) { Barcode128 code128 = new Barcode128(); code128.setCode(passCodeA); code128.setFont(ipaGothic.getBaseFont()); code128.setBarHeight(40f); PdfPCell cellPassCodeA_Bc = new PdfPCell(code128.createImageWithBarcode(cb, null, null)); cellPassCodeA_Bc.setVerticalAlignment(Element.ALIGN_MIDDLE); cellPassCodeA_Bc.setHorizontalAlignment(Element.ALIGN_CENTER); cellPassCodeA_Bc.setFixedHeight(80); pdfPTable.addCell(cellPassCodeA_Bc); } else { PdfPCell cellPassCodeA_Bc = new PdfPCell(new Paragraph("---", ipaGothic)); cellPassCodeA_Bc.setVerticalAlignment(Element.ALIGN_MIDDLE); cellPassCodeA_Bc.setHorizontalAlignment(Element.ALIGN_CENTER); cellPassCodeA_Bc.setFixedHeight(80); pdfPTable.addCell(cellPassCodeA_Bc); } if (passCodeB.length() != 0 && !noBarCodePrint) { Barcode128 code128 = new Barcode128(); code128.setCode(passCodeB); code128.setFont(ipaGothic.getBaseFont()); code128.setBarHeight(40f); PdfPCell cellPassCodeB_Bc = new PdfPCell(code128.createImageWithBarcode(cb, null, null)); cellPassCodeB_Bc.setVerticalAlignment(Element.ALIGN_MIDDLE); cellPassCodeB_Bc.setHorizontalAlignment(Element.ALIGN_CENTER); cellPassCodeB_Bc.setFixedHeight(80); pdfPTable.addCell(cellPassCodeB_Bc); } else { PdfPCell cellPassCodeB_Bc = new PdfPCell(new Paragraph("---", ipaGothic)); cellPassCodeB_Bc.setVerticalAlignment(Element.ALIGN_MIDDLE); cellPassCodeB_Bc.setHorizontalAlignment(Element.ALIGN_CENTER); cellPassCodeB_Bc.setFixedHeight(80); pdfPTable.addCell(cellPassCodeB_Bc); } PdfPCell cellCommentKey = new PdfPCell(new Paragraph("?", ipaGothic)); cellCommentKey.setVerticalAlignment(Element.ALIGN_MIDDLE); cellCommentKey.setHorizontalAlignment(Element.ALIGN_CENTER); pdfPTable.addCell(cellCommentKey); PdfPCell cellCommentValue = new PdfPCell(new Paragraph(comment, ipaGothic)); cellCommentValue.setVerticalAlignment(Element.ALIGN_TOP); cellCommentValue.setHorizontalAlignment(Element.ALIGN_LEFT); cellCommentValue.setFixedHeight(150); pdfPTable.addCell(cellCommentValue); PdfPCell cellIssueKey = new PdfPCell(new Paragraph("", ipaGothic)); cellIssueKey.setVerticalAlignment(Element.ALIGN_MIDDLE); cellIssueKey.setHorizontalAlignment(Element.ALIGN_CENTER); pdfPTable.addCell(cellIssueKey); Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); String strDate = sdf.format(cal.getTime()); PdfPCell cellIssueValue = new PdfPCell(new Paragraph(strDate, ipaGothic)); cellIssueValue.setVerticalAlignment(Element.ALIGN_MIDDLE); cellIssueValue.setHorizontalAlignment(Element.ALIGN_CENTER); cellIssueValue.setFixedHeight(20); pdfPTable.addCell(cellIssueValue); //?? document.add(pdfPTable); /* // CODE 128 document.add(new Paragraph("?? : " + mainTitle, ipaGothic)); document.add(new Paragraph(" : " + subTitle, ipaGothic)); document.add(new Paragraph("-------------------------------------------------------")); document.add(new Paragraph(" " + strDate)); document.add(new Paragraph("-------------------------------------------------------")); BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.EMBEDDED); Font font = new Font(bf, 12); document.add(new Paragraph("", ipaGothic)); document.add(new Paragraph(url, ipaGothic)); code128.setCode(url); code128.setFont(bf); code128.setX(1); //document.add(code128.createImageWithBarcode(cb, null, null)); document.add(new Paragraph("USER", ipaGothic)); if (userName.length() != 0) { document.add(new Paragraph(userName, ipaGothic)); code128.setCode(userName); code128.setFont(bf); code128.setBarHeight(40f); document.add(code128.createImageWithBarcode(cb, null, null)); } document.add(new Paragraph("CODE", ipaGothic)); if (passCode.length() != 0) { document.add(new Paragraph(passCode, ipaGothic)); code128.setCode(passCode); code128.setFont(bf); document.add(code128.createImageWithBarcode(cb, null, null)); } document.add(new Paragraph("?", ipaGothic)); document.add(new Paragraph(comment, ipaGothic)); */ // step 5 document.close(); } catch (RuntimeException ex) { document.close(); throw ex; } }
From source file:pw.core.pdf.PWPdfFile.java
License:Open Source License
protected BaseFont getFont(Font font, Encoding encoding, boolean isEmbedded) { String fontName = font.ordinal() < FONT_NAMES.length ? FONT_NAMES[font.ordinal()] : FONT_NAMES[FONT_NAMES.length - 1]; String encodingName = encoding.ordinal() < ENCODING_NAMES.length ? ENCODING_NAMES[encoding.ordinal()] : ENCODING_NAMES[ENCODING_NAMES.length - 1]; try {//from w ww . jav a 2 s . co m return BaseFont.createFont(fontName, encodingName, isEmbedded ? BaseFont.EMBEDDED : BaseFont.NOT_EMBEDDED); } catch (DocumentException e) { throw new PWError(e, "Failed to get font."); } catch (IOException e) { throw new PWError(e, "Failed to get font."); } }
From source file:quanlyhoadon.PDFProcess.java
public boolean printPDF(String dest, Company company, Bill bill, LinhTinh lt) { try {/* w w w. ja v a 2s . c o m*/ BaseFont urName = BaseFont.createFont("font/Time_New_Roman.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font nameFont = new Font(urName, 16, Font.BOLD); Font addFont = new Font(urName, 12); Font boldFont = new Font(urName, 14, Font.BOLD); Font contentFont = new Font(urName, 14); Font footFont = new Font(urName, 16); Document document = new Document(PageSize.A4.rotate()); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(3); table.setWidthPercentage(100); table.setWidths(new float[] { 2, 6, 3 }); //logo Image img = Image.getInstance("logo.jpg"); img.scaleAbsolute(100, 100); PdfPCell cell = new PdfPCell(img); cell.setBorder(PdfPCell.NO_BORDER); //middle content String name = company.getName(); String address = "\n\n?C: " + company.getAddress(); String phone = "\n\n?T: " + company.getPhone(); String hed = "\n\n PHIU B?O GI?"; Phrase mid = new Phrase(); mid.add(new Chunk(name, nameFont)); mid.add(new Chunk(address + phone, addFont)); mid.add(new Chunk(hed, nameFont)); PdfPCell midCell = new PdfPCell(mid); // midCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); midCell.setPaddingTop(15); midCell.setPaddingLeft(40); midCell.setPaddingRight(15); midCell.setPaddingBottom(15); midCell.setBorder(PdfPCell.NO_BORDER); // right content String soCt = "S CT: " + bill.getId(); String date = "\n\nNgy: " + bill.getDate() + "\n\nKho : " + lt.getKho(); Phrase right = new Phrase(); right.add(new Chunk(soCt + date, addFont)); PdfPCell rightCell = new PdfPCell(right); rightCell.setPadding(15); rightCell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); table.addCell(midCell); table.addCell(rightCell); document.add(table); Phrase p1 = new Phrase(); p1.add(new Chunk("?n v: ", addFont)); p1.add(new Chunk(lt.getDonVi(), boldFont)); p1.add(new Chunk("\n?a Ch: \n", addFont)); Paragraph pagra = new Paragraph(p1); pagra.setIndentationLeft(15); pagra.setSpacingBefore(10); pagra.setSpacingAfter(10); document.add(pagra); //table lt PdfPTable tablList = new PdfPTable(6); tablList.setWidthPercentage(100); tablList.setWidths(new float[] { 1, 5, 7, 2, 3, 4 }); String title[] = { "STT", "M Hng", "Tn Hng", "SL", "?n Gi", "Thnh Ti?n" }; for (int i = 0; i < title.length; ++i) { Phrase phr = new Phrase(); phr.add(new Chunk(title[i], boldFont)); PdfPCell p = new PdfPCell(phr); p.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); tablList.addCell(p); } //--- add content for (int i = 0; i < bill.getCount(); ++i) { Phrase[] phrase = new Phrase[6]; PdfPCell[] pCell = new PdfPCell[6]; for (int k = 0; k < 6; ++k) { phrase[k] = new Phrase(); } phrase[0].add(new Chunk((i + 1) + "", contentFont)); phrase[1].add(new Chunk(bill.getMaHangAt(i), contentFont)); phrase[2].add(new Chunk(bill.getTenHangAt(i), contentFont)); phrase[3].add(new Chunk(bill.getSoLuongAt(i) + "", contentFont)); phrase[4].add(new Chunk(bill.getDonGiaAt(i) + "", contentFont)); phrase[5].add(new Chunk(bill.getThanhTienAt(i) + "", contentFont)); for (int j = 0; j < 6; ++j) { pCell[j] = new PdfPCell(phrase[j]); pCell[j].setHorizontalAlignment(PdfPCell.ALIGN_CENTER); tablList.addCell(pCell[j]); } } PdfPCell cell1 = new PdfPCell(new Phrase(new Chunk("Tng", boldFont))); cell1.setColspan(3); cell1.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); tablList.addCell(cell1); // PdfPCell cell2 = new PdfPCell(new Phrase(new Chunk("" + bill.getTong(), boldFont))); cell2.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); tablList.addCell(cell2); // PdfPCell cell3 = new PdfPCell(new Phrase(new Chunk("", boldFont))); cell3.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); tablList.addCell(cell3); // PdfPCell cell4 = new PdfPCell(new Phrase(new Chunk(String.format("%,d", bill.tongTien()), boldFont))); cell4.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); tablList.addCell(cell4); document.add(tablList); //-------------------in phuong thuc thanh toan Phrase p2 = new Phrase(); p2.add(new Chunk("Phng thc thanh ton: ", contentFont)); p2.add(new Chunk(lt.getThanhToan(), boldFont)); p2.add(new Chunk("\nLu mua hng min tr li. Vui lng gi phiu trong 7 ngy: ", contentFont)); Paragraph pagra2 = new Paragraph(p2); pagra2.setSpacingBefore(5); pagra2.setSpacingAfter(5); document.add(pagra2); //---- footer content PdfPTable tblFoot = new PdfPTable(3); tblFoot.setWidthPercentage(100); tblFoot.setWidths(new float[] { 1, 1, 1 }); PdfPCell leftFootCell = new PdfPCell(new Phrase(new Chunk("NGI LP ", footFont))); PdfPCell midFootCell = new PdfPCell(new Phrase(new Chunk("K TO?N ", footFont))); PdfPCell rightFootCell = new PdfPCell(new Phrase(new Chunk("TH TRNG ?N V ", footFont))); leftFootCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); leftFootCell.setBorder(PdfPCell.NO_BORDER); midFootCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); midFootCell.setBorder(PdfPCell.NO_BORDER); rightFootCell.setBorder(PdfPCell.NO_BORDER); rightFootCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); tblFoot.addCell(leftFootCell); tblFoot.addCell(midFootCell); tblFoot.addCell(rightFootCell); document.add(tblFoot); // document.close(); JOptionPane.showMessageDialog(null, "In hoadon.pdf thnh cng"); return true; } catch (Exception e) { JOptionPane.showMessageDialog(null, "hoadon.pdf ang c m\nVui lng ng li. Sau th li"); e.printStackTrace(); return false; } }
From source file:Report.ItextReport.java
public static void absText(PdfWriter writer, String text, int x, int y) { try {/*from w w w.j a va2s . c om*/ PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.saveState(); cb.beginText(); cb.moveText(x, y); cb.setFontAndSize(bf, 12); cb.showText(text); cb.endText(); cb.restoreState(); } catch (DocumentException | IOException e) { e.getMessage(); } }