private Chunk formatNumber(StyleListProperties listProperties, int value) { Chunk symbol = new Chunk("", getFont()); StyleTextProperties textProperties = listProperties.getTextProperties(); if (textProperties != null) { Font font = textProperties.getFont(); if (font != null) { symbol.setFont(font); } } StyleNumFormat numFormat = listProperties.getNumFormat(); if (numFormat != null) { StringBuilder sbuf = new StringBuilder(); // num-prefix String numPrefix = listProperties.getNumPrefix(); if (numPrefix != null) { sbuf.append(numPrefix); } // number if (numFormat.isAlphabetical()) { sbuf.append(RomanAlphabetFactory.getString(value, numFormat.isLowercase())); } else if (numFormat.isRoman()) { sbuf.append(RomanNumberFactory.getString(value, numFormat.isLowercase())); } else { sbuf.append(value); } // num-suffix String numSuffix = listProperties.getNumSuffix(); if (numSuffix != null) { sbuf.append(numSuffix); } symbol.append(sbuf.toString()); } return symbol; }
/** * Adds an item to the page for printing. If the document was not successfully opened, or if the * item has an invalid barcode, no action will be taken and a negative Result will be returned. * * @param item */ public Result addItem(Item item) { if (!_open) { return new Result(false, "Cannot add a product to an unopened file."); } if (!item.getBarcode().isValid()) { return new Result(false, "Cannot print an invalid barcode."); } // Generate the barcode image: BarcodeEAN codeEAN = new BarcodeEAN(); codeEAN.setCodeType(com.itextpdf.text.pdf.Barcode.UPCA); codeEAN.setCode(item.getBarcode().toString()); Image bcimage = codeEAN.createImageWithBarcode(_rawContent, null, null); // Add some text to a chunk, and the image to a chunk. Font font = new Font(Font.FontFamily.HELVETICA, 8); Chunk c1 = new Chunk( item.getProductDescription() + "\n" + item.getShortEntryDateString() + " -> " + item.getShortExpirationDateString() + "\n\n"); c1.setFont(font); Phrase p = new Phrase(c1); Chunk c2 = new Chunk(bcimage, 0, 0); p.add(c2); PdfPCell cell = new PdfPCell(p); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setPadding(10); cell.setBorder(0); _table.addCell(cell); _itemsAdded++; return new Result(true, "Barcode added successfully."); }
private static LinkedList<Chunk> splitScrambleToLineChunks( String paddedScramble, Font scrambleFont, float scrambleColumnWidth) { float availableScrambleWidth = scrambleColumnWidth - 2 * SCRAMBLE_PADDING_HORIZONTAL; int startIndex = 0; int endIndex = 0; LinkedList<Chunk> lineChunks = new LinkedList<Chunk>(); while (startIndex < paddedScramble.length()) { // Walk forwards until we've grabbed the maximum number of characters // that fit in a line, we've run out of characters, or we hit a newline. float substringWidth; for (endIndex++; endIndex <= paddedScramble.length(); endIndex++) { if (paddedScramble.charAt(endIndex - 1) == '\n') { break; } String scrambleSubstring = NON_BREAKING_SPACE + paddedScramble.substring(startIndex, endIndex) + NON_BREAKING_SPACE; substringWidth = scrambleFont.getBaseFont().getWidthPoint(scrambleSubstring, scrambleFont.getSize()); if (substringWidth > availableScrambleWidth) { break; } } // endIndex is one past the best fit, so remove one character and it should fit! endIndex--; // If we're not at the end of the scramble, make sure we're not cutting // a turn in half by walking backwards until we're right before a turn. // Any spaces added for padding after a turn are considered part of // that turn because they're actually NON_BREAKING_SPACE, not a ' '. int perfectFitEndIndex = endIndex; if (endIndex < paddedScramble.length()) { while (true) { if (endIndex < startIndex) { // We walked all the way to the beginning of the line // without finding a good breaking point. Give up and break // in the middle of a turn =(. endIndex = perfectFitEndIndex; break; } // Another dirty hack for sq1: turns only line up // nicely if every line starts with a (x,y). We ensure this // by forcing every line to end with a /. boolean isSquareOne = paddedScramble.indexOf('/') >= 0; if (isSquareOne) { char previousCharacter = paddedScramble.charAt(endIndex - 1); if (previousCharacter == '/') { break; } } else { char currentCharacter = paddedScramble.charAt(endIndex); boolean isTurnCharacter = currentCharacter != ' '; if (!isTurnCharacter || currentCharacter == '\n') { break; } } endIndex--; } } String scrambleSubstring = NON_BREAKING_SPACE + paddedScramble.substring(startIndex, endIndex) + NON_BREAKING_SPACE; // Add NON_BREAKING_SPACE until the scrambleSubstring takes up as much as // space as is available on a line. do { scrambleSubstring += NON_BREAKING_SPACE; substringWidth = scrambleFont.getBaseFont().getWidthPoint(scrambleSubstring, scrambleFont.getSize()); } while (substringWidth <= availableScrambleWidth); // scrambleSubstring is now too big for our line, so remove the // last character. scrambleSubstring = scrambleSubstring.substring(0, scrambleSubstring.length() - 1); // Walk past all whitespace that comes immediately after the line wrap // we are about to insert. while (endIndex < paddedScramble.length() && (paddedScramble.charAt(endIndex) == ' ' || paddedScramble.charAt(endIndex) == '\n')) { endIndex++; } startIndex = endIndex; Chunk lineChunk = new Chunk(scrambleSubstring); lineChunks.add(lineChunk); lineChunk.setFont(scrambleFont); // Force a line wrap! lineChunk.append("\n"); } return lineChunks; }