Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Tabel sangat penting saat bekerja dengan dokumen PDF. Mereka menyediakan fitur hebat untuk menampilkan informasi secara sistematis. Ruang nama Aspose.PDF berisi kelas-kelas bernama Table, Cell, dan Row yang menyediakan fungsionalitas untuk membuat tabel saat menghasilkan dokumen PDF dari awal.
Potongan kode berikut juga bekerja dengan pustaka Aspose.PDF.Drawing.
Tabel dapat dibuat dengan membuat objek dari Kelas Tabel.
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
Untuk menambahkan tabel ke file PDF yang sudah ada dengan Aspose.PDF for .NET, lakukan langkah-langkah berikut:
Potongan kode berikut menunjukkan cara menambahkan teks ke file PDF yang sudah ada.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddTable()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Tables();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "AddTable.pdf"))
{
// Initializes a new instance of the Table
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
// Set the table border color as LightGray
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
// Set the border for table cells
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
// Create a loop to add 10 rows
for (int row_count = 1; row_count < 10; row_count++)
{
// Add row to table
Aspose.Pdf.Row row = table.Rows.Add();
// Add table cells
row.Cells.Add("Column (" + row_count + ", 1)");
row.Cells.Add("Column (" + row_count + ", 2)");
row.Cells.Add("Column (" + row_count + ", 3)");
}
// Add table object to first page of input document
document.Pages[1].Paragraphs.Add(table);
// Save PDF document
document.Save(dataDir + "AddTable_out.pdf");
}
}
Aspose.PDF for .NET menyediakan properti ColSpan untuk menggabungkan kolom dalam tabel dan properti RowSpan untuk menggabungkan baris.
Kami menggunakan properti ColSpan
atau RowSpan
pada objek Cell
yang membuat sel tabel. Setelah menerapkan properti yang diperlukan, sel yang dibuat dapat ditambahkan ke tabel.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddTableRowColSpan()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Tables();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Initializes a new instance of the Table
Aspose.Pdf.Table table = new Aspose.Pdf.Table
{
// Set the table border color as LightGray
Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Color.Black),
// Set the border for table cells
DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Color.Black)
};
// Add 1st row to table
Aspose.Pdf.Row row1 = table.Rows.Add();
for (int cellCount = 1; cellCount <5; cellCount++)
{
// Add table cells
row1.Cells.Add($"Test 1 {cellCount}");
}
// Add 2nd row to table
Aspose.Pdf.Row row2 = table.Rows.Add();
row2.Cells.Add($"Test 2 1");
var cell = row2.Cells.Add($"Test 2 2");
cell.ColSpan = 2;
row2.Cells.Add($"Test 2 4");
// Add 3rd row to table
Aspose.Pdf.Row row3 = table.Rows.Add();
row4.Cells.Add("Test 3 1");
row4.Cells.Add("Test 3 2");
row4.Cells.Add("Test 3 3");
row4.Cells.Add("Test 3 4");
// Add 4th row to table
Aspose.Pdf.Row row4 = table.Rows.Add();
row3.Cells.Add("Test 4 1");
cell = row3.Cells.Add("Test 4 2");
cell.RowSpan = 2;
row3.Cells.Add("Test 4 3");
row3.Cells.Add("Test 4 4");
// Add 5th row to table
row4 = table.Rows.Add();
row4.Cells.Add("Test 5 1");
row4.Cells.Add("Test 5 3");
row4.Cells.Add("Test 5 4");
// Add table object to first page of input document
page.Paragraphs.Add(table);
// Save PDF document
document.Save(dataDir + "AddTableRowColSpan_out.pdf");
}
}
Hasil dari eksekusi kode di bawah ini adalah tabel yang digambarkan pada gambar berikut:
Harap dicatat bahwa ini juga mendukung fitur untuk mengatur gaya batas, margin, dan padding sel untuk tabel. Sebelum masuk ke rincian teknis lebih lanjut, penting untuk memahami konsep batas, margin, dan padding yang disajikan di bawah ini dalam diagram:
Dalam gambar di atas, Anda dapat melihat bahwa batas tabel, baris, dan sel tumpang tindih. Menggunakan Aspose.PDF, sebuah tabel dapat memiliki margin dan sel dapat memiliki padding. Untuk mengatur margin sel, kita harus mengatur padding sel.
Untuk mengatur batas Tabel, Row dan Cell objek, gunakan properti Table.Border, Row.Border, dan Cell.Border. Batas sel juga dapat diatur menggunakan properti DefaultCellBorder dari kelas Table atau Row. Semua properti terkait batas yang dibahas di atas ditugaskan ke sebuah instance dari kelas Row, yang dibuat dengan memanggil konstruktornya. Kelas Row memiliki banyak overload yang mengambil hampir semua parameter yang diperlukan untuk menyesuaikan batas.
Padding sel dapat dikelola menggunakan properti DefaultCellPadding dari kelas Tabel. Semua properti terkait padding ditugaskan ke sebuah instance dari kelas MarginInfo yang mengambil informasi tentang parameter Kiri
, Kanan
, Atas
, dan Bawah
untuk membuat margin kustom.
Dalam contoh berikut, lebar batas sel diatur menjadi 0,1 poin, lebar batas tabel diatur menjadi 1 poin, dan padding sel diatur menjadi 5 poin.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddMarginsOrPadding()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Tables();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
Aspose.Pdf.Page page = document.Pages.Add();
// Instantiate a table object
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
// Add the table in paragraphs collection of the desired section
page.Paragraphs.Add(tab1);
// Set with column widths of the table
tab1.ColumnWidths = "50 50 50";
// Set default cell border using BorderInfo object
tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F);
// Set table border using another customized BorderInfo object
tab1.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F);
// Create MarginInfo object and set its left, bottom, right and top margins
Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo();
margin.Top = 5f;
margin.Left = 5f;
margin.Right = 5f;
margin.Bottom = 5f;
// Set the default cell padding to the MarginInfo object
tab1.DefaultCellPadding = margin;
// Create rows in the table and then cells in the rows
Aspose.Pdf.Row row1 = tab1.Rows.Add();
row1.Cells.Add("col1");
row1.Cells.Add("col2");
row1.Cells.Add();
Aspose.Pdf.Text.TextFragment mytext = new Aspose.Pdf.Text.TextFragment("col3 with large text string");
// Row1.Cells.Add("col3 with large text string to be placed inside cell");
row1.Cells[2].Paragraphs.Add(mytext);
row1.Cells[2].IsWordWrapped = false;
// Row1.Cells[2].Paragraphs[0].FixedWidth= 80;
Aspose.Pdf.Row row2 = tab1.Rows.Add();
row2.Cells.Add("item1");
row2.Cells.Add("item2");
row2.Cells.Add("item3");
// Save PDF document
document.Save(dataDir + "MarginsOrPadding_out.pdf");
}
}
Untuk membuat tabel dengan sudut membulat, gunakan nilai RoundedBorderRadius
dari kelas BorderInfo dan atur gaya sudut tabel menjadi bulat.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateTableWithRoundCorner()
{
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
Aspose.Pdf.GraphInfo graph = new Aspose.Pdf.GraphInfo();
graph.Color = Aspose.Pdf.Color.Red;
// Create a blank BorderInfo object
Aspose.Pdf.BorderInfo bInfo = new Aspose.Pdf.BorderInfo(BorderSide.All, graph);
// Set the border a rounder border where radius of round is 15
bInfo.RoundedBorderRadius = 15;
// Set the table Corner style as Round.
tab1.CornerStyle = Aspose.Pdf.BorderCornerStyle.Round;
// Set the table border information
tab1.Border = bInfo;
}
Saat membuat tabel menggunakan agen visual seperti Microsoft Word, Anda sering kali akan menemukan diri Anda menggunakan salah satu opsi AutoFit untuk secara otomatis mengatur ukuran tabel ke lebar yang diinginkan. Misalnya, Anda dapat menggunakan opsi AutoFit to Window untuk menyesuaikan tabel dengan lebar halaman dan opsi AutoFit to Contents untuk memungkinkan setiap sel tumbuh atau menyusut untuk mengakomodasi isinya.
Secara default, Aspose.PDF menyisipkan tabel baru menggunakan ColumnAdjustment
dengan nilai Customized
. Tabel akan disesuaikan dengan lebar yang tersedia di halaman. Untuk mengubah perilaku ukuran pada tabel semacam itu atau tabel yang sudah ada, Anda dapat memanggil metode Table.autoFit(int). Metode ini menerima enumerasi AutoFitBehavior yang mendefinisikan jenis penyesuaian otomatis yang diterapkan pada tabel.
Seperti di Microsoft Word, metode autofit sebenarnya adalah pintasan yang menerapkan berbagai properti ke tabel sekaligus. Properti-properti ini sebenarnya yang memberikan perilaku yang diamati pada tabel. Kami akan membahas properti-properti ini untuk setiap opsi autofit. Kami akan menggunakan tabel berikut dan menerapkan pengaturan auto fit yang berbeda sebagai demonstrasi:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddAutoFitToWindow()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Tables();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
Aspose.Pdf.Page sec1 = document.Pages.Add();
// Instantiate a table object
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
// Add the table in paragraphs collection of the desired section
sec1.Paragraphs.Add(tab1);
// Set with column widths of the table
tab1.ColumnWidths = "50 50 50";
tab1.ColumnAdjustment = ColumnAdjustment.AutoFitToWindow;
// Set default cell border using BorderInfo object
tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F);
// Set table border using another customized BorderInfo object
tab1.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F);
// Create MarginInfo object and set its left, bottom, right and top margins
Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo();
margin.Top = 5f;
margin.Left = 5f;
margin.Right = 5f;
margin.Bottom = 5f;
// Set the default cell padding to the MarginInfo object
tab1.DefaultCellPadding = margin;
// Create rows in the table and then cells in the rows
Aspose.Pdf.Row row1 = tab1.Rows.Add();
row1.Cells.Add("col1");
row1.Cells.Add("col2");
row1.Cells.Add("col3");
Aspose.Pdf.Row row2 = tab1.Rows.Add();
row2.Cells.Add("item1");
row2.Cells.Add("item2");
row2.Cells.Add("item3");
// Save PDF document
document.Save(dataDir + "AutoFitToWindow_out.pdf");
}
}
Terkadang, diperlukan untuk mendapatkan lebar tabel secara dinamis. Kelas Aspose.PDF.Table memiliki metode GetWidth untuk tujuan ini. Misalnya, Anda belum mengatur lebar kolom tabel secara eksplisit dan mengatur ColumnAdjustment ke AutoFitToContent. Dalam hal ini, Anda dapat mendapatkan lebar tabel sebagai berikut.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetTableWidth()
{
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
Aspose.Pdf.Page page = document.Pages.Add();
// Initialize new table
Aspose.Pdf.Table table = new Aspose.Pdf.Table
{
ColumnAdjustment = ColumnAdjustment.AutoFitToContent
};
// Add row in table
Aspose.Pdf.Row row = table.Rows.Add();
// Add cell in table
Aspose.Pdf.Cell cell = row.Cells.Add("Cell 1 text");
cell = row.Cells.Add("Cell 2 text");
// Get table width
Console.WriteLine(table.GetWidth());
}
}
Aspose.PDF for .NET mendukung fitur untuk menambahkan sel tabel ke dalam file PDF. Saat membuat tabel, dimungkinkan untuk menambahkan teks atau gambar ke dalam sel. Selain itu, API juga menawarkan fitur untuk mengonversi file SVG ke format PDF. Dengan menggunakan kombinasi fitur-fitur ini, dimungkinkan untuk memuat gambar SVG dan menambahkannya ke dalam sel tabel.
Potongan kode berikut menunjukkan langkah-langkah untuk membuat instance tabel dan menambahkan gambar SVG di dalam sel tabel.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddSvgObjectToTable()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Tables();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Create an image instance
Aspose.Pdf.Image img = new Aspose.Pdf.Image();
// Set image type as SVG
img.FileType = Aspose.Pdf.ImageFileType.Svg;
// Path for source file
img.File = dataDir + "SVGToPDF.svg";
// Set width for image instance
img.FixWidth = 50;
// Set height for image instance
img.FixHeight = 50;
// Create table instance
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
// Set width for table cells
table.ColumnWidths = "100 100";
// Create row object and add it to table instance
Aspose.Pdf.Row row = table.Rows.Add();
// Create cell object and add it to row instance
Aspose.Pdf.Cell cell = row.Cells.Add();
// Add textfragment to paragraphs collection of cell object
cell.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("First cell"));
// Add another cell to row object
cell = row.Cells.Add();
// Add SVG image to paragraphs collection of recently added cell instance
cell.Paragraphs.Add(img);
// Create page object and add it to pages collection of document instance
Aspose.Pdf.Page page = document.Pages.Add();
// Add table to paragraphs collection of page object
page.Paragraphs.Add(table);
// Save PDF document
document.Save(dataDir + "AddSVGObjectToTable_out.pdf");
}
}
Terkadang Anda mungkin memiliki kebutuhan untuk mengimpor konten basis data yang memiliki beberapa tag HTML dan kemudian mengimpor konten tersebut ke objek Tabel. Saat mengimpor konten, tag HTML harus dirender sesuai di dalam dokumen PDF. Kami telah meningkatkan metode ImprotDataTable() untuk mencapai kebutuhan tersebut sebagai berikut:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddHtmlInsideTableCell()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Tables();
DataTable dt = new DataTable("Employee");
dt.Columns.Add("data", System.Type.GetType("System.String"));
DataRow dr = dt.NewRow();
dr[0] = "<li>Department of Emergency Medicine: 3400 Spruce Street Ground Silverstein Bldg Philadelphia PA 19104-4206</li>";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "<li>Penn Observation Medicine Service: 3400 Spruce Street Ground Floor Donner Philadelphia PA 19104-4206</li>";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "<li>UPHS/Presbyterian - Dept. of Emergency Medicine: 51 N. 39th Street . Philadelphia PA 19104-2640</li>";
dt.Rows.Add(dr);
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Initializes a new instance of the Table
Aspose.Pdf.Table tableProvider = new Aspose.Pdf.Table();
//Set column widths of the table
tableProvider.ColumnWidths = "400 50 ";
// Set the table border color as LightGray
tableProvider.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5F, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
// Set the border for table cells
tableProvider.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5F, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo();
margin.Top = 2.5F;
margin.Left = 2.5F;
margin.Bottom = 1.0F;
tableProvider.DefaultCellPadding = margin;
tableProvider.ImportDataTable(dt, false, 0, 0, 3, 1, true);
page.Paragraphs.Add(tableProvider);
// Save PDF document
document.Save(dataDir + "HTMLInsideTableCell_out.pdf");
}
}
Sebagai perilaku default, saat membuat tabel di dalam file PDF, tabel mengalir ke halaman berikutnya ketika mencapai margin bawah tabel. Namun, kita mungkin memiliki kebutuhan untuk memaksa menyisipkan pemisah halaman ketika sejumlah baris tertentu ditambahkan untuk tabel. Potongan kode berikut menunjukkan langkah-langkah untuk menyisipkan pemisah halaman ketika 10 baris ditambahkan untuk tabel.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void InsertPageBreak()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Tables();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Create table instance
Aspose.Pdf.Table tab = new Aspose.Pdf.Table();
// Set border style for table
tab.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Red);
// Set default border style for table with border color as Red
tab.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Red);
// Specify table columsn widht
tab.ColumnWidths = "100 100";
// Create a loop to add 200 rows for table
for (int counter = 0; counter <= 200; counter++)
{
Aspose.Pdf.Row row = new Aspose.Pdf.Row();
tab.Rows.Add(row);
Aspose.Pdf.Cell cell1 = new Aspose.Pdf.Cell();
cell1.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Cell " + counter + ", 0"));
row.Cells.Add(cell1); Aspose.Pdf.Cell cell2 = new Aspose.Pdf.Cell();
cell2.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Cell " + counter + ", 1"));
row.Cells.Add(cell2);
// When 10 rows are added, render new row in new page
if (counter % 10 == 0 && counter != 0)
{
row.IsInNewPage = true;
}
}
// Add table to paragraphs collection of PDF file
page.Paragraphs.Add(tab);
// Save PDF document
document.Save(dataDir + "InsertPageBreak_out.pdf");
}
}
Secara default, paragraf ditambahkan ke koleksi Paragraf objek Halaman. Namun, dimungkinkan untuk merender tabel di halaman baru alih-alih langsung setelah objek tingkat paragraf yang sebelumnya ditambahkan di halaman.
Untuk merender tabel di halaman baru, gunakan properti IsInNewPage di kelas BaseParagraph. Potongan kode berikut menunjukkan caranya.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddTableOnNewPage()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Tables();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
Aspose.Pdf.PageInfo pageInfo = document.PageInfo;
Aspose.Pdf.MarginInfo marginInfo = pageInfo.Margin;
marginInfo.Left = 37;
marginInfo.Right = 37;
marginInfo.Top = 37;
marginInfo.Bottom = 37;
pageInfo.IsLandscape = true;
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
table.ColumnWidths = "50 100";
// Add page
Page curPage = document.Pages.Add();
for (int i = 1; i <= 120; i++)
{
Aspose.Pdf.Row row = table.Rows.Add();
row.FixedRowHeight = 15;
Aspose.Pdf.Cell cell1 = row.Cells.Add();
cell1.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Content 1"));
Aspose.Pdf.Cell cell2 = row.Cells.Add();
cell2.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("HHHHH"));
}
Aspose.Pdf.Paragraphs paragraphs = curPage.Paragraphs;
paragraphs.Add(table);
Aspose.Pdf.Table table1 = new Aspose.Pdf.Table();
table.ColumnWidths = "100 100";
for (int i = 1; i <= 10; i++)
{
Aspose.Pdf.Row row = table1.Rows.Add();
Aspose.Pdf.Cell cell1 = row.Cells.Add();
cell1.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("LAAAAAAA"));
Aspose.Pdf.Cell cell2 = row.Cells.Add();
cell2.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("LAAGGGGGG"));
}
table1.IsInNewPage = true;
// Keep table 1 to next page
paragraphs.Add(table1);
// Save PDF document
document.Save(dataDir + "AddTableOnNewPage_out.pdf");
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.