/** * Reuses an existing image. * @param ref the reference to the image dictionary * @throws BadElementException on error * @return the image */ public static Image getInstance(PRIndirectReference ref) throws BadElementException { PdfDictionary dic = (PdfDictionary)PdfReader.getPdfObjectRelease(ref); int width = ((PdfNumber)PdfReader.getPdfObjectRelease(dic.get(PdfName.WIDTH))).intValue(); int height = ((PdfNumber)PdfReader.getPdfObjectRelease(dic.get(PdfName.HEIGHT))).intValue(); Image imask = null; PdfObject obj = dic.get(PdfName.SMASK); if (obj != null && obj.isIndirect()) { imask = getInstance((PRIndirectReference)obj); } else { obj = dic.get(PdfName.MASK); if (obj != null && obj.isIndirect()) { PdfObject obj2 = PdfReader.getPdfObjectRelease(obj); if (obj2 instanceof PdfDictionary) imask = getInstance((PRIndirectReference)obj); } } Image img = new ImgRaw(width, height, 1, 1, null); img.imageMask = imask; img.directReference = ref; return img; }
/** * Gets the text from a page. * @param page the page number of the page * @return a String with the content as plain text (without PDF syntax) * @throws IOException */ public String getTextFromPage(int page) throws IOException { int totalPages = reader.getNumberOfPages(); if (totalPages < page) { throw new IOException("indicated page does not exists, requested page " + page + " document pages " + totalPages); } if (page <= 0) { throw new IOException("page number must be postive:" + page); } PdfDictionary pageDic = reader.getPageN(page); if (pageDic == null) { return ""; } PdfDictionary resourcesDic = pageDic.getAsDict(PdfName.RESOURCES); extractionProcessor.processContent(getContentBytesForPage(page), resourcesDic); return extractionProcessor.getResultantText(); }
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) { PdfName dictionaryName = (PdfName) operands.get(0); PdfDictionary extGState = processor.resources.getAsDict(PdfName.EXTGSTATE); if (extGState == null) { throw new IllegalArgumentException( "Resources do not contain ExtGState entry. Unable to process operator " + operator); } PdfDictionary gsDic = extGState.getAsDict(dictionaryName); if (gsDic == null) { throw new IllegalArgumentException(dictionaryName + " is an unknown graphics state dictionary"); } // at this point, all we care about is the FONT entry in the GS dictionary PdfArray fontParameter = gsDic.getAsArray(PdfName.FONT); if (fontParameter != null) { CMapAwareDocumentFont font = new CMapAwareDocumentFont( (PRIndirectReference) fontParameter.getPdfObject(0)); float size = fontParameter.getAsNumber(1).floatValue(); processor.gs().font = font; processor.gs().fontSize = size; } }
public void completeInfoDictionary(PdfDictionary info) { if (isPdfX() && !isPdfA1()) { if (info.get(PdfName.GTS_PDFXVERSION) == null) { if (isPdfX1A2001()) { info.put(PdfName.GTS_PDFXVERSION, new PdfString("PDF/X-1:2001")); info.put(new PdfName("GTS_PDFXConformance"), new PdfString("PDF/X-1a:2001")); } else if (isPdfX32002()) info.put(PdfName.GTS_PDFXVERSION, new PdfString("PDF/X-3:2002")); } if (info.get(PdfName.TITLE) == null) { info.put(PdfName.TITLE, new PdfString("Pdf document")); } if (info.get(PdfName.CREATOR) == null) { info.put(PdfName.CREATOR, new PdfString("Unknown")); } if (info.get(PdfName.TRAPPED) == null) { info.put(PdfName.TRAPPED, new PdfName("False")); } } }
PdfObject getColorspace() { if (icc_profile != null) { if ((colorType & 2) == 0) return PdfName.DEVICEGRAY; else return PdfName.DEVICERGB; } if (gamma == 1f && !hasCHRM) { if ((colorType & 2) == 0) return PdfName.DEVICEGRAY; else return PdfName.DEVICERGB; } else { PdfArray array = new PdfArray(); PdfDictionary dic = new PdfDictionary(); if ((colorType & 2) == 0) { if (gamma == 1f) return PdfName.DEVICEGRAY; array.add(PdfName.CALGRAY); dic.put(PdfName.GAMMA, new PdfNumber(gamma)); dic.put(PdfName.WHITEPOINT, new PdfLiteral("[1 1 1]")); array.add(dic); } else { PdfObject wp = new PdfLiteral("[1 1 1]"); array.add(PdfName.CALRGB); if (gamma != 1f) { PdfArray gm = new PdfArray(); PdfNumber n = new PdfNumber(gamma); gm.add(n); gm.add(n); gm.add(n); dic.put(PdfName.GAMMA, gm); } if (hasCHRM) { float z = yW*((xG-xB)*yR-(xR-xB)*yG+(xR-xG)*yB); float YA = yR*((xG-xB)*yW-(xW-xB)*yG+(xW-xG)*yB)/z; float XA = YA*xR/yR; float ZA = YA*((1-xR)/yR-1); float YB = -yG*((xR-xB)*yW-(xW-xB)*yR+(xW-xR)*yB)/z; float XB = YB*xG/yG; float ZB = YB*((1-xG)/yG-1); float YC = yB*((xR-xG)*yW-(xW-xG)*yW+(xW-xR)*yG)/z; float XC = YC*xB/yB; float ZC = YC*((1-xB)/yB-1); float XW = XA+XB+XC; float YW = 1;//YA+YB+YC; float ZW = ZA+ZB+ZC; PdfArray wpa = new PdfArray(); wpa.add(new PdfNumber(XW)); wpa.add(new PdfNumber(YW)); wpa.add(new PdfNumber(ZW)); wp = wpa; PdfArray matrix = new PdfArray(); matrix.add(new PdfNumber(XA)); matrix.add(new PdfNumber(YA)); matrix.add(new PdfNumber(ZA)); matrix.add(new PdfNumber(XB)); matrix.add(new PdfNumber(YB)); matrix.add(new PdfNumber(ZB)); matrix.add(new PdfNumber(XC)); matrix.add(new PdfNumber(YC)); matrix.add(new PdfNumber(ZC)); dic.put(PdfName.MATRIX, matrix); } dic.put(PdfName.WHITEPOINT, wp); array.add(dic); } return array; } }
/** * Adds a prefix for the Collection item. * You can only use this method after you have set the value of the item. * @param prefix a prefix */ public void setPrefix(String key, String prefix) { PdfName fieldname = new PdfName(key); PdfObject o = get(fieldname); if (o == null) throw new IllegalArgumentException("You must set a value before adding a prefix."); PdfDictionary dict = new PdfDictionary(PdfName.COLLECTIONSUBITEM); dict.put(PdfName.D, o); dict.put(PdfName.P, new PdfString(prefix, PdfObject.TEXT_UNICODE)); put(fieldname, dict); }
@Override public void draw() { if (addActualText) { PdfDictionary markedContentProps = new PdfDictionary(); markedContentProps.put(PdfName.ACTUALTEXT, new PdfString(allText, PdfObject.TEXT_UNICODE)); pdfContentByte.beginMarkedContentSequence(PdfName.SPAN, markedContentProps, true); } TabSegment segment = segments.get(segmentIndex); segment.layout.draw( pdfGraphics2D, x + drawPosX,// + leftPadding, y + topPadding + verticalAlignOffset + drawPosY ); if (addActualText) { pdfContentByte.endMarkedContentSequence(); } return; }
public PdfAttachment unpackFile(PdfDictionary filespec) throws IOException { if (filespec == null) return null; PdfName type = filespec.getAsName(PdfName.TYPE); if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type)) return null; PdfDictionary ef = filespec.getAsDict(PdfName.EF); if (ef == null) return null; PRStream prs = (PRStream)PdfReader.getPdfObject(ef.get(PdfName.F)); if (prs == null) return null; PdfString pdfDesc = filespec.getAsString(PdfName.DESC); String desc = pdfDesc != null ? pdfDesc.toString() : ""; PdfString pdfName = filespec.getAsString(PdfName.F); String name = pdfName != null ? pdfName.toString() : ""; byte[] data = PdfReader.getStreamBytes(prs); return new PdfAttachment(name, desc, data); }
/** * Processes PDF syntax * @param contentBytes the bytes of a content stream * @param resources the resources that come with the content stream */ public void processContent(byte[] contentBytes, PdfDictionary resources){ reset(); this.resources = resources; try { PdfContentParser ps = new PdfContentParser(new PRTokeniser(contentBytes)); ArrayList operands = new ArrayList(); while (ps.parse(operands).size() > 0){ PdfLiteral operator = (PdfLiteral)operands.get(operands.size()-1); invokeOperator(operator, operands); } } catch (Exception e) { throw new ExceptionConverter(e); } }
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) { PdfName dictionaryName = (PdfName)operands.get(0); PdfDictionary extGState = processor.resources.getAsDict(PdfName.EXTGSTATE); if (extGState == null) throw new IllegalArgumentException("Resources do not contain ExtGState entry. Unable to process operator " + operator); PdfDictionary gsDic = extGState.getAsDict(dictionaryName); if (gsDic == null) throw new IllegalArgumentException(dictionaryName + " is an unknown graphics state dictionary"); // at this point, all we care about is the FONT entry in the GS dictionary PdfArray fontParameter = gsDic.getAsArray(PdfName.FONT); if (fontParameter != null){ CMapAwareDocumentFont font = new CMapAwareDocumentFont((PRIndirectReference)fontParameter.getPdfObject(0)); float size = fontParameter.getAsNumber(1).floatValue(); processor.gs().font = font; processor.gs().fontSize = size; } }
/** * Writes information about a specific page from PdfReader to the specified output stream. * @since 2.1.5 * @param reader the PdfReader to read the page content from * @param pageNum the page number to read * @param out the output stream to send the content to * @throws IOException */ static public void listContentStreamForPage(PdfReader reader, int pageNum, PrintWriter out) throws IOException { out.println("==============Page " + pageNum + "===================="); out.println("- - - - - Dictionary - - - - - -"); PdfDictionary pageDictionary = reader.getPageN(pageNum); out.println(getDictionaryDetail(pageDictionary)); out.println("- - - - - Content Stream - - - - - -"); RandomAccessFileOrArray f = reader.getSafeFile(); byte[] contentBytes = reader.getPageContent(pageNum, f); f.close(); InputStream is = new ByteArrayInputStream(contentBytes); int ch; while ((ch = is.read()) != -1){ out.print((char)ch); } out.println("- - - - - Text Extraction - - - - - -"); PdfTextExtractor extractor = new PdfTextExtractor(reader); String extractedText = extractor.getTextFromPage(pageNum); if (extractedText.length() != 0) out.println(extractedText); else out.println("No text found on page " + pageNum); out.println(); }
/** * Processes PDF syntax * @param contentBytes the bytes of a content stream * @param resources the resources that come with the content stream */ public void processContent(byte[] contentBytes, PdfDictionary resources) { this.resources.push(resources); try { PdfContentParser ps = new PdfContentParser(new PRTokeniser(contentBytes)); ArrayList operands = new ArrayList(); while (ps.parse(operands).size() > 0) { PdfLiteral operator = (PdfLiteral) operands.get(operands.size() - 1); invokeOperator(operator, operands); } } catch (Exception e) { throw new ExceptionConverter(e); } this.resources.pop(); }
@Override public PdfObject getDirectObject(PdfName key) { for (int i = stack.size() - 1; i >= 0; i--) { PdfDictionary dict = stack.get(i); PdfObject o = dict.getDirectObject(key); if (o != null) { return o; } } return null; }
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) { PdfName fontResourceName = (PdfName) operands.get(0); float size = ((PdfNumber) operands.get(1)).floatValue(); PdfDictionary fontsDictionary = processor.resources.getAsDict(PdfName.FONT); CMapAwareDocumentFont font = new CMapAwareDocumentFont( (PRIndirectReference) fontsDictionary.get(fontResourceName)); processor.gs().font = font; processor.gs().fontSize = size; }
/** Adds the version to the Catalog dictionary. */ public void addToCatalog(PdfDictionary catalog) { if(catalog_version != null) { catalog.put(PdfName.VERSION, catalog_version); } if (extensions != null) { catalog.put(PdfName.EXTENSIONS, extensions); } }
public void completeExtraCatalog(PdfDictionary extraCatalog) { if (isPdfX() && !isPdfA1()) { if (extraCatalog.get(PdfName.OUTPUTINTENTS) == null) { PdfDictionary out = new PdfDictionary(PdfName.OUTPUTINTENT); out.put(PdfName.OUTPUTCONDITION, new PdfString("SWOP CGATS TR 001-1995")); out.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("CGATS TR 001")); out.put(PdfName.REGISTRYNAME, new PdfString("http://www.color.org")); out.put(PdfName.INFO, new PdfString("")); out.put(PdfName.S, PdfName.GTS_PDFX); extraCatalog.put(PdfName.OUTPUTINTENTS, new PdfArray(out)); } } }
/** * Adds the viewer preferences defined in the preferences parameter to a * PdfDictionary (more specifically the root or catalog of a PDF file). * * @param catalog */ public void addToCatalog(PdfDictionary catalog) { // Page Layout catalog.remove(PdfName.PAGELAYOUT); if ((pageLayoutAndMode & PdfWriter.PageLayoutSinglePage) != 0) catalog.put(PdfName.PAGELAYOUT, PdfName.SINGLEPAGE); else if ((pageLayoutAndMode & PdfWriter.PageLayoutOneColumn) != 0) catalog.put(PdfName.PAGELAYOUT, PdfName.ONECOLUMN); else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoColumnLeft) != 0) catalog.put(PdfName.PAGELAYOUT, PdfName.TWOCOLUMNLEFT); else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoColumnRight) != 0) catalog.put(PdfName.PAGELAYOUT, PdfName.TWOCOLUMNRIGHT); else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoPageLeft) != 0) catalog.put(PdfName.PAGELAYOUT, PdfName.TWOPAGELEFT); else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoPageRight) != 0) catalog.put(PdfName.PAGELAYOUT, PdfName.TWOPAGERIGHT); // Page Mode catalog.remove(PdfName.PAGEMODE); if ((pageLayoutAndMode & PdfWriter.PageModeUseNone) != 0) catalog.put(PdfName.PAGEMODE, PdfName.USENONE); else if ((pageLayoutAndMode & PdfWriter.PageModeUseOutlines) != 0) catalog.put(PdfName.PAGEMODE, PdfName.USEOUTLINES); else if ((pageLayoutAndMode & PdfWriter.PageModeUseThumbs) != 0) catalog.put(PdfName.PAGEMODE, PdfName.USETHUMBS); else if ((pageLayoutAndMode & PdfWriter.PageModeFullScreen) != 0) catalog.put(PdfName.PAGEMODE, PdfName.FULLSCREEN); else if ((pageLayoutAndMode & PdfWriter.PageModeUseOC) != 0) catalog.put(PdfName.PAGEMODE, PdfName.USEOC); else if ((pageLayoutAndMode & PdfWriter.PageModeUseAttachments) != 0) catalog.put(PdfName.PAGEMODE, PdfName.USEATTACHMENTS); // viewer preferences (Table 8.1 of the PDF Reference) catalog.remove(PdfName.VIEWERPREFERENCES); if (viewerPreferences.size() > 0) { catalog.put(PdfName.VIEWERPREFERENCES, viewerPreferences); } }
private Image indexedModel(byte bdata[], int bpc, int paletteEntries) throws BadElementException { Image img = new ImgRaw(width, height, 1, bpc, bdata); PdfArray colorspace = new PdfArray(); colorspace.add(PdfName.INDEXED); colorspace.add(PdfName.DEVICERGB); byte np[] = getPalette(paletteEntries); int len = np.length; colorspace.add(new PdfNumber(len / 3 - 1)); colorspace.add(new PdfString(np)); PdfDictionary ad = new PdfDictionary(); ad.put(PdfName.COLORSPACE, colorspace); img.setAdditional(ad); return img; }
/** * Demonstrates some Layer functionality. * */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(); // step 2: PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Layers.pdf")); writer.setPdfVersion(PdfWriter.VERSION_1_5); // step 3: writer.setViewerPreferences(PdfWriter.PageModeUseOC); document.open(); // step 4: PdfContentByte cb = writer.getDirectContent(); Phrase explanation = new Phrase("Layer grouping", new Font(Font.HELVETICA, 20, Font.BOLD, Color.red)); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50, 650, 0); PdfLayer l1 = new PdfLayer("Layer 1", writer); PdfLayer l2 = new PdfLayer("Layer 2", writer); PdfLayer l3 = new PdfLayer("Layer 3", writer); PdfLayerMembership m1 = new PdfLayerMembership(writer); m1.addMember(l2); m1.addMember(l3); Phrase p1 = new Phrase("Text in layer 1"); Phrase p2 = new Phrase("Text in layer 2 or layer 3"); Phrase p3 = new Phrase("Text in layer 3"); cb.beginLayer(l1); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0); cb.endLayer(); cb.beginLayer(m1); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0); cb.endLayer(); cb.beginLayer(l3); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0); cb.endLayer(); PdfOCProperties p = writer.getOCProperties(); PdfArray order = new PdfArray(); order.add(l1.getRef()); PdfArray group = new PdfArray(); group.add(new PdfString("A group of two", PdfObject.TEXT_UNICODE)); group.add(l2.getRef()); group.add(l3.getRef()); order.add(group); PdfDictionary d = new PdfDictionary(); d.put(PdfName.ORDER, order); p.put(PdfName.D, d); // step 5: we close the document document.close(); }
private void processBytes(final byte[] pdfBytes, final int pageNumber) throws IOException { final PdfReader pdfReader = new PdfReader(pdfBytes); final PdfDictionary pageDictionary = pdfReader.getPageN(pageNumber); final PdfDictionary resourceDictionary = pageDictionary.getAsDict(PdfName.RESOURCES); final PdfObject contentObject = pageDictionary.get(PdfName.CONTENTS); final byte[] contentBytes = readContentBytes(contentObject); _processor.processContent(contentBytes, resourceDictionary); }
protected void startText(String text, boolean isHyperlink) { if (isTagged) { PdfDictionary markedContentProps = new PdfDictionary(); markedContentProps.put(PdfName.ACTUALTEXT, new PdfString(text, PdfObject.TEXT_UNICODE)); PdfStructureElement textTag = new PdfStructureElement(tagStack.peek(), isHyperlink ? PdfName.LINK : PdfName.TEXT); // the following method is part of the patched iText pdfContentByte.beginMarkedContentSequence(textTag, markedContentProps); } }
/** * Merge metadata from the FOP-generated PDF and a PDF we're merging into it. * Generally metadata in the merge file takes precedence over the FOP metadata, * but the "overrideMetadata" option reverses that behavior. */ private void mergeMetadata(HashMap<String, String>[] infos, PdfWriter pdfWriter, XPathContext context) throws XPathException { boolean override = getAttribBool("overrideMetadata", context, false); HashMap<String, String> toPut = new HashMap(); if (override) { toPut.putAll(infos[1]); toPut.putAll(infos[0]); } else { toPut.putAll(infos[0]); toPut.putAll(infos[1]); } PdfDictionary outInfo = pdfWriter.getInfo(); for (String key : toPut.keySet()) { // Keep iText as the producer if (key.equals("Producer")) continue; // Filter out empty values. String val = toPut.get(key).trim(); if (val.length() == 0) continue; // Add the new metadata outInfo.put(new PdfName(key), new PdfString(val, PdfObject.TEXT_UNICODE)); } }
/** * Demonstrates how to group optional content. * * @param args * no arguments needed */ public static void main(String[] args) { System.out.println("Grouping optional content"); try { // step 1 Document document = new Document(PageSize.A4, 50, 50, 50, 50); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "droidtext" + java.io.File.separator + "contentgroups.pdf")); writer.setPdfVersion(PdfWriter.VERSION_1_5); writer.setViewerPreferences(PdfWriter.PageModeUseOC); // step 3 document.open(); // step 4 PdfContentByte cb = writer.getDirectContent(); Phrase explanation = new Phrase("Layer grouping", new Font(Font.HELVETICA, 20, Font.BOLD, Color.red)); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50, 650, 0); PdfLayer l1 = new PdfLayer("Layer 1", writer); PdfLayer l2 = new PdfLayer("Layer 2", writer); PdfLayer l3 = new PdfLayer("Layer 3", writer); PdfLayerMembership m1 = new PdfLayerMembership(writer); m1.addMember(l2); m1.addMember(l3); Phrase p1 = new Phrase("Text in layer 1"); Phrase p2 = new Phrase("Text in layer 2 or layer 3"); Phrase p3 = new Phrase("Text in layer 3"); cb.beginLayer(l1); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0); cb.endLayer(); cb.beginLayer(m1); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0); cb.endLayer(); cb.beginLayer(l3); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0); cb.endLayer(); cb.sanityCheck(); PdfOCProperties p = writer.getOCProperties(); PdfArray order = new PdfArray(); order.add(l1.getRef()); PdfArray group = new PdfArray(); group.add(new PdfString("A group of two", PdfObject.TEXT_UNICODE)); group.add(l2.getRef()); group.add(l3.getRef()); order.add(group); PdfDictionary d = new PdfDictionary(); d.put(PdfName.ORDER, order); p.put(PdfName.D, d); // step 5 document.close(); } catch (Exception de) { de.printStackTrace(); } }
/** * Demonstrates how to order optional content groups. * * @param args * no arguments needed */ public static void main(String[] args) { System.out.println("Ordering optional content groups"); try { // step 1 Document document = new Document(PageSize.A4, 50, 50, 50, 50); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "droidtext" + java.io.File.separator + "orderedlayers.pdf")); writer.setPdfVersion(PdfWriter.VERSION_1_5); writer.setViewerPreferences(PdfWriter.PageModeUseOC); // step 3 document.open(); // step 4 PdfContentByte cb = writer.getDirectContent(); Phrase explanation = new Phrase("Ordered layers", new Font(Font.HELVETICA, 20, Font.BOLD, Color.red)); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50, 650, 0); PdfLayer l1 = new PdfLayer("Layer 1", writer); PdfLayer l2 = new PdfLayer("Layer 2", writer); PdfLayer l3 = new PdfLayer("Layer 3", writer); PdfLayerMembership m1 = new PdfLayerMembership(writer); m1.addMember(l2); m1.addMember(l3); Phrase p1 = new Phrase("Text in layer 1"); Phrase p2 = new Phrase("Text in layer 2 or layer 3"); Phrase p3 = new Phrase("Text in layer 3"); cb.beginLayer(l1); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0); cb.endLayer(); cb.beginLayer(m1); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0); cb.endLayer(); cb.beginLayer(l3); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0); cb.endLayer(); cb.sanityCheck(); PdfOCProperties p = writer.getOCProperties(); PdfArray order = new PdfArray(); order.add(l1.getRef()); order.add(l2.getRef()); order.add(l3.getRef()); PdfDictionary d = new PdfDictionary(); d.put(PdfName.ORDER, order); p.put(PdfName.D, d); // step 5 document.close(); } catch (Exception de) { de.printStackTrace(); } }
/** * Gets the text from a page. * @param page the page number of the page * @return a String with the content as plain text (without PDF syntax) * @throws IOException */ public String getTextFromPage(int page) throws IOException { PdfDictionary pageDic = reader.getPageN(page); PdfDictionary resourcesDic = pageDic.getAsDict(PdfName.RESOURCES); extractionProcessor.processContent(getContentBytesForPage(page), resourcesDic); return extractionProcessor.getResultantText(); }
/** * Writes information about a specific page from PdfReader to the specified * output stream. * * @since 2.1.5 * @param reader * the PdfReader to read the page content from * @param pageNum * the page number to read * @param out * the output stream to send the content to * @throws IOException */ static public void listContentStreamForPage(PdfReader reader, int pageNum, PrintWriter out) throws IOException { out.println("==============Page " + pageNum + "===================="); out.println("- - - - - Dictionary - - - - - -"); PdfDictionary pageDictionary = reader.getPageN(pageNum); out.println(getDictionaryDetail(pageDictionary)); out.println("- - - - - Content Stream - - - - - -"); RandomAccessFileOrArray f = reader.getSafeFile(); byte[] contentBytes = reader.getPageContent(pageNum, f); f.close(); InputStream is = new ByteArrayInputStream(contentBytes); int ch; while ((ch = is.read()) != -1) { out.print((char) ch); } out.println("- - - - - Text Extraction - - - - - -"); PdfTextExtractor extractor = new PdfTextExtractor(reader); String extractedText = extractor.getTextFromPage(pageNum); if (extractedText.length() != 0) out.println(extractedText); else out.println("No text found on page " + pageNum); out.println(); }
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) { PdfName fontResourceName = (PdfName)operands.get(0); float size = ((PdfNumber)operands.get(1)).floatValue(); PdfDictionary fontsDictionary = processor.resources.getAsDict(PdfName.FONT); CMapAwareDocumentFont font = new CMapAwareDocumentFont((PRIndirectReference)fontsDictionary.get(fontResourceName)); processor.gs().font = font; processor.gs().fontSize = size; }
PdfObject getColorspace() { if (gamma == 1f && !hasCHRM) { if ((colorType & 2) == 0) return PdfName.DEVICEGRAY; else return PdfName.DEVICERGB; } else { PdfArray array = new PdfArray(); PdfDictionary dic = new PdfDictionary(); if ((colorType & 2) == 0) { if (gamma == 1f) return PdfName.DEVICEGRAY; array.add(PdfName.CALGRAY); dic.put(PdfName.GAMMA, new PdfNumber(gamma)); dic.put(PdfName.WHITEPOINT, new PdfLiteral("[1 1 1]")); array.add(dic); } else { PdfObject wp = new PdfLiteral("[1 1 1]"); array.add(PdfName.CALRGB); if (gamma != 1f) { PdfArray gm = new PdfArray(); PdfNumber n = new PdfNumber(gamma); gm.add(n); gm.add(n); gm.add(n); dic.put(PdfName.GAMMA, gm); } if (hasCHRM) { float z = yW*((xG-xB)*yR-(xR-xB)*yG+(xR-xG)*yB); float YA = yR*((xG-xB)*yW-(xW-xB)*yG+(xW-xG)*yB)/z; float XA = YA*xR/yR; float ZA = YA*((1-xR)/yR-1); float YB = -yG*((xR-xB)*yW-(xW-xB)*yR+(xW-xR)*yB)/z; float XB = YB*xG/yG; float ZB = YB*((1-xG)/yG-1); float YC = yB*((xR-xG)*yW-(xW-xG)*yW+(xW-xR)*yG)/z; float XC = YC*xB/yB; float ZC = YC*((1-xB)/yB-1); float XW = XA+XB+XC; float YW = 1;//YA+YB+YC; float ZW = ZA+ZB+ZC; PdfArray wpa = new PdfArray(); wpa.add(new PdfNumber(XW)); wpa.add(new PdfNumber(YW)); wpa.add(new PdfNumber(ZW)); wp = wpa; PdfArray matrix = new PdfArray(); matrix.add(new PdfNumber(XA)); matrix.add(new PdfNumber(YA)); matrix.add(new PdfNumber(ZA)); matrix.add(new PdfNumber(XB)); matrix.add(new PdfNumber(YB)); matrix.add(new PdfNumber(ZB)); matrix.add(new PdfNumber(XC)); matrix.add(new PdfNumber(YC)); matrix.add(new PdfNumber(ZC)); dic.put(PdfName.MATRIX, matrix); } dic.put(PdfName.WHITEPOINT, wp); array.add(dic); } return array; } }