/** @see com.lowagie.toolbox.AbstractTool#execute() */ public void execute() { try { if (getValue("odd") == null) throw new InstantiationException("You need to choose a sourcefile for the odd pages"); File odd_file = (File) getValue("odd"); if (getValue("even") == null) throw new InstantiationException("You need to choose a sourcefile for the even pages"); File even_file = (File) getValue("even"); if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file"); File pdf_file = (File) getValue("destfile"); RandomAccessFileOrArray odd = new RandomAccessFileOrArray(odd_file.getAbsolutePath()); RandomAccessFileOrArray even = new RandomAccessFileOrArray(even_file.getAbsolutePath()); Image img = TiffImage.getTiffImage(odd, 1); Document document = new Document(new Rectangle(img.getScaledWidth(), img.getScaledHeight())); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf_file)); document.open(); PdfContentByte cb = writer.getDirectContent(); int count = Math.max(TiffImage.getNumberOfPages(odd), TiffImage.getNumberOfPages(even)); for (int c = 0; c < count; ++c) { try { Image imgOdd = TiffImage.getTiffImage(odd, c + 1); Image imgEven = TiffImage.getTiffImage(even, count - c); document.setPageSize(new Rectangle(imgOdd.getScaledWidth(), imgOdd.getScaledHeight())); document.newPage(); imgOdd.setAbsolutePosition(0, 0); cb.addImage(imgOdd); document.setPageSize(new Rectangle(imgEven.getScaledWidth(), imgEven.getScaledHeight())); document.newPage(); imgEven.setAbsolutePosition(0, 0); cb.addImage(imgEven); } catch (Exception e) { System.out.println("Exception page " + (c + 1) + " " + e.getMessage()); } } odd.close(); even.close(); document.close(); } catch (Exception e) { e.printStackTrace(); } }
public static void populateDescription(String path, BaseFont font, FontDescription descr) throws IOException, NoSuchFieldException, IllegalAccessException, DocumentException { RandomAccessFileOrArray rf = null; try { rf = new RandomAccessFileOrArray(getTTCName(path)); rf = populateDescription0(path, font, descr, rf); } finally { if (rf != null) { try { rf.close(); } catch (IOException e) { // ignore } } } }
private static RandomAccessFileOrArray populateDescription0( String path, BaseFont font, FontDescription descr, RandomAccessFileOrArray rf) throws NoSuchFieldException, IllegalAccessException, DocumentException, IOException { Map tables = extractTables(font); descr.setStyle(guessStyle(font)); int[] location = (int[]) tables.get("OS/2"); if (location == null) { throw new DocumentException("Table 'OS/2' does not exist in " + path); } rf.seek(location[0]); int want = 4; long got = rf.skip(want); if (got < want) { throw new DocumentException( "Skip TT font weight, expect read " + want + " bytes, but only got " + got); } descr.setWeight(rf.readUnsignedShort()); want = 20; got = rf.skip(want); if (got < want) { throw new DocumentException( "Skip TT font strikeout, expect read " + want + " bytes, but only got " + got); } descr.setYStrikeoutSize(rf.readShort()); descr.setYStrikeoutPosition(rf.readShort()); location = (int[]) tables.get("post"); if (location != null) { rf.seek(location[0]); want = 8; got = rf.skip(want); if (got < want) { throw new DocumentException( "Skip TT font underline, expect read " + want + " bytes, but only got " + got); } descr.setUnderlinePosition(rf.readShort()); descr.setUnderlineThickness(rf.readShort()); } rf.close(); rf = null; return rf; }
/** * Reads the font data. * * @param ttfAfm the font as a <CODE>byte</CODE> array, possibly <CODE>null</CODE> * @throws DocumentException the font is invalid * @throws IOException the font file could not be read */ void process() throws DocumentException, IOException { positionTables = new HashMap<String, int[]>(); metadataTables = new HashMap<String, byte[]>(); try { rf = new RandomAccessFileOrArray(fileName); if (ttcIndex.length() > 0) { int dirIdx = Integer.parseInt(ttcIndex); if (dirIdx < 0) throw new DocumentException("The font index for " + fileName + " must be positive."); String mainTag = readStandardString(4); if (!mainTag.equals("ttcf")) throw new DocumentException(fileName + " is not a valid TTC file."); rf.skipBytes(4); int dirCount = rf.readInt(); if (dirIdx >= dirCount) throw new DocumentException( "The font index for " + fileName + " must be between 0 and " + (dirCount - 1) + ". It was " + dirIdx + "."); rf.skipBytes(dirIdx * 4); directoryOffset = rf.readInt(); } rf.seek(directoryOffset); rf.readFully(directoryRawData); int ttId = Util.getInt(directoryRawData, 0); if (ttId != 0x00010000 && ttId != 0x4F54544F) throw new DocumentException(fileName + " is not a valid TTF or OTF file."); int num_tables = Util.getUnsignedShort(directoryRawData, 4); for (int k = 0; k < num_tables; ++k) { byte[] rawData = new byte[16]; rf.readFully(rawData); String tag = getStandardString(rawData, 0, 4); int table_location[] = new int[2]; table_location[0] = Util.getInt(rawData, 8); table_location[1] = Util.getInt(rawData, 12); positionTables.put(tag, table_location); metadataTables.put(tag, rawData); } fontName = getBaseFont(); fullName = getNames(4); // full name familyName = getNames(1); // family name notice = getNames(0); version = getNames(5); if (!justNames) { fillTables(); readGlyphWidths(); readCMaps(); readKerning(); readBbox(); GlyphWidths = null; } } finally { if (rf != null) { rf.close(); if (!embedded) rf = null; } } }
public void close() throws IOException { rf.close(); }