-2

I am trying to create/maintain CRUD a PDF letter template using a PDFBox open source Java PDF Generation Library. The letter would have the following section has represented in my POJO class.

private byte[] fileContent;
private String heading;
private String returnAddress;
private Date issueDate;
private String recipientName;
private String recipientAddress;
private String salutation;
private String title;
private String body;
private String closingText;
private String signatureLine;
private String companyLine1;
private String companyLine2;

My application is a spring boot web service, which allows the end user to view,edit and create new pdf letter. Based on the documentation and tutorials I have seen online, I cannot create separate sections or content in the PDF org.apache.pdfbox.pdmodel.PDPage using key-value. So, I can retrieve the content in sections using the keys. Please, any ideas how to achieve this separation of the sections of the letters, using key-value pair for effective CRUD operation on the PDF Doc.

I am also considering creating a MongoDB collection called Letter, with document containing the the different sections of the letter as keys (fields) in the MongoDB document. Then, may have another field holding the full PDF document as a byte stream. I thought this may be a better design than saving the whole document and then parsing or traversing using substring or regex etc

Any design thoughts or implementation thoughts or references or PDFBox Sample code for similar feature e.g. creating and reading PDF content using key-value pair, will be appreciated. Thanks.

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
Mega
  • 1,304
  • 5
  • 22
  • 37
  • 1
    Your question is not about iText, please remove the tag. – Amedee Van Gasse Jun 28 '16 at 10:38
  • Both, a developer with iText experience would be able to contribute. Besides, I only added it in the tag NOT the message body or title. BTW, is why it was down voted? – Mega Jun 28 '16 at 11:39
  • Of course: a developer with iText experience could explain how to meet your requirement with iText, but such an answer would be downvoted because you're explicitly asking for a PdfBox solution. Expecting that an iText developer would give a PdfBox answer is strange. It's as if you would buy a Peugeot in one garage and then go to another garage where they sell Mercedes asking for support. You'd be sent to the Peugeot garage. Note that I voted to close your question because it is too broad. I think the question is down-voted for the same reason. – Bruno Lowagie Jun 28 '16 at 12:02
  • There's a [How to ask a good question?](http://stackoverflow.com/help/how-to-ask) in the FAQ. You broke some of the rules, for instance: you added the iText tag, which isn't relevant to your question. Also: your question isn't a problem that can be reproduced in an unambiguous way. I could think of three different answers if this were an iText question, but I don't know which answer would be most appropriate in your case because your question isn't specific enough. – Bruno Lowagie Jun 28 '16 at 12:09
  • 1
    Finally, you know that the iText community is bigger than the PdfBox community. You know that there are people who are paid to answer questions about iText. Adding iText as a tag could be interpreted as a ruse to get an answer from people who are paid to answer while you have no intention whatsoever to use iText. That would be foul play, wouldn't it? – Bruno Lowagie Jun 28 '16 at 12:11
  • Why are you presumptuous and hostile. So quick to judge and cast down vote, rather than post supportive comments. IT and software development community is not meant to be hostile. Well, I have found a solution that works and I will post to help others. – Mega Jun 30 '16 at 15:52

1 Answers1

0

I executed the following steps to resolve my problem:

  1. User can create pdf template using a pdf editor like Adobe Pro
  2. Then, the user would assign the sections of the letter to keys/placeholders in the Adobe Pro DC tool.
  3. My service endpoint was invoked via Postman to load the PDF to MongoDB from my local machine
  4. I used another endpoint to find the newly inserted document and read the various letter sections as follows:

GridFSDBFile customerLetter = gridFsTemplate.findOne(query);

InputStream pdfInputStream= customerLetter.getInputStream();

org.apache.pdfbox.pdmodel.PDDocument pdDocument=PDDocument.load(pdfInputStream);

    org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm letterPage = pdDocument.getDocumentCatalog().getAcroForm();

    System.out.println("1  >>>>>>>> "+ letterPage.getField("customerName").getValue());
    System.out.println("2  >>>>>>>> "+ letterPage.getField("customerAddressLine1").getValue());

 Hope this helps someone. 
Mega
  • 1,304
  • 5
  • 22
  • 37
  • I didn't really understand the question, and don't really understand the answer. (And note that none of the PDFBox regulars participated) About your answer: I thought you wanted to write to a PDF and now you're reading? (I didn't downvote but voted close as "too broad", because it sounded more like a strategy/opinion question). Re "IT and software development community is not meant to be hostile" I agree with that, feel free to ask further PDFBox questions on SO or in the PDFBox mailing list. The best questions here are those that are the most specific. – Tilman Hausherr Jun 30 '16 at 19:38
  • The story required a read-write operation on the PDF file. The logic is similar for both. The fact that I was able to create a PDF in an offline editor, with key-value place holders meant I could later update the PDF sections using the keyS as accessors to the various sections of the PDF. I am sure you understood me a bit! Have a nice weekend. – Mega Jun 30 '16 at 20:12