blocks/dump_block_properties
Dump Block information from an existing PDF document.
Download Java Code Switch to PHP Code Show Output Show Input (announcement_blocks.pdf)
/*
* Dump block information from an existing PDF document.
*
* Required software: PDFlib+PDI/PPS 9
* Required data: PDF input file
*/
package com.pdflib.cookbook.pdflib.blocks;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
import java.util.Locale;
public class dump_block_properties {
static String getProperty(pdflib p, int doc, String info)
throws PDFlibException {
String output = "";
String objtype = p.pcos_get_string(doc, "type:" + info);
if (objtype.equals("name") || objtype.equals("string")
|| objtype.equals("boolean")) {
output += p.pcos_get_string(doc, info);
} else if (objtype.equals("number")) {
output += String.format("%.2f", p.pcos_get_number(doc, info));
} else if (objtype.equals("array")) {
output += getArrayPropertyAsString(p, doc, info);
} else if (objtype.equals("dict")) {
output += getDictPropertyAsString(p, doc, info);
} else {
output += "[ " + info + "unknown type (" + objtype + ")]";
}
return output;
}
static String getDictPropertyAsString(pdflib p, int doc, String path)
throws PDFlibException {
String output = "";
int length = (int) p.pcos_get_number(doc, "length:" + path);
for (int i = 0; i < length; i++) {
String info = path + "[" + i + "].key";
String key = p.pcos_get_string(doc, info);
output += key + " [";
info = path + "[" + i + "]";
output += getProperty(p, doc, info);
output += "] ";
}
return "[ " + output + "]";
}
static String getArrayPropertyAsString(pdflib p, int doc, String path)
throws PDFlibException {
String output = "";
int length = (int) p.pcos_get_number(doc, "length:" + path);
for (int i = 0; i < length; i++) {
String info = path + "[" + i + "]";
output += getProperty(p, doc, info);
output += " ";
}
return "[ " + output + "]";
}
public static void main(String argv[]) {
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
String pdfinput = "announcement_blocks.pdf";
String docoptlist = "requiredmode=minimum";
pdflib p = null;
int exitcode = 0;
Locale.setDefault(Locale.US);
try {
String title = "Block properties for \"" + pdfinput + "\":";
System.out.println(title);
System.out.println();
p = new pdflib();
p.set_option("searchpath={" + searchpath + "}");
/* This means we must check return values of load_font() etc. */
p.set_option("errorpolicy=return");
/*
* We do not create any output document, so no call to
* begin_document() is required.
*/
/* Open the input document */
int doc = p.open_pdi_document(pdfinput, docoptlist);
if (doc == -1) {
throw new Exception("Error: " + p.get_errmsg());
}
int pagecount = (int) p.pcos_get_number(doc, "length:pages");
for (int page = 0; page < pagecount; page++) {
int count = (int) p.pcos_get_number(doc,
"length:pages[" + page + "]/blocks");
for (int i = 0; i < count; i++) {
String info = "type:pages[" + page + "]/blocks[" + i + "]";
String objtype = p.pcos_get_string(doc, info);
info = "pages[" + page + "]/blocks[" + i + "].key";
String key = p.pcos_get_string(doc, info);
System.out.println(key + ":");
String pcospath = "pages[" + page + "]/blocks/" + key;
int length = (int) p.pcos_get_number(doc,
"length:" + pcospath);
for (int j = 0; j < length; j++) {
info = pcospath + "[" + j + "].key";
key = p.pcos_get_string(doc, info);
String blockproppath = pcospath + "/" + key;
objtype = p.pcos_get_string(doc,
"type:" + blockproppath);
System.out.print(" " + objtype + "(" + blockproppath
+ "\t" + key + "\t");
System.out.println(getProperty(p, doc, blockproppath) + ")");
}
System.out.println();
}
}
p.close_pdi_document(doc);
} catch (PDFlibException e) {
System.err.println("PDFlib exception occurred:");
System.err.println("[" + e.get_errnum() + "] " + e.get_apiname()
+ ": " + e.get_errmsg());
exitcode = 1;
} catch (Exception e) {
System.err.println(e);
exitcode = 1;
} finally {
if (p != null) {
p.delete();
}
System.exit(exitcode);
}
}
}