For example,
{
"appId": "wxa033f083bd79132d",
"timestamp": 1657699905,
"nonceStr": "SKIe2Ce352",
"signature": "20fbc391ed6ed78a3a267c31526239110926a8d1",
"jsApiList": [
"updateAppMessageShareData",
"updateTimelineShareData"
]
}
I want to generate pdf file with formatted json,like this:
But I can only generate something like this:
I've tried making it rich text and displaying it inside the pdf, but that doesn't seem to work. And my code
public void testPDF() throws IOException {
String json = "{\"appId\":\"wxa033f083bd79132d\",\"timestamp\":1657699905,\"nonceStr\":\"SKIe2Ce352\",\"signature\":\"20fbc391ed6ed78a3a267c31526239110926a8d1\",\"jsApiList\":[\"updateAppMessageShareData\",\"updateTimelineShareData\"]}";
JSONObject jsonObject = JSON.parseObject(json);
PDDocument document = new PDDocument();
PDPage pdPage = new PDPage();
document.addPage(pdPage);
PDPageContentStream contentStream = new PDPageContentStream(document, pdPage);
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 14);
contentStream.newLineAtOffset(25, 500);
contentStream.setLeading(14.5f);
contentStream.showText("{");
contentStream.newLine();
jsonObject.forEach((key, value) -> {
try {
contentStream.showText(String.format("\"%s\": %s ", key, value));
contentStream.newLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
contentStream.showText("}");
contentStream.newLine();
contentStream.endText();
contentStream.close();
document.save(new File("D:\\new-doc-text.pdf"));
document.close();
}