(Java) Extract PDF from JSON
Demonstrates how to extract a PDF file contained within JSON. The file is represented as a base64 string within the JSON. Note: This example can extract any type of file, not just a PDF file.
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
CkJsonObject json = new CkJsonObject();
// Load the JSON.
boolean success = json.LoadFile("qa_data/json/JSR5U.json");
if (success != true) {
System.out.println(json.lastErrorText());
return;
}
// The JSON we loaded contains this:
// {
// ...
// ...
// "data": {
// "content": "JVBERi0xLjQ..."
// }
// ...
// ...
// }
CkStringBuilder sb = new CkStringBuilder();
json.StringOfSb("data.content",sb);
CkBinData bd = new CkBinData();
bd.AppendEncodedSb(sb,"base64");
success = bd.WriteFile("qa_output/a0015.pdf");
}
}
|