PeopleCode Format Style PDF Example

Continuing our series of tutorials that explains how to generate PDF document from PeopleSoft applications; in this post we will see how to change the font style from PeopleSoft applications while creating PDF files.Varying the font style on generated PDF is a common requirement whether you want to add a Heading, Create a footnote and so on. It will be handy to have a working PeopleCode example that supplements your PDF generation task by helping you on changing font style on created PDFs. In this post, we will aim to provide the reader, the various options available in iText to specify font styles.

We begin with creating a Document and PdfWriter object, and keep the Document open to add some style elements from PeopleCode later on. You can start with writing some code through Application Designer as shown below:-
Local JavaObject &Obj_FormatPDFText_l = CreateJavaObject("com.lowagie.text.Document");
Local JavaObject &obj_writePDFoutput_l = GetJavaClass("com.lowagie.text.pdf.PdfWriter").getInstance(&Obj_FormatPDFText_l, CreateJavaObject("java.io.FileOutputStream", "FormatPDFTextFromPeopleCode.pdf", True));
&Obj_FormatPDFText_l.open();

As in the case of our Java Example for formatting PDF document, we will create a Font object from PeopleCode for our styling needs.
Local JavaObject &Obj_fontobject_l = CreateJavaObject("com.lowagie.text.Font");

In a short while from now, we will be attaching the Font object with the Paragraph object . And in order to add the Paragraph Object to Document object we will be using the wrapper class approach as outlined in our hyperlinks in PDF document example. Make sure that you study that tutorial, to get a feel on what this wrapper class method is all about.

To set a text style to Bold from PeopleCode, use the example as shown below
&Obj_fontobject_l.setStyle("bold");
&Obj_para_temp = CreateJavaObject("com.lowagie.text.Paragraph", "Filler Text from PeopleCode", &Obj_fontobject_l);
&Obj_FormatPDFText_l = &obj_reflection_l.addParagraphtoDocument(&Obj_FormatPDFText_l, &Obj_para_temp);

To set a text style in Bold + Italic, with the font color in Blue, you can invoke both "setStyle" and "setColor" methods in the Font object, and pass the required values. The inputs to "setColor" are RGB values. To make our font in Blue, we set R and G to 0 and B to maximum (255). The PeopleCode is provided below

&Obj_fontobject_l.setStyle("italic");
&Obj_fontobject_l.setColor(0, 0, 255);
&Obj_para_temp = CreateJavaObject("com.lowagie.text.Paragraph", "Blue Bold Italic Font Example", &Obj_fontobject_l);
&Obj_FormatPDFText_l = &obj_reflection_l.addParagraphtoDocument(&Obj_FormatPDFText_l, &Obj_para_temp);

To underline a text in Green, with Bold+Italic applied as style as well, you can use the code example as shown below
&Obj_fontobject_l.setStyle("underline");
&Obj_fontobject_l.setColor(0, 255, 0);
&Obj_para_temp = CreateJavaObject("com.lowagie.text.Paragraph", "Green Underline Font Example", &Obj_fontobject_l);
&Obj_FormatPDFText_l = &obj_reflection_l.addParagraphtoDocument(&Obj_FormatPDFText_l, &Obj_para_temp);

To increase the font size of a text in PDF from PeopleCode, you can use the "setSize" method of font object. Note that this method requires a float input and you have to pass a float variable from PeopleCode to this method. If it is not done this way, you will get a "No Overload Matches" error on the screen. The example provided below, shows two scenarios: A case where the font size is increased to 30 and a case where the font size is reduced to 4.
&Obj_fontobject_l = CreateJavaObject("com.lowagie.text.Font");
Local float &FontSize = 30;
&Obj_fontobject_l.setSize(&FontSize);
&Obj_para_temp = CreateJavaObject("com.lowagie.text.Paragraph", "Increased Font Size", &Obj_fontobject_l);
&Obj_FormatPDFText_l = &obj_reflection_l.addParagraphtoDocument(&Obj_FormatPDFText_l, &Obj_para_temp);
&FontSize = 4;
&Obj_fontobject_l.setSize(&FontSize);
&Obj_para_temp = CreateJavaObject("com.lowagie.text.Paragraph", "Small Font Size", &Obj_fontobject_l);
&Obj_FormatPDFText_l = &obj_reflection_l.addParagraphtoDocument(&Obj_FormatPDFText_l, &Obj_para_temp);

So, we have seen how to change the style of a text through PeopleCode, alter its size and change its color when it is presented on a PDF document. The complete PeopleCode example to apply a formatting style to a text in a PDF document is shown below
Local JavaObject &Obj_FormatPDFText_l = CreateJavaObject("com.lowagie.text.Document");
Local JavaObject &obj_writePDFoutput_l = GetJavaClass("com.lowagie.text.pdf.PdfWriter").getInstance(&Obj_FormatPDFText_l, CreateJavaObject("java.io.FileOutputStream", "FormatPDFTextFromPeopleCode.pdf", True));
&Obj_FormatPDFText_l.open();
Local JavaObject &Obj_fontobject_l = CreateJavaObject("com.lowagie.text.Font");
Local JavaObject &obj_reflection_l = CreateJavaObject("image");
&Obj_fontobject_l.setStyle("bold");
&Obj_para_temp = CreateJavaObject("com.lowagie.text.Paragraph", "Filler Text from PeopleCode", &Obj_fontobject_l);
&Obj_FormatPDFText_l = &obj_reflection_l.addParagraphtoDocument(&Obj_FormatPDFText_l, &Obj_para_temp);
&Obj_fontobject_l.setStyle("italic");
&Obj_fontobject_l.setColor(0, 0, 255);
&Obj_para_temp = CreateJavaObject("com.lowagie.text.Paragraph", "Blue Bold Italic Font Example", &Obj_fontobject_l);
&Obj_FormatPDFText_l = &obj_reflection_l.addParagraphtoDocument(&Obj_FormatPDFText_l, &Obj_para_temp);
&Obj_fontobject_l.setStyle("underline");
&Obj_fontobject_l.setColor(0, 255, 0);
&Obj_para_temp = CreateJavaObject("com.lowagie.text.Paragraph", "Green Underline Font Example", &Obj_fontobject_l);
&Obj_FormatPDFText_l = &obj_reflection_l.addParagraphtoDocument(&Obj_FormatPDFText_l, &Obj_para_temp);
&Obj_fontobject_l = CreateJavaObject("com.lowagie.text.Font");
Local float &FontSize = 30;
&Obj_fontobject_l.setSize(&FontSize);
&Obj_para_temp = CreateJavaObject("com.lowagie.text.Paragraph", "Increased Font Size", &Obj_fontobject_l);
&Obj_FormatPDFText_l = &obj_reflection_l.addParagraphtoDocument(&Obj_FormatPDFText_l, &Obj_para_temp);
&FontSize = 4;
&Obj_fontobject_l.setSize(&FontSize);
&Obj_para_temp = CreateJavaObject("com.lowagie.text.Paragraph", "Small Font Size", &Obj_fontobject_l);
&Obj_FormatPDFText_l = &obj_reflection_l.addParagraphtoDocument(&Obj_FormatPDFText_l, &Obj_para_temp);
&Obj_FormatPDFText_l.close();

The output of our example is presented below as a screen dump..(taken from the actual PDF document that was generated out of this PeopleCode example)
Apply Formatting Styles to PDF Documents from PeopleCode - iText Example Output

4 comments:

  1. Great information! How do you set the alignment or placement of the paragraph?

    ReplyDelete
  2. Hi, great post, but how does one set the alignment or position of text for PDFs being generated from PeopleCode?

    ReplyDelete
  3. Can you Help me? When run the process, return ERRO in this position:
    > Local JavaObject &obj_reflection_l = CreateJavaObject("image");
    the error is:
    " Excepción Java: java.lang.NoClassDefFoundError: image: buscando clase image (2,725) CO_APPAP03.MAIN. GBL.default.1900-01-01.Step03.OnExecute PCPC:599 Statement:5 "

    I need created PDF, the created it's OK. But need with Format.

    ReplyDelete
  4. Hi, Please let me know where this PDF file will be created? do we need to specify any path?

    ReplyDelete