Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Java

Uploaded by

pasopem528
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java

Uploaded by

pasopem528
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Creating a professional Java report document can be done in a few steps.

Here’s
a quick guide to creating a simple Java report in a document format, which you
can adapt depending on the format (e.g., Word, PDF).
1. Generate
Copy code
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.14</version>
</dependency>
Step 2: Java Code to Create the PDF
Here’s how you can use iText to generate a simple PDF report:
java
Copy code
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

import java.io.File;

public class PdfReportGenerator {

public static void main(String[] args) {


String destination = "Java_Report.pdf";

try {
PdfWriter writer = new PdfWriter(new File(destination));
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);

// Add report content


document.add(new Paragraph("Java Report"));
document.add(new Paragraph("Summary of Findings:"));
document.add(new Paragraph("1. All unit tests passed successfully."));
document.add(new Paragraph("2. Code coverage reached 95%."));
document.add(new Paragraph("3. No critical issues detected in the
system."));

// Close document
document.close();

System.out.println("Report created successfully at " + destination);

} catch (Exception e) {
e.printStackTrace();
}
}
}
2. Generate a Word Document Using Apache POI
Alternatively, if you need a Word document, you can use the Apache POI library.
Step 1: Add Apache POI Dependency
xml
Copy code
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
Step 2: Java Code to Create the Word Document
java
Copy code
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;

public class WordReportGenerator {

public static void main(String[] args) {


String destination = "Java_Report.docx";

try (XWPFDocument document = new XWPFDocument();


FileOutputStream out = new FileOutputStream(destination)) {

XWPFParagraph paragraph = document.createParagraph();


XWPFRun run = paragraph.createRun();
run.setText("Java Report");
run.setBold(true);
run.setFontSize(14);

XWPFParagraph content = document.createParagraph();


XWPFRun contentRun = content.createRun();
contentRun.setText("Summary of Findings:\n");
contentRun.addBreak();
contentRun.setText("1. All unit tests passed successfully.\n");
contentRun.setText("2. Code coverage reached 95%.\n");
contentRun.setText("3. No critical issues detected in the system.");

document.write(out);
System.out.println("Word document created successfully at " +
destination);

You might also like