Reference https://amg-solution.jp/blog/1519
JasperReport converts the form to PDF according to the following flow.
Load the form template ↓ Compile template ↓ Embed data in template ↓ Output as PDF file
So let's first create a template file that will be the type
First, create a folder called report under resource (any name is fine) and right-click! Click ** New ** to ** JasperReports **
Use the Blank_A4 template. Click Blank A4 to Next, enter the file name to Next
Click ** One Empty Record ** for ** Data Adapter ** to continue. JasperReport has a function to bring data directly from DB. But most of the time, you want to process it in Java and use it. So set it empty.
When ** Congratulations ** is displayed, the wizard is complete!
I think that such a screen came out.
This is the composition of the page called a band. The title is at the top of the page, and the page header and page footer are always printed at the beginning and end of the page. The text is output in Detail 1.
Take over the tutorial here and remove the extra bands.
Right-click ** Page_Header ** from ** Outline ** and select ** delete **.
The basic usage of JasperReport is easy.
Let's drag Static Text on the right side of the screen to the screen.
You now have a fixed text. I will display the Japanese of this.
Click the text to see the ** Property View **. From this property, you can manipulate this element (called). Open the StaticText tab in the properties and change ** Text **. If you change it, change it to Japanese font ** Change Font ** to IPAexg
Next, let's enter the date and customer name.
Next I would like to enter the date and customer name. The date and customer name change each time you print, so you need to control it from Java. JasperReport provides a container called parameters to receive values from Java. Let's make it right away.
Let's open ** Parameters ** from ** Outline **
The list in the right frame is the default parameter that the template has. This time, I want to prepare a new customer name and date, so let's prepare parameters ** Client_name ** and ** Date_today **.
Right-click Parameters and click ** create Parameter **.
Change Name to ** Client_name ** and press Ctrl + s. You can leave the type as String. Let's make ** Date_today ** in the same way.
Now the form is ready to assign a value. However, the process of drawing this has not been completed. Place ** Text Filed ** on the screen and draw the parameter values.
Drag two ** TextField ** onto the screen
Once placed, one will be the customer name and the other the date. Open the TextField properties and open the TextField tab.
Associate this ** Expression ** with a parameter. Change the customer name to ** $ P {Client_name} ** and the date to ** $ P {Date_today} **. Like this (Although I will omit it from here, please ** change all fonts to IPAexg ** for the elements placed on the screen.)
Is it like this?
Then the main form. Make a table.
this time
Column 1: Product Name (String) Column 2: Price (Integer)
Make a very simple table.
Right-click ** Field ** in the outline and click ** create Field **
Create fields for ** product_name ** (String type) and ** price ** (Integer type).
If you can, select this and drag it to the template!
(Of course, the created field also changes the font.)
Let's change the format of the numbers and make it cool. You can select the format from the ** TextField ** Pattern. It is the same as Excel and Java formats, so let's make it with reference to that.
Now you have the necessary elements for the sample form. Let's make it look a little better.
It turned out to be something like this. (You can color the border by clicking the qualification from the Borders tab.)
When it comes to a real business document, I think that the opening greetings and closing words will be included, but this time I have omitted it for the sake of clarity.
This completes the form template. After that, let's write the processing on the Java side that controls this
You can also preview the created form. Let's switch the tab at the bottom of the form screen from ** Design ** to ** preview ** (if you get a compile error, the file should be buggy at the time of the form)
The setting to enter the value in the parameter is displayed, so let's brew and preview the value you like
Recommended Posts