Java Code Examples for org.apache.pdfbox.cos.COSName#getPDFName()
The following examples show how to use
org.apache.pdfbox.cos.COSName#getPDFName() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDResources.java From gcs with Mozilla Public License 2.0 | 6 votes |
/** * Returns a unique key for a new resource. */ private COSName createKey(COSName kind, String prefix) { COSDictionary dict = (COSDictionary)resources.getDictionaryObject(kind); if (dict == null) { return COSName.getPDFName(prefix + 1); } // find a unique key String key; int n = dict.keySet().size(); do { ++n; key = prefix + n; } while (dict.containsKey(key)); return COSName.getPDFName(key); }
Example 2
Source File: PDStructureElement.java From gcs with Mozilla Public License 2.0 | 6 votes |
/** * Removes a class name. * * @param className the class name */ public void removeClassName(String className) { if (className == null) { return; } COSName key = COSName.C; COSBase c = this.getCOSObject().getDictionaryObject(key); COSName name = COSName.getPDFName(className); if (c instanceof COSArray) { COSArray array = (COSArray) c; array.remove(name); if ((array.size() == 2) && (array.getInt(1) == 0)) { this.getCOSObject().setItem(key, array.getObject(0)); } } else { COSBase directC = c; if (c instanceof COSObject) { directC = ((COSObject) c).getObject(); } if (name.equals(directC)) { this.getCOSObject().setItem(key, null); } } }
Example 3
Source File: PDVisibleSigBuilder.java From gcs with Mozilla Public License 2.0 | 6 votes |
@Override public void createImageForm(PDResources imageFormResources, PDResources innerFormResource, PDStream imageFormStream, PDRectangle bbox, AffineTransform at, PDImageXObject img) throws IOException { PDFormXObject imageForm = new PDFormXObject(imageFormStream); imageForm.setBBox(bbox); imageForm.setMatrix(at); imageForm.setResources(imageFormResources); imageForm.setFormType(1); imageFormResources.getCOSObject().setDirect(true); COSName imageFormName = COSName.getPDFName("n2"); innerFormResource.put(imageFormName, imageForm); COSName imageName = imageFormResources.add(img, "img"); pdfStructure.setImageForm(imageForm); pdfStructure.setImageFormName(imageFormName); pdfStructure.setImageName(imageName); LOG.info("Created image form"); }
Example 4
Source File: PDAbstractContentStream.java From gcs with Mozilla Public License 2.0 | 5 votes |
protected COSName getName(PDColorSpace colorSpace) { if (colorSpace instanceof PDDeviceGray || colorSpace instanceof PDDeviceRGB || colorSpace instanceof PDDeviceCMYK) { return COSName.getPDFName(colorSpace.getName()); } else { return resources.add(colorSpace); } }
Example 5
Source File: PDPageContentStream.java From gcs with Mozilla Public License 2.0 | 5 votes |
private COSName getName(PDColorSpace colorSpace) throws IOException { if (colorSpace instanceof PDDeviceGray || colorSpace instanceof PDDeviceRGB || colorSpace instanceof PDDeviceCMYK) { return COSName.getPDFName(colorSpace.getName()); } else { return resources.add(colorSpace); } }
Example 6
Source File: PDFontDescriptor.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * This will set the font name. * * @param fontName The new name for the font. */ public void setFontName( String fontName ) { COSName name = null; if( fontName != null ) { name = COSName.getPDFName( fontName ); } dic.setItem( COSName.FONT_NAME, name ); }
Example 7
Source File: PDFontDescriptor.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * This will set the font stretch. * * @param fontStretch The new stretch for the font. */ public void setFontStretch( String fontStretch ) { COSName name = null; if( fontStretch != null ) { name = COSName.getPDFName( fontStretch ); } dic.setItem( COSName.FONT_STRETCH, name ); }
Example 8
Source File: RemoveStrikeoutComment.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
/** * <a href="https://stackoverflow.com/questions/45812696/pdfbox-delete-comment-maintain-strikethrough"> * PDFBox delete comment maintain strikethrough * </a> * <br/> * <a href="https://expirebox.com/files/3d955e6df4ca5874c38dbf92fc43b5af.pdf"> * only_fields.pdf * </a> * <a href="https://file.io/DTvqhC"> * (alternative download) * </a> * <p> * The OP only wanted the comment removed, not the strike-through. Thus, we must * not remove the annotation but merely the comment building attributes. * </p> */ @Test public void testRemoveLikeStephanImproved() throws IOException { final COSName POPUP = COSName.getPDFName("Popup"); try (InputStream resource = getClass().getResourceAsStream("only_fields.pdf")) { PDDocument document = Loader.loadPDF(resource); List<PDAnnotation> annotations = new ArrayList<>(); PDPageTree allPages = document.getDocumentCatalog().getPages(); List<COSObjectable> objectsToRemove = new ArrayList<>(); for (int i = 0; i < allPages.getCount(); i++) { PDPage page = allPages.get(i); annotations = page.getAnnotations(); for (PDAnnotation annotation : annotations) { if ("StrikeOut".equals(annotation.getSubtype())) { COSDictionary annotationDict = annotation.getCOSObject(); COSBase popup = annotationDict.getItem(POPUP); annotationDict.removeItem(POPUP); annotationDict.removeItem(COSName.CONTENTS); // plain text comment annotationDict.removeItem(COSName.RC); // rich text comment annotationDict.removeItem(COSName.T); // author if (popup != null) objectsToRemove.add(popup); } } annotations.removeAll(objectsToRemove); } document.save(new File(RESULT_FOLDER, "only_fields-removeImproved.pdf")); } }
Example 9
Source File: BaseParser.java From gcs with Mozilla Public License 2.0 | 4 votes |
/** * This will parse a PDF name from the stream. * * @return The parsed PDF name. * @throws IOException If there is an error reading from the stream. */ protected COSName parseCOSName() throws IOException { readExpectedChar('/'); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int c = seqSource.read(); while (c != -1) { int ch = c; if (ch == '#') { int ch1 = seqSource.read(); int ch2 = seqSource.read(); // Prior to PDF v1.2, the # was not a special character. Also, // it has been observed that various PDF tools do not follow the // spec with respect to the # escape, even though they report // PDF versions of 1.2 or later. The solution here is that we // interpret the # as an escape only when it is followed by two // valid hex digits. if (isHexDigit((char)ch1) && isHexDigit((char)ch2)) { String hex = Character.toString((char) ch1) + (char) ch2; try { buffer.write(Integer.parseInt(hex, 16)); } catch (NumberFormatException e) { throw new IOException("Error: expected hex digit, actual='" + hex + "'", e); } c = seqSource.read(); } else { // check for premature EOF if (ch2 == -1 || ch1 == -1) { LOG.error("Premature EOF in BaseParser#parseCOSName"); c = -1; break; } seqSource.unread(ch2); c = ch1; buffer.write(ch); } } else if (isEndOfName(ch)) { break; } else { buffer.write(ch); c = seqSource.read(); } } if (c != -1) { seqSource.unread(c); } byte[] bytes = buffer.toByteArray(); String string; if (isValidUTF8(bytes)) { string = new String(buffer.toByteArray(), Charsets.UTF_8); } else { // some malformed PDFs don't use UTF-8 see PDFBOX-3347 string = new String(buffer.toByteArray(), Charsets.WINDOWS_1252); } return COSName.getPDFName(string); }
Example 10
Source File: PDDeviceColorSpace.java From gcs with Mozilla Public License 2.0 | 4 votes |
@Override public COSBase getCOSObject() { return COSName.getPDFName(getName()); }
Example 11
Source File: ZapfDingbatsEncoding.java From gcs with Mozilla Public License 2.0 | 4 votes |
@Override public COSBase getCOSObject() { return COSName.getPDFName("ZapfDingbatsEncoding"); }
Example 12
Source File: SymbolEncoding.java From gcs with Mozilla Public License 2.0 | 4 votes |
@Override public COSBase getCOSObject() { return COSName.getPDFName("SymbolEncoding"); }
Example 13
Source File: RenderType3Character.java From testarea-pdfbox2 with Apache License 2.0 | 4 votes |
/** * <a href="http://stackoverflow.com/questions/42032729/render-type3-font-character-as-image-using-pdfbox"> * Render Type3 font character as image using PDFBox * </a> * <br/> * <a href="https://drive.google.com/file/d/0B0f6X4SAMh2KRDJTbm4tb3E1a1U/view"> * 4700198773.pdf * </a> * from * <a href="http://stackoverflow.com/questions/37754112/extract-text-with-custom-font-result-non-readble"> * extract text with custom font result non readble * </a> * <p> * This test shows how one can render individual Type 3 font glyphs as bitmaps. * Unfortunately PDFBox out-of-the-box does not provide a class to render contents * of arbitrary XObjects, merely for rendering pages; thus, we simply create a page * with the glyph in question and render that page. * </p> * <p> * As the OP did not provide a sample PDF, we simply use one from another * stackoverflow question. There obviously might remain issues with the * OP's files. * </p> */ @Test public void testRender4700198773() throws IOException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Method PDPageContentStreamWrite = PDPageContentStream.class.getSuperclass().getDeclaredMethod("write", String.class); PDPageContentStreamWrite.setAccessible(true); try ( InputStream resource = getClass().getResourceAsStream("4700198773.pdf")) { PDDocument document = Loader.loadPDF(resource); PDPage page = document.getPage(0); PDResources pageResources = page.getResources(); COSName f1Name = COSName.getPDFName("F1"); PDType3Font fontF1 = (PDType3Font) pageResources.getFont(f1Name); Map<String, Integer> f1NameToCode = fontF1.getEncoding().getNameToCodeMap(); COSDictionary charProcsDictionary = fontF1.getCharProcs(); for (COSName key : charProcsDictionary.keySet()) { COSStream stream = (COSStream) charProcsDictionary.getDictionaryObject(key); PDType3CharProc charProc = new PDType3CharProc(fontF1, stream); PDRectangle bbox = charProc.getGlyphBBox(); if (bbox == null) bbox = charProc.getBBox(); Integer code = f1NameToCode.get(key.getName()); if (code != null) { PDDocument charDocument = new PDDocument(); PDPage charPage = new PDPage(bbox); charDocument.addPage(charPage); charPage.setResources(pageResources); PDPageContentStream charContentStream = new PDPageContentStream(charDocument, charPage); charContentStream.beginText(); charContentStream.setFont(fontF1, bbox.getHeight()); // charContentStream.write(String.format("<%2X> Tj\n", code).getBytes()); PDPageContentStreamWrite.invoke(charContentStream, String.format("<%2X> Tj\n", code)); charContentStream.endText(); charContentStream.close(); File result = new File(RESULT_FOLDER, String.format("4700198773-%s-%s.png", key.getName(), code)); PDFRenderer renderer = new PDFRenderer(charDocument); BufferedImage image = renderer.renderImageWithDPI(0, 96); ImageIO.write(image, "PNG", result); charDocument.save(new File(RESULT_FOLDER, String.format("4700198773-%s-%s.pdf", key.getName(), code))); charDocument.close(); } } } }
Example 14
Source File: RenderType3Character.java From testarea-pdfbox2 with Apache License 2.0 | 4 votes |
/** * <a href="http://stackoverflow.com/questions/42032729/render-type3-font-character-as-image-using-pdfbox"> * Render Type3 font character as image using PDFBox * </a> * <br/> * <a href="https://drive.google.com/file/d/0B0f6X4SAMh2KRDJTbm4tb3E1a1U/view"> * 4700198773.pdf * </a> * from * <a href="http://stackoverflow.com/questions/37754112/extract-text-with-custom-font-result-non-readble"> * extract text with custom font result non readble * </a> * <p> * This test shows how one can render individual Type 3 font glyphs as bitmaps. * Unfortunately PDFBox out-of-the-box does not provide a class to render contents * of arbitrary XObjects, merely for rendering pages; thus, we simply create a page * with the glyph in question and render that page. * </p> * <p> * As the OP did not provide a sample PDF, we simply use one from another * stackoverflow question. There obviously might remain issues with the * OP's files. * </p> */ @Test public void testRenderSdnList() throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { Method PDPageContentStreamWrite = PDPageContentStream.class.getSuperclass().getDeclaredMethod("write", String.class); PDPageContentStreamWrite.setAccessible(true); try ( InputStream resource = getClass().getResourceAsStream("sdnlist.pdf")) { PDDocument document = Loader.loadPDF(resource); PDPage page = document.getPage(1); PDResources pageResources = page.getResources(); COSName f1Name = COSName.getPDFName("R144"); PDType3Font fontF1 = (PDType3Font) pageResources.getFont(f1Name); Map<String, Integer> f1NameToCode = fontF1.getEncoding().getNameToCodeMap(); COSDictionary charProcsDictionary = fontF1.getCharProcs(); for (COSName key : charProcsDictionary.keySet()) { COSStream stream = (COSStream) charProcsDictionary.getDictionaryObject(key); PDType3CharProc charProc = new PDType3CharProc(fontF1, stream); PDRectangle bbox = charProc.getGlyphBBox(); if (bbox == null) bbox = charProc.getBBox(); Integer code = f1NameToCode.get(key.getName()); if (code != null) { PDDocument charDocument = new PDDocument(); PDPage charPage = new PDPage(bbox); charDocument.addPage(charPage); charPage.setResources(pageResources); PDPageContentStream charContentStream = new PDPageContentStream(charDocument, charPage); charContentStream.beginText(); charContentStream.setFont(fontF1, bbox.getHeight()); //charContentStream.getOutputStream().write(String.format("<%2X> Tj\n", code).getBytes()); PDPageContentStreamWrite.invoke(charContentStream, String.format("<%2X> Tj\n", code)); charContentStream.endText(); charContentStream.close(); File result = new File(RESULT_FOLDER, String.format("sdnlist-%s-%s.png", key.getName(), code)); PDFRenderer renderer = new PDFRenderer(charDocument); BufferedImage image = renderer.renderImageWithDPI(0, 96); ImageIO.write(image, "PNG", result); charDocument.save(new File(RESULT_FOLDER, String.format("sdnlist-%s-%s.pdf", key.getName(), code))); charDocument.close(); } } } }