1

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.

Community
  • 1
  • 1
user2967784
  • 33
  • 1
  • 10
  • 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 Feb 21 '18 at 10:29

1 Answers1

2

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

mkl
  • 90,588
  • 15
  • 125
  • 265
  • Hi mkl, thank you for the suggestion. getFontDef() is getter method which returns me font and the declaration is as follows PDType1Font fontDef = PDType1Font.COURIER; how are you calculating value for contentStream.newLineAtOffset(200, 00);. I have more than 100 fields in the form. How can I decide the value? – user2967784 Feb 21 '18 at 13:05
  • *"How can I decide the value"* - Essentially you have to know what layout you want. If the field names and values then probably are too long, you'll have to either choose a smaller font size or break lines in each column. For line breaking you may want to do something like [here](https://stackoverflow.com/a/19683618/1729265), merely limited to the width of the column, not of the page. – mkl Feb 21 '18 at 13:34