[英]How to make a PDF of multiple pages with iText Java
I have a list with fruits, I'm trying to put a fruit of the list with it's data into a JPanel by using a for loop. 我有一个包含水果的列表,我试图通过使用for循环将列表的成果与数据放入JPanel。 I want to fill the screen with JPanels of 4 by 2 (total 8) into one page.
我想用4个2(共8个)的JPanel填充屏幕到一个页面。 See result on the image below.
请参见下图中的结果。
In the code example I didn't used the fruitslist, but instead I used a for loop which represents like I have a list of 11 fruits >> for(int i = 0; i < 11; i++). 在代码示例中,我没有使用fruitslist,而是使用了for循环,表示我有11个水果>> for(int i = 0; i <11; i ++)的列表。 So what I'm expecting to get now is one PDF file with 2 pages.
所以我现在期待的是一个包含2页的PDF文件。 The first one contains 8 panels and the second one 3 panels.
第一个包含8个面板,第二个包含3个面板。 What I'm wondering is how I can make the application to get multiple paged PDF where one page only contains max 8 panels.
我想知道的是我如何使应用程序获得多页分页PDF,其中一页只包含最多8个面板。 Any help would be appreciated.
任何帮助,将不胜感激。
//The code of the JFrame// // JFrame的代码//
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.*;
import java.io.FileOutputStream;
import java.util.Date;
import javax.swing.*;
public class SmallList extends JFrame {
GridLayout gridLayout = new GridLayout(0,4);
public SmallList(String name) {
super(name);
setResizable(false);
}
public void addComponentsToPane(final Container pane) {
final JPanel components = new JPanel();
components.setLayout(gridLayout);
for(int i = 0; i < 11; i++) {
JPanel jPanel = new javax.swing.JPanel();
JSeparator seperator = new javax.swing.JSeparator();
JSeparator separator1 = new javax.swing.JSeparator();
JLabel nameLabel = new javax.swing.JLabel();
JLabel unitLabel = new javax.swing.JLabel();
JLabel priceLabel = new javax.swing.JLabel();
JLabel centLabel = new javax.swing.JLabel();
JLabel comma = new javax.swing.JLabel();
jPanel.setBackground(new java.awt.Color(255, 255, 255));
jPanel.setAlignmentX(0.0F);
jPanel.setAlignmentY(0.0F);
jPanel.setMaximumSize(null);
jPanel.setPreferredSize(new java.awt.Dimension(203, 289));
seperator.setBackground(new java.awt.Color(0, 0, 0));
separator1.setBackground(new java.awt.Color(0, 0, 0));
nameLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
nameLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
unitLabel.setFont(new java.awt.Font("Tahoma", 0, 40)); // NOI18N
unitLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
priceLabel.setFont(new java.awt.Font("Tahoma", 0, 90)); // NOI18N
priceLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
priceLabel.setText(" ");
centLabel.setFont(new java.awt.Font("Tahoma", 0, 55)); // NOI18N
centLabel.setText(" ");
comma.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
comma.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
comma.setText(",");
javax.swing.GroupLayout jPanelLayout = new javax.swing.GroupLayout(jPanel);
jPanel.setLayout(jPanelLayout);
jPanelLayout.setHorizontalGroup(
jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(seperator)
.addComponent(separator1)
.addGroup(jPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(unitLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(nameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelLayout.createSequentialGroup()
.addComponent(priceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanelLayout.createSequentialGroup()
.addComponent(comma)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelLayout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(centLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap())
);
jPanelLayout.setVerticalGroup(
jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanelLayout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(unitLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(seperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanelLayout.createSequentialGroup()
.addGap(32, 32, 32)
.addGroup(jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(priceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(comma)))
.addGroup(jPanelLayout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(centLabel)))
.addGap(32, 32, 32)
.addComponent(separator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
components.add(jPanel);
}
pane.add(components);
print();
}
private void print() {
Date date = new Date();
try {
JFrame f = new JFrame("Productlijst klein");
f.add(this.getComponent(0));
f.setUndecorated(true);
f.pack();
f.setVisible(true);
Document document = new Document(PageSize.A4.rotate());
String fileName = "prijslijst" + date.getTime() + ".pdf";
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate template = cb.createTemplate(PageSize.A4.getHeight(), PageSize.A4.getWidth());
Graphics2D g2d = template.createGraphics(f.getWidth(), f.getHeight());
f.paintAll(g2d);
g2d.dispose();
cb.addTemplate(template, 0, 0);
document.close();
} catch (Exception e) {
System.out.println(e);
}
}
/**
* Create the GUI and show it. For thread safety,
* this method is invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
SmallList frame = new SmallList("Fruit - kleine afmeting");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
frame.addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
/* Use an appropriate Look and Feel */
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
} }
The result will be like this, when the application is finished. 应用程序完成后,结果将如下所示。 One page will contain 8 JPanels of fruits.
一页将包含8个JPanels水果。
Solution 解
Instead of using pdf graphics, simply make your table in the PDF using iText. 而不是使用pdf图形,只需使用iText在PDF中创建表。 splitting the model (the underlying data) and your graphic representation of it (a user interface, or a PDF document) is always a good idea, as this allows you flexibility in the future.
拆分模型(基础数据)和它的图形表示(用户界面或PDF文档)总是一个好主意,因为这将使您在未来具有灵活性。
Code for table examples (which is likely how you'd have to build your PDF) can be found at the iText webpage 可以在iText网页上找到表格示例代码(可能是您构建PDF的方式)
https://developers.itextpdf.com/content/itext-7-examples/itext-7-tables https://developers.itextpdf.com/content/itext-7-examples/itext-7-tables
Explicitly inserting a page break has already been asked (and answered) here 此处已经询问(并回答)明确插入分页符
how can I make a page break using itext 如何使用itext进行分页
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.