本文整理汇总了Java中org.apache.pdfbox.cos.COSDictionary.getDictionaryObject方法的典型用法代码示例。如果您正苦于以下问题:Java COSDictionary.getDictionaryObject方法的具体用法?Java COSDictionary.getDictionaryObject怎么用?Java COSDictionary.getDictionaryObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pdfbox.cos.COSDictionary
的用法示例。
在下文中一共展示了COSDictionary.getDictionaryObject方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRenderSdnList
import org.apache.pdfbox.cos.COSDictionary; //导入方法依赖的package包/类
/**
* <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
{
try ( InputStream resource = getClass().getResourceAsStream("sdnlist.pdf"))
{
PDDocument document = PDDocument.load(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.getOutput().write(String.format("<%2X> Tj\n", code).getBytes());
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();
}
}
}
}
示例2: testRender4700198773
import org.apache.pdfbox.cos.COSDictionary; //导入方法依赖的package包/类
/**
* <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
{
try ( InputStream resource = getClass().getResourceAsStream("4700198773.pdf"))
{
PDDocument document = PDDocument.load(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.getOutput().write(String.format("<%2X> Tj\n", code).getBytes());
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();
}
}
}
}
示例3: validate
import org.apache.pdfbox.cos.COSDictionary; //导入方法依赖的package包/类
/**
* Validates the PDF file specified in the constructor
* @return PDFDocumentInfo structure
**/
public PDFDocumentInfo validate() throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException {
String infoString = null;
PDDocument document = null;
try {
document = PDDocument.load(new File(path));
COSDictionary trailer = document.getDocument().getTrailer();
COSDictionary root = (COSDictionary) trailer.getDictionaryObject(COSName.ROOT);
COSDictionary acroForm = (COSDictionary) root.getDictionaryObject(COSName.ACRO_FORM);
if (acroForm == null) {
return doc;
}
COSArray fields = (COSArray) acroForm.getDictionaryObject(COSName.FIELDS);
for (int i = 0; i < fields.size(); i ++) {
COSDictionary field = (COSDictionary) fields.getObject(i);
COSName type = field.getCOSName(COSName.FT);
if (COSName.SIG.equals(type)) {
COSDictionary sig = (COSDictionary) field.getDictionaryObject(COSName.V);
if (sig != null) {
getSignatureInfo(sig);
}
}
}
}
finally {
if (document != null) {
document.close();
}
}
return doc;
}
示例4: setField
import org.apache.pdfbox.cos.COSDictionary; //导入方法依赖的package包/类
public static void setField(PDDocument _pdfDocument, String name, String value) throws IOException
{
PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField(name);
COSDictionary dict = ((PDField) field).getDictionary();
COSString defaultAppearance = (COSString) dict
.getDictionaryObject(COSName.DA);
if (defaultAppearance != null)
{
dict.setString(COSName.DA, "/Helv 10 Tf 0 g");
if (name.equalsIgnoreCase("Field1")) {
dict.setString(COSName.DA, "/Helv 12 Tf 0 g");
}
}
if (field instanceof PDTextbox)
{
field = new PDTextbox(acroForm, dict);
((PDField) field).setValue(value);
}
}
示例5: setFieldBold
import org.apache.pdfbox.cos.COSDictionary; //导入方法依赖的package包/类
public static void setFieldBold(PDDocument _pdfDocument, String name, String value) throws IOException
{
PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField(name);
COSDictionary dict = ((PDField) field).getDictionary();
COSString defaultAppearance = (COSString) dict
.getDictionaryObject(COSName.DA);
if (defaultAppearance != null)
{
dict.setString(COSName.DA, "/Helv 10 Tf 2 Tr .5 w 0 g");
if (name.equalsIgnoreCase("Field1")) {
dict.setString(COSName.DA, "/Helv 12 Tf 0 g");
}
}
if (field instanceof PDTextbox)
{
field = new PDTextbox(acroForm, dict);
((PDField) field).setValue(value);
}
}
示例6: asDictionary
import org.apache.pdfbox.cos.COSDictionary; //导入方法依赖的package包/类
COSDictionary asDictionary(COSDictionary dictionary, COSName name)
{
COSBase object = dictionary.getDictionaryObject(name);
return object instanceof COSDictionary ? (COSDictionary) object : null;
}
示例7: getSignatures
import org.apache.pdfbox.cos.COSDictionary; //导入方法依赖的package包/类
private List<PdfSignatureOrDocTimestampInfo> getSignatures(CertificatePool validationCertPool, byte[] originalBytes) {
List<PdfSignatureOrDocTimestampInfo> signatures = new ArrayList<PdfSignatureOrDocTimestampInfo>();
PDDocument doc = null;
try {
doc = PDDocument.load(originalBytes);
List<PDSignature> pdSignatures = doc.getSignatureDictionaries();
if (Utils.isCollectionNotEmpty(pdSignatures)) {
LOG.debug("{} signature(s) found", pdSignatures.size());
PdfDict catalog = new PdfBoxDict(doc.getDocumentCatalog().getCOSObject(), doc);
PdfDssDict dssDictionary = PdfDssDict.extract(catalog);
for (PDSignature signature : pdSignatures) {
String subFilter = signature.getSubFilter();
COSDictionary dict = signature.getCOSObject();
COSString item = (COSString) dict.getDictionaryObject(COSName.CONTENTS);
byte[] cms = item.getBytes();
byte[] cmsWithByteRange = signature.getContents(originalBytes);
if (!Arrays.equals(cmsWithByteRange, cms)) {
LOG.warn("The byte range doesn't match found /Content value!");
}
if (Utils.isStringEmpty(subFilter) || Utils.isArrayEmpty(cms)) {
LOG.warn("Wrong signature with empty subfilter or cms.");
continue;
}
byte[] signedContent = signature.getSignedContent(originalBytes);
int[] byteRange = signature.getByteRange();
PdfDict signatureDictionary = new PdfBoxDict(signature.getCOSObject(), doc);
PdfSignatureOrDocTimestampInfo signatureInfo = null;
if (PdfBoxDocTimeStampService.SUB_FILTER_ETSI_RFC3161.getName().equals(subFilter)) {
boolean isArchiveTimestamp = false;
// LT or LTA
if (dssDictionary != null) {
// check is DSS dictionary already exist
if (isDSSDictionaryPresentInPreviousRevision(getOriginalBytes(byteRange, signedContent))) {
isArchiveTimestamp = true;
}
}
signatureInfo = new PdfBoxDocTimestampInfo(validationCertPool, signature, signatureDictionary, dssDictionary, cms, signedContent,
isArchiveTimestamp);
} else {
signatureInfo = new PdfBoxSignatureInfo(validationCertPool, signature, signatureDictionary, dssDictionary, cms, signedContent);
}
if (signatureInfo != null) {
signatures.add(signatureInfo);
}
}
Collections.sort(signatures, new PdfSignatureOrDocTimestampInfoComparator());
linkSignatures(signatures);
for (PdfSignatureOrDocTimestampInfo sig : signatures) {
LOG.debug("Signature " + sig.uniqueId() + " found with byteRange " + Arrays.toString(sig.getSignatureByteRange()) + " ("
+ sig.getSubFilter() + ")");
}
}
} catch (Exception e) {
LOG.warn("Cannot analyze signatures : " + e.getMessage(), e);
} finally {
Utils.closeQuietly(doc);
}
return signatures;
}
示例8: analysePdfObjects
import org.apache.pdfbox.cos.COSDictionary; //导入方法依赖的package包/类
/****************************************************************************
* Analysis PDF-Objects
*
* @param file
* @return: nothings, puts out the information in a file
* @throws IOException
*/
public static void analysePdfObjects(File file) throws IOException {
PrintWriter pdfboxanalysis = new PrintWriter(new FileWriter("D://pdfboxanalysis.txt"));
pdfboxanalysis.println(file.toString());
PDDocument pdf = PDDocument.load(file);
PDDocumentInformation info = pdf.getDocumentInformation();
COSDictionary dict = info.getDictionary();
Collection<COSBase> l = dict.getValues();
COSArray mediaBox = (COSArray) dict.getDictionaryObject("MediaBox");
System.out.println("MediaBox: " + mediaBox);
COSDictionary trailer = pdf.getDocument().getTrailer();
System.out.println("Trailer:" + trailer);
if (pdf.isEncrypted()) { // this actually works easily
System.out.println("Encrypted");
}
for (Object o : l) {
// System.out.println(o.toString());
pdfboxanalysis.println(o.toString());
}
PDDocumentCatalog cat = pdf.getDocumentCatalog();
@SuppressWarnings("unchecked")
List<PDPage> lp = cat.getAllPages();
pdfboxanalysis.println("# Pages: " + lp.size());
PDPage page = lp.get(4);
pdfboxanalysis.println("Page: " + page);
pdfboxanalysis.println("\tCropBox: " + page.getCropBox());
pdfboxanalysis.println("\tMediaBox: " + page.getMediaBox());
pdfboxanalysis.println("\tResources: " + page.getResources());
pdfboxanalysis.println("\tRotation: " + page.getRotation());
pdfboxanalysis.println("\tArtBox: " + page.getArtBox());
pdfboxanalysis.println("\tBleedBox: " + page.getBleedBox());
pdfboxanalysis.println("\tContents: " + page.getContents());
pdfboxanalysis.println("\tTrimBox: " + page.getTrimBox());
List<PDAnnotation> la = page.getAnnotations();
pdfboxanalysis.println("\t# Annotations: " + la.size());
pdfboxanalysis.close();
}
示例9: test
import org.apache.pdfbox.cos.COSDictionary; //导入方法依赖的package包/类
public void test() {
String filePath = "";
PDDocument document;
try {
document = PDDocument.load(new File(filePath));
List<SignatureInformations> result = null;
List<SignatureInformations> results = new ArrayList<SignatureInformations>();
for (PDSignature sig : document.getSignatureDictionaries()) {
COSDictionary sigDict = sig.getCOSObject();
COSString contents = (COSString) sigDict.getDictionaryObject(COSName.CONTENTS);
FileInputStream fis = new FileInputStream(filePath);
byte[] buf = null;
try {
buf = sig.getSignedContent(fis);
} finally {
fis.close();
}
CAdESChecker checker = new CAdESChecker();
result = checker.checkDetattachedSignature(buf, contents.getBytes());
if (result == null || result.isEmpty()) {
//Erro
}
results.addAll(checker.getSignaturesInfo());
}
for (SignatureInformations sis : results){
for (BasicCertificate bc : sis.getSignersBasicCertificates()){
if (bc.hasCertificatePF()){
System.out.println(bc.getICPBRCertificatePF().getCPF());
}
if (bc.hasCertificatePJ()){
System.out.println(bc.getICPBRCertificatePJ().getCNPJ());
System.out.println(bc.getICPBRCertificatePJ().getResponsibleCPF());
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}