packagebravado; |
importjava.io.ByteArrayOutputStream; |
importjava.io.File; |
importjava.io.FileOutputStream; |
importjava.io.FileWriter; |
importjava.io.IOException; |
importjava.text.SimpleDateFormat; |
importorg.json.JSONArray; |
importorg.json.JSONException; |
importorg.json.JSONObject; |
importcom.itextpdf.text.Document; |
importcom.itextpdf.text.DocumentException; |
importcom.itextpdf.text.PageSize; |
importcom.itextpdf.text.Paragraph; |
importcom.itextpdf.text.Rectangle; |
importcom.itextpdf.text.pdf.PdfWriter; |
publicclassPDFGenerator { |
publicstaticvoidprocess(Documentdocument, JSONObjectjson) throwsJSONException, DocumentException { |
for (String k : json.keySet()) { |
Object object = json.get(k); |
if (object instanceofJSONArray) { |
JSONArray list = json.getJSONArray(k); |
process(document, list); |
} elseif (object instanceofJSONObject) { |
process(document, json.getJSONObject(k)); |
} else { |
document.add(newParagraph(k +''+ json.get(k))); |
} |
} |
} |
publicstaticvoidprocess(Documentdocument, JSONArrayjson) throwsJSONException, DocumentException { |
for (int x =0; x < json.length(); x++) { |
Object object = json.get(x); |
if (object instanceofJSONArray) { |
JSONArray list = json.getJSONArray(x); |
process(document, list); |
} elseif (object instanceofJSONObject) { |
process(document, json.getJSONObject(x)); |
} else { |
document.add(newParagraph(json.get(x).toString())); |
} |
} |
} |
publicstaticFilejsonTopdf(JSONObjectjson) throwsIOException, DocumentException { |
Document document =newDocument(PageSize.A4, 70, 55, 100, 55); |
File file =File.createTempFile('consulta', '.pdf'); |
FileOutputStream output =newFileOutputStream(file); |
PdfWriter writer =PdfWriter.getInstance(document, output); |
writer.setEncryption('a'.getBytes(), 'b'.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128); |
writer.createXmpMetadata(); |
writer.setBoxSize('art', newRectangle(36, 54, 559, 788)); |
document.open(); |
document.addCreationDate(); |
document.addTitle('documento'); |
document.newPage(); |
process(document, json); |
document.close(); |
return file; |
} |
} |
json.keySet() is unresolved anyfix? (Line 24) Thank you. |