List of usage examples for com.itextpdf.text.pdf PdfName FONT
PdfName FONT
To view the source code for com.itextpdf.text.pdf PdfName FONT.
Click Source Link
From source file:com.betel.flowers.pdf.util.RemoveBlankPageFromPDF.java
public static void removeBlankPdfPages(String source, String destination) throws IOException, DocumentException { PdfReader r = null;/*from w w w . j a va2s . co m*/ RandomAccessSourceFactory rasf = null; RandomAccessFileOrArray raf = null; Document document = null; PdfCopy writer = null; try { r = new PdfReader(source); // deprecated // RandomAccessFileOrArray raf // = new RandomAccessFileOrArray(pdfSourceFile); // itext 5.4.1 rasf = new RandomAccessSourceFactory(); raf = new RandomAccessFileOrArray(rasf.createBestSource(source)); document = new Document(r.getPageSizeWithRotation(1)); writer = new PdfCopy(document, new FileOutputStream(destination)); document.open(); PdfImportedPage page = null; for (int i = 1; i <= r.getNumberOfPages(); i++) { // first check, examine the resource dictionary for /Font or // /XObject keys. If either are present -> not blank. PdfDictionary pageDict = r.getPageN(i); PdfDictionary resDict = (PdfDictionary) pageDict.get(PdfName.RESOURCES); boolean noFontsOrImages = true; if (resDict != null) { noFontsOrImages = resDict.get(PdfName.FONT) == null && resDict.get(PdfName.XOBJECT) == null; } if (!noFontsOrImages) { byte bContent[] = r.getPageContent(i, raf); ByteArrayOutputStream bs = new ByteArrayOutputStream(); bs.write(bContent); if (bs.size() > BLANK_THRESHOLD) { page = writer.getImportedPage(r, i); writer.addPage(page); } } } } finally { if (document != null) { document.close(); } if (writer != null) { writer.close(); } if (raf != null) { raf.close(); } if (r != null) { r.close(); } } }
From source file:de.mat.utils.pdftools.PdfExtractEmptyPages.java
License:Mozilla Public License
/** * <h4>FeatureDomain:</h4>/*from ww w .j av a 2 s . com*/ * PublishingTools * <h4>FeatureDescription:</h4> * reads readerOrig and adds pages to writerRemoved if empty, or to * writerTrimmed if not empty * <h4>FeatureResult:</h4> * <ul> * <li>updates writerTrimmed - add all pages which are not empty * <li>updates writerRemoved - add all empty pages * </ul> * <h4>FeatureKeywords:</h4> * PDF Publishing * @param origFileName - orig filename of the sourcepdf * @param readerOrig - reader of source * @param writerTrimmed - writer for trimmed pages * @param writerRemoved - writer for empty pages * @param flgTrim - ?? * @return - count of trimmed pages * @throws Exception */ public static int addTrimmedPages(String origFileName, PdfReader readerOrig, PdfCopy writerTrimmed, PdfCopy writerRemoved, boolean flgTrim) throws Exception { PdfImportedPage page = null; int countTrimmedPages = 0; //loop each page for (int i = 1; i <= readerOrig.getNumberOfPages(); i++) { boolean flgIsEmpty = true; // get dictionary PdfDictionary pageDict = readerOrig.getPageN(i); // every pdf-version has its own way :-( char version = readerOrig.getPdfVersion(); if (version == '3') { // PDF-Version: 3 // examine the resource dictionary for /Font or // /XObject keys. If either are present, they're almost // certainly actually used on the page -> not blank. PdfObject myObj = pageDict.get(PdfName.RESOURCES); PdfDictionary resDict = null; if (myObj instanceof PdfDictionary) { resDict = (PdfDictionary) myObj; } else { resDict = (PdfDictionary) PdfReader.getPdfObject(myObj); } if (resDict != null) { flgIsEmpty = resDict.get(PdfName.FONT) == null && resDict.get(PdfName.XOBJECT) == null; if (LOGGER.isInfoEnabled()) { if (flgIsEmpty) { LOGGER.info("probably empty page " + i + " Version: 1." + version + " FONT/XOBJECT found in File:" + origFileName); } else { LOGGER.info("normal page " + i + " Version: 1." + version + " no FONT/XOBJECT found in File:" + origFileName); } } } } else if (version == '4') { // PDF-Version: 4 // check the contentsize. // get the page content byte bContent[] = readerOrig.getPageContent(i); ByteArrayOutputStream bs = new ByteArrayOutputStream(); // write the content to an output stream bs.write(bContent); flgIsEmpty = true; if (bs.size() > blankPdfsize) { if (LOGGER.isInfoEnabled()) LOGGER.info("normal page " + i + " Version: 1." + version + " BS:" + bs.size() + " File:" + origFileName); flgIsEmpty = false; } else { if (LOGGER.isInfoEnabled()) LOGGER.info("probably empty page " + i + " Version: 1." + version + " BS:" + bs.size() + " File:" + origFileName); } } else if (version == '5') { // PDF-Version: 5 // check the contentsize. // get the page content byte bContent[] = readerOrig.getPageContent(i); ByteArrayOutputStream bs = new ByteArrayOutputStream(); // write the content to an output stream bs.write(bContent); flgIsEmpty = true; if (bs.size() > blankPdfsize_v5) { if (LOGGER.isInfoEnabled()) LOGGER.info("normal page " + i + " Version: 1." + version + " BS:" + bs.size() + " File:" + origFileName); flgIsEmpty = false; } else { if (LOGGER.isInfoEnabled()) LOGGER.info("probably empty page " + i + " Version: 1." + version + " BS:" + bs.size() + " File:" + origFileName); } } // add page to removed or trimmed document if (!flgIsEmpty || !flgTrim) { if (LOGGER.isInfoEnabled()) LOGGER.info("add page " + i); page = writerTrimmed.getImportedPage(readerOrig, i); writerTrimmed.addPage(page); countTrimmedPages++; } else { if (LOGGER.isInfoEnabled()) LOGGER.info("skip page " + i + " Version: 1." + version + " File:" + origFileName); if (writerRemoved != null) { page = writerRemoved.getImportedPage(readerOrig, i); writerRemoved.addPage(page); } } } return countTrimmedPages; }
From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java
License:Open Source License
private Font retrieveFontFromAcroForm(PdfName fontName, PdfNumber size) { PdfIndirectReference fontIndirReference = pdfStamper.getReader().getAcroForm().getAsDict(PdfName.DR) .getAsDict(PdfName.FONT).getAsIndirectObject(fontName); BaseFont bfont = BaseFont.createFont((PRIndirectReference) fontIndirReference); return new Font(bfont, size.floatValue()); }
From source file:pl.edu.icm.cermine.structure.ITextCharacterExtractor.java
License:Open Source License
/** * Processes PDF's fonts dictionary. During the process alternative names * of Standard 14 Fonts are changed to the standard ones, provided that * the font definition doesn't include Widths array. * * Font dictionary in PDF file often includes an array of individual glyphs' widths. * Widths array is always required except for the Standard 14 Fonts, which widths * are kept by iText itself. Unfortunately, if the font uses alternative name instead of * standard one (see PDF Reference 1.7, table H.3), iText doesn't recognize the font as * one of the Standard 14 Fonts, and is unable to determine glyphs widths. In such cases * this method will change alternative names to standard ones before PDF's parsing process *//*from w w w .j a va 2 s . c o m*/ private void processAlternativeFontNames(PdfDictionary resources) { if (resources == null) { return; } PdfDictionary fontsDictionary = resources.getAsDict(PdfName.FONT); if (fontsDictionary == null) { return; } for (PdfName pdfFontName : fontsDictionary.getKeys()) { if (!(fontsDictionary.get(pdfFontName) instanceof PRIndirectReference)) { return; } PRIndirectReference indRef = (PRIndirectReference) fontsDictionary.get(pdfFontName); if (!(PdfReader.getPdfObjectRelease(indRef) instanceof PdfDictionary)) { return; } PdfDictionary fontDictionary = (PdfDictionary) PdfReader.getPdfObjectRelease(indRef); PdfName baseFont = fontDictionary.getAsName(PdfName.BASEFONT); if (baseFont != null) { String fontName = PdfName.decodeName(baseFont.toString()); if (fontDictionary.getAsArray(PdfName.WIDTHS) == null && ALT_TO_STANDART_FONTS.containsKey(fontName)) { fontDictionary.put(PdfName.BASEFONT, ALT_TO_STANDART_FONTS.get(fontName)); } } } }
From source file:spntoolsdata.pdf.util.RemoveBlankPageFromPDF.java
public static void removeBlankPdfPages(String source, String destination) throws IOException, DocumentException { PdfReader r = null;//from w w w .j a v a2s.c o m RandomAccessSourceFactory rasf = null; RandomAccessFileOrArray raf = null; Document document = null; PdfCopy writer = null; try { r = new PdfReader(source); // deprecated // RandomAccessFileOrArray raf // = new RandomAccessFileOrArray(pdfSourceFile); // itext 5.4.1 rasf = new RandomAccessSourceFactory(); raf = new RandomAccessFileOrArray(rasf.createBestSource(source)); document = new Document(r.getPageSizeWithRotation(1)); writer = new PdfCopy(document, new FileOutputStream(destination)); document.open(); PdfImportedPage page = null; for (int i = 1; i <= r.getNumberOfPages(); i++) { // first check, examine the resource dictionary for /Font or // /XObject keys. If either are present -> not blank. PdfDictionary pageDict = r.getPageN(i); PdfDictionary resDict = (PdfDictionary) pageDict.get(PdfName.RESOURCES); boolean noFontsOrImages = true; if (resDict != null) { noFontsOrImages = resDict.get(PdfName.FONT) == null && resDict.get(PdfName.XOBJECT) == null; } if (!noFontsOrImages) { byte bContent[] = r.getPageContent(i, raf); ByteArrayOutputStream bs = new ByteArrayOutputStream(); bs.write(bContent); if (bs.size() > BLANK_THRESHOLD) { page = writer.getImportedPage(r, i); writer.addPage(page); } } } } finally { if (document != null) document.close(); if (writer != null) writer.close(); if (raf != null) raf.close(); if (r != null) r.close(); } }