1
votes

I am using Apache PDFBox(version : 2.0.8) to generate a PDF document from my .jspx page.

In my .jspx page form, there are many fields, so i decided to arrange all the fields as 2 column layout. So, I need suggestion to achieve the layout using PDFbox.

// Sample Code snippet I have used to generate PDF is as below.

   PDDocument document = new PDDocument();
   PDPage page = new PDPage();
   document.addPage(page);
   PDFont font = getFontDef();
   PDPageContentStream contentStream =new PDPageContentStream(document, page);
   contentStream.beginText();
   contentStream.showText("Name: " );
   contentStream.setFont(getFontDef().COURIER ,15);
   contentStream.showText ("Rajeev");
   contentStream.showText("Address: " +"BNG");
   contentStream.newLine();
   contentStream.showText("State: " +"KAR");
   contentStream.showText("Country: " +"INDIA");
   contentStream.endText();
   contentStream.close();

I need to show label(i.e. Name, Address,State and Country) in bold font and Value(i.e. Rajeev, BNG,KAR,IND respectively) as normal font.
To get bold font for the label, I have tried as shown below

contentStream.setFont(getFontDef().COURIER ,15);

and it is working , but I have to add the above line before each label field. is there any better approaches I can use to set bold font for all the labels?

I am also facing some issue in aligning these form fields into two column layout. Ex

Name: Rajeev      Address: BNG
State: KAR      Country: IND

i.e. Name and Address in first line and Name should be in first column and Address should be in second column. Similarly, State and country in second line and State in first column and country is in second column.

1
Please create a separate stack overflow question for each item you ask. You in particular appear to ask a how to make some text output bold and b how to align outputs. - mkl

1 Answers

2
votes

To arrange text in columns, you can use the newLineAtOffset to move horizontally, not or at least not only vertically.

To switch back and forth between bold and normal type faces, you usually do indeed have to set and reset the font again and again. Alternatively you could first draw all bold text and then all normal text (or the other way around), but then you'd have to use more newLineAtOffset calls to jump around. Furthermore, some PDF viewers might follow your jumps when you try to select and copy text.

PDFont fontNormal = PDType1Font.HELVETICA;
PDFont fontBold = PDType1Font.HELVETICA_BOLD;

PDPageContentStream contentStream =new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.newLineAtOffset(100, 600);
contentStream.setFont(fontBold, 15);
contentStream.showText("Name: ");
contentStream.setFont(fontNormal, 15);
contentStream.showText ("Rajeev");
contentStream.newLineAtOffset(200, 00);
contentStream.setFont(fontBold, 15);
contentStream.showText("Address: " );
contentStream.setFont(fontNormal, 15);
contentStream.showText ("BNG");
contentStream.newLineAtOffset(-200, -20);
contentStream.setFont(fontBold, 15);
contentStream.showText("State: " );
contentStream.setFont(fontNormal, 15);
contentStream.showText ("KAR");
contentStream.newLineAtOffset(200, 00);
contentStream.setFont(fontBold, 15);
contentStream.showText("Country: " );
contentStream.setFont(fontNormal, 15);
contentStream.showText ("INDIA");
contentStream.endText();
contentStream.close();

(ArrangeText test testArrangeTextForUser2967784)

I don't know what your method getFontDef() returns, so I chose fixed fonts for bold and normal text here.

The result:

Screenshot