Tab 1
此演示向您展示如何在 PDF 文档中创建包含指定数据的表格。
Data
e-iceblue
Option
downloads
如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo。
如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。。
Tab 2
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.grid.PdfGrid;
import com.spire.pdf.grid.PdfGridCell;
import com.spire.pdf.grid.PdfGridRow;
import java.awt.*;
import java.util.List;
public class PdfTableDemo {
public void createTable(List< CustomerModel > data,Color borderColor){
String outputFile = "output.pdf";
String []haeder={"Contact","Company","City","Country","Phone"};
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.getPages().add(PdfPageSize.A4, new PdfMargins(40, 30, 40, 30));
PdfBorders borders = new PdfBorders();
if (borderColor == null) {
borders.setAll(new PdfPen(new PdfRGBColor(Color.TRANSLUCENT)));
} else {
borders.setAll(new PdfPen(new PdfRGBColor(borderColor)));
}
PdfGrid grid = null;
PdfGridRow pdfGridRow;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 16), true);
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 10), true);
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
grid = new PdfGrid();
grid.setAllowCrossPages(true);
grid.getStyle().setCellSpacing(3f);
grid.getColumns().add(5);
double width = page.getCanvas().getClientSize().getWidth() - (grid.getColumns().getCount() + 1);
grid.getColumns().get(0).setWidth(width * 0.2);
grid.getColumns().get(1).setWidth(width * 0.2);
grid.getColumns().get(2).setWidth(width * 0.2);
grid.getColumns().get(3).setWidth(width * 0.2);
grid.getColumns().get(4).setWidth(width * 0.2);
pdfGridRow = grid.getHeaders().add(1)[0];
pdfGridRow.setHeight(50);
int i = 0;
//header row
for (i = 0; i < haeder.length; i++) {
PdfGridCell cell = pdfGridRow.getCells().get(i);
cell.setColumnSpan(1);
cell.setValue(haeder[i]);
cell.setStringFormat(format1);
cell.getStyle().setBorders(borders);
cell.getStyle().setFont(font1);
}
//data row
for (i = 0; i < data.size(); i++) {
pdfGridRow = grid.getRows().add();
pdfGridRow.setHeight(40f);
CustomerModel model=data.get(i);
String []values={model.getContact(),model.getCompany(),model.getCity(),model.getCountry(),model.getPhone()};
for (int j = 0; j < pdfGridRow.getCells().getCount(); j++) {
PdfGridCell cell = pdfGridRow.getCells().get(j);
cell.setColumnSpan(1);
cell.setValue(values[j]);
cell.getStyle().setBorders(borders);
cell.getStyle().setFont(font2);
cell.setStringFormat(format1);
}
}
grid.setRepeatHeader(true);
grid.draw(page, new Point(0, 0));
pdf.saveToFile(outputFile, FileFormat.PDF);
pdf.close();
}
}
class CustomerModel {
private String contact;
private String company;
private String city;
private String country;
private String phone;
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Tab 3
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System;
using System.Collections.Generic;
using System.Drawing;
namespace DemoOnlineCode
{
class CreateTableDemo
{
public void createTable(List< CustomerModel > data, Color borderColor)
{
String outputFile = "output.pdf";
String[] haeder = { "Contact", "Company", "City", "Country", "Phone" };
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4, new PdfMargins(40, 30, 40, 30));
PdfBorders borders = new PdfBorders();
if (borderColor == null)
{
borders.All=new PdfPen(new PdfRGBColor(Color.Transparent));
}
else
{
borders.All=new PdfPen(new PdfRGBColor(borderColor));
}
PdfGrid grid = null;
PdfGridRow pdfGridRow;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial",16f, FontStyle.Bold), true);
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 10f,FontStyle.Regular), true);
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
grid = new PdfGrid();
grid.AllowCrossPages=true;
grid.Style.CellSpacing=3f;
grid.Columns.Add(5);
float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
grid.Columns[0].Width=width * 0.2f;
grid.Columns[1].Width=width * 0.2f;
grid.Columns[2].Width=width * 0.2f;
grid.Columns[3].Width=width * 0.2f;
grid.Columns[4].Width=width * 0.2f;
pdfGridRow = grid.Headers.Add(1)[0];
pdfGridRow.Height=50;
int i = 0;
//header row
for (i = 0; i < haeder.Length; i++)
{
PdfGridCell cell = pdfGridRow.Cells[i];
cell.ColumnSpan=1;
cell.Value = haeder[i];
cell.StringFormat = format1;
cell.Style.Borders = borders;
cell.Style.Font = font1;
}
//data row
for (i = 0; i < data.Count; i++)
{
pdfGridRow = grid.Rows.Add();
pdfGridRow.Height = 40f;
CustomerModel model = data[i];
String[] values = { model.Contact, model.Company,model.City, model.Country, model.Phone };
for (int j = 0; j < pdfGridRow.Cells.Count; j++)
{
PdfGridCell cell = pdfGridRow.Cells[j];
cell.ColumnSpan=1;
cell.Value = values[j];
cell.Style.Borders = borders;
cell.Style.Font = font2;
cell.StringFormat = format1;
}
}
grid.RepeatHeader = true;
grid.Draw(page, new Point(0, 0));
pdf.SaveToFile(outputFile, FileFormat.PDF);
pdf.Close();
}
}
}
class CustomerModel
{
public string Contact;
public string Company;
public string City;
public string Country;
public string Phone;
}