我会尽量做到最完整。
我想用Itext 5在pdf中写一个“tableview”。
但我没有找到:在PDF中编写tableview的行是什么。
我用scenebuilder创建了tableview。
应用程序是打印帐单,tableview是产品列表。
有我的尝试:
@FXML
private TableView<Product> tableProduct;
//Create table products
final ObservableList<Product> productSelected = FXCollections.observableArrayList(
new Product("Product name 1",23,23,1.0),
new Product("Product name 2",23,4,11.0),
new Product("Product name 3",45,3,11.0)
);
@Override
public void initialize(URL location, ResourceBundle resources) {
index.set(-1); //Ligne de code pour supprimer que quand on selcection:
namecolumn.setCellValueFactory(new PropertyValueFactory<Product, String>("name"));
tvacolumn.setCellValueFactory(new PropertyValueFactory<Product, Integer>("tva"));
quantitycolumn.setCellValueFactory(new PropertyValueFactory<Product, Integer>("quantity"));
pucolumn.setCellValueFactory(new PropertyValueFactory<Product, Double>("pu"));
totalcolumn.setCellValueFactory(new PropertyValueFactory<Product, Double>("total"));
tableProduct.setItems(productSelected);
//get the index when clicking on table row(ligne)
tableProduct.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() {
@Override
public void changed(ObservableValue<?> observable, Object oldValue, Object newValue) {
index.set(productSelected.indexOf(newValue));
//System.out.println("Ok index is:"+productSelected.indexOf(newValue));
}
});
}
===================================PDF=====================================
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(chemin));
document.open();
document.add(tableProduct); //Error
document.add(tableProduct.getItems().toString()); //Error
catch (DocumentException de) {
de.printStackTrace();
Logger.getLogger(PDFgenerator.class.getName()).log(Level.SEVERE, null, de);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
document.close();
我将继续搜索解决方案。如果我发现,我说。 谢谢你的帮助这么快;)
答案 0 :(得分:0)
这是我提出的解决方案;)
public PdfPTable TableauPrincipale() {
PdfPTable table = new PdfPTable(5);
table.setWidthPercentage(100);
//------------------------On créer l'objet cellule--------------------
PdfPCell cell = null;
//Description
table. addCell (getCell("Description",PdfPCell.ALIGN_CENTER ));
//TVA
table. addCell (getCell("TVA",PdfPCell.ALIGN_CENTER ));
//Quantity
table. addCell (getCell("Quantity",PdfPCell.ALIGN_CENTER ));
//P.U
table. addCell (getCell("P.U",PdfPCell.ALIGN_CENTER ));
//Total
table. addCell (getCell("Total",PdfPCell.ALIGN_CENTER ));
//CONTENU DU TABLEAU
for(int i = 0; i <=NombresProduits ; i++)
{
table. addCell (getCell(tableProduct.getItems().get(i).getName(),PdfPCell.ALIGN_CENTER ));
table.addCell(getCell(Integer.toString(tableProduct.getItems().get(i).getTva())+"%",PdfPCell.ALIGN_CENTER ));
table.addCell(getCell((Integer.toString(tableProduct.getItems().get(i).getQuantity())),PdfPCell.ALIGN_CENTER ));
table.addCell(getCell((Double.toString(tableProduct.getItems().get(i).getPu())),PdfPCell.ALIGN_CENTER ));
table.addCell(getCell((Double.toString(tableProduct.getItems().get(i).getTotal()))+"€",PdfPCell.ALIGN_CENTER ));
}
return table;
} //Corps principale de l'appli
private PdfPCell getCell(String text, int alignment) {
PdfPCell cell = new PdfPCell(new Phrase(text));
cell.setPadding(0);
cell.setHorizontalAlignment(alignment);
cell.setBorder(PdfPCell.NO_BORDER);
return cell;
}
答案 1 :(得分:0)
PdfPTable table = new PdfTable(3); //where 3 is number of columns.
For(int j=0; j<tableContent.size(); j++) { //where tableContent is the observable list.
table.addCell(tableContent.get(j).getName1());
// where Name 1-3 are the product you want to print.
table.addCell(tableContent.get(j).getName2());
table.addCell(tableContent.get(j).getName3());
}
doc.add(table).
答案 2 :(得分:-1)
请参阅iTextPDF PDF with Table 示例
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class SimplePDFTable {
public static void main(String[] args) throws IOException,
DocumentException {
new SimplePDFTable().createPdf("sample.pdf");
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(8);
for(int aw = 0; aw < 16; aw++){
table.addCell("hi");
}
document.add(table);
document.close();
}
}