Highlighted
Respected Contributor.
Respected Contributor.
2254 views

Calling Java from VC 2.1

Jump to solution

Hello,

We are quite new to Visual Cobol 2.1 for Eclipse under Linux.

We have an application in "classical cobol" and would like to be able to call the java library iText to generate pdf (or rtf) files. For example, we would like to read the content of file and generate a pdf report. The file is a MF cobol indexed file, accessed and updated with our cobol programs.

Visual Cobol offers the possibility to integrate both worlds.

But practically, how to proceed ?

A skeleton on how to start would be a great help. I'm sure that I'm not the only one with this kind of question!

Regards,

A.R.

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Calling Java from VC 2.1

Jump to solution

Yes, you can do this... I guess the first part is to get familiar with the iText library and JVM COBOL.

With this in mind.. below is the first example converted to COBOL, as you can see it is quite similar to Java version..

$set sourceformat"free"
$set ilusing "java.lang"
$set ilusing "java.io"
$set ilusing "com.itextpdf.text"
$set ilusing "com.itextpdf.text.pdf"


class-id HelloWorld.

01 RESULT String value "HelloWorld.pdf" static.

method-id. main static.
procedure division using by value args as type String occurs any.

invoke type HelloWorld::new()::createPdf(RESULT)
end method.

method-id createPdf.
procedure division using by value filename as type String.

declare document as type Document

*> step 1
set document to Type Document::new()

*> step 2
invoke type PdfWriter::getInstance(document,
new FileOutputStream(filename))

*> step 3
invoke document::open()

*> step 4
invoke document::add(new Paragraph("Hello World!"))

*> step 5
invoke document::close()

end method.
end class.

View solution in original post

0 Likes
5 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Calling Java from VC 2.1

Jump to solution

Yes, you can do this... I guess the first part is to get familiar with the iText library and JVM COBOL.

With this in mind.. below is the first example converted to COBOL, as you can see it is quite similar to Java version..

$set sourceformat"free"
$set ilusing "java.lang"
$set ilusing "java.io"
$set ilusing "com.itextpdf.text"
$set ilusing "com.itextpdf.text.pdf"


class-id HelloWorld.

01 RESULT String value "HelloWorld.pdf" static.

method-id. main static.
procedure division using by value args as type String occurs any.

invoke type HelloWorld::new()::createPdf(RESULT)
end method.

method-id createPdf.
procedure division using by value filename as type String.

declare document as type Document

*> step 1
set document to Type Document::new()

*> step 2
invoke type PdfWriter::getInstance(document,
new FileOutputStream(filename))

*> step 3
invoke document::open()

*> step 4
invoke document::add(new Paragraph("Hello World!"))

*> step 5
invoke document::close()

end method.
end class.

View solution in original post

0 Likes
Highlighted
Respected Contributor.
Respected Contributor.

RE: Calling Java from VC 2.1

Jump to solution

Thanks a lot for your answer.

I'll try to play around the given canvas. iText is not the problem - although...

It's more the glue with cobol, at least how to start!

Regards,

Alain

0 Likes
Highlighted
Respected Contributor.
Respected Contributor.

RE: Calling Java from VC 2.1

Jump to solution

Great!

I have been able to generate a pdf file and add some paragraph...

I have a new problem concerning the transposition in vc of a java part of an itext example :

In java, we have this example :

package part1.chapter01;
 
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
 
/** * Examples with mirrored margins. */
public class HelloWorldMirroredMargins {
 
    /** Path to the resulting PDF file. */
    public static final String RESULT
        = "results/part1/chapter01/hello_mirrored_margins.pdf";
 
    /** * Creates a PDF file: hello_mirrored_margins.pdf * @param args no arguments needed */
    public static void main(String[] args)
        throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.setPageSize(PageSize.A5);
        document.setMargins(36, 72, 108, 180);
        document.setMarginMirroring(true);
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph(
            "The left margin of this odd page is 36pt (0.5 inch); " +
            "the right margin 72pt (1 inch); " +
            "the top margin 108pt (1.5 inch); " +
            "the bottom margin 180pt (2.5 inch)."));
        Paragraph paragraph = new Paragraph();
        paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
        for (int i = 0; i < 20; i++) {
            paragraph.add("Hello World! Hello People! " +
            		"Hello Sky! Hello Sun! Hello Moon! Hello Stars!");
        }
        document.add(paragraph);
        document.add(new Paragraph(
            "The right margin of this even page is 36pt (0.5 inch); " +
            "the left margin 72pt (1 inch)."));
        // step 5
        document.close();
    }
}

With your help, it was easily transposed in cobol except for the part in red. In java, Element is not declared explicitely. And I have found how to transfer it in cobol :

      $set sourceformat"free"
      $set ilusing "java.lang"
      $set ilusing "java.io"
      $set ilusing "com.itextpdf.text"
      $set ilusing "com.itextpdf.text.pdf"
      $set ilusing "com.itextpdf.text.Document"
      $set ilusing "com.itextpdf.text.DocumentException"
      $set ilusing "com.itextpdf.text.Element"
      $set ilusing "com.itextpdf.text.PageSize"
      $set ilusing "com.itextpdf.text.Paragraph"
      $set ilusing "com.itextpdf.text.pdf.PdfWriter"
 
class-id HelloWorld public.

working-storage section.
1 RESULT String value "HelloWorldMargins.pdf" static.

method-id. main static.
local-storage section.
procedure division using by value args as type String occurs any.
invoke type HelloWorld::new()::createPdf(RESULT)
goback.
end method.

method-id createPdf.
local-storage section.
1 i pic 99.
procedure division using by value filename as type String.

    declare document as type Document
    declare myParagraph as type Paragraph

    *> step 1
    set document to Type Document::new()

    *> step 2
    invoke type PdfWriter::getInstance(document, new FileOutputStream(filename))
    *>invoke document::SetPageSize(PageSize.A5)
    invoke document::setMargins(36,72,108,180)
    invoke document::setMarginMirroring(true)
    
    *> step 3
    invoke document::open()

    *> step 4
    invoke document::add(new Paragraph(
            "The left margin of this odd page is 36pt (0.5 inch); "
            & "the right margin 72pt (1 inch); "
            & "the top margin 108pt (1.5 inch); "
            & "the bottom margin 180pt (2.5 inch)."))
    set myParagraph to type Paragraph::new()
    *>invoke myParagraph::setAlignment(Element.ALIGN_JUSTIFIED)
    perform varying i from 1 by 1 until i > 20
      invoke myParagraph::add("Hello World! Hello People! "
                    & "Hello Sky! Hello Sun! Hello Moon! Hello Stars!")
    end-perform    
    invoke document::add(myParagraph)
    invoke document::add(new Paragraph(
            "The right margin of this even page is 36pt (0.5 inch); "
            &"the left margin 72pt (1 inch)."))
            
    *> step 5
    invoke document::close()

end method.

end class.

If you have another good advise, i'll be grateful!

Regards,

Alain


0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Calling Java from VC 2.1

Jump to solution

Try...

      invoke document::setPageSize(type PageSize::A5)

and

       invoke myParagraph::setAlignment(type Element::ALIGN_JUSTIFIED)

0 Likes
Highlighted
Respected Contributor.
Respected Contributor.

RE: Calling Java from VC 2.1

Jump to solution

What a talent!

Thank you, it works fine.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.