Skip to content

Commit 5315dfa

Browse files
Add 91 example(s) for working-with-tables
1 parent 8e1f5d9 commit 5315dfa

93 files changed

Lines changed: 9232 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.InteropServices;
4+
using Aspose.Pdf;
5+
using Aspose.Pdf.Text;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
// Paths (adjust as needed)
12+
const string outputPath = "AutoNumberedTable.pdf";
13+
14+
// Create a new PDF document
15+
using (Document doc = new Document())
16+
{
17+
// Add a page to the document
18+
Page page = doc.Pages.Add();
19+
20+
// Create a table with two columns
21+
Table table = new Table
22+
{
23+
// Optional: set table position and width
24+
Left = 50,
25+
Top = 700,
26+
ColumnWidths = "50 200" // first column 50 units, second column 200 units
27+
};
28+
29+
// Sample data for the second column
30+
string[] data = { "Apple", "Banana", "Cherry", "Date", "Elderberry" };
31+
32+
// Add rows: first column will be filled later with numbers
33+
foreach (string item in data)
34+
{
35+
// Add a new row
36+
Row row = table.Rows.Add();
37+
38+
// Add empty cell for the auto‑numbered column
39+
row.Cells.Add(new Cell());
40+
41+
// Add cell with actual data
42+
Cell dataCell = new Cell();
43+
dataCell.Paragraphs.Add(new TextFragment(item));
44+
row.Cells.Add(dataCell);
45+
}
46+
47+
// Insert sequential numbers into the first cell of each row
48+
for (int i = 0; i < table.Rows.Count; i++)
49+
{
50+
Row row = table.Rows[i];
51+
// Ensure the first cell exists
52+
if (row.Cells.Count > 0)
53+
{
54+
// Clear any existing content (if any) and add the number
55+
row.Cells[0].Paragraphs.Clear();
56+
row.Cells[0].Paragraphs.Add(new TextFragment((i + 1).ToString()));
57+
}
58+
}
59+
60+
// Add the table to the page
61+
page.Paragraphs.Add(table);
62+
63+
// Save the PDF – guard against missing GDI+ (libgdiplus) on non‑Windows platforms
64+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
65+
{
66+
doc.Save(outputPath);
67+
Console.WriteLine($"PDF saved to '{outputPath}'.");
68+
}
69+
else
70+
{
71+
try
72+
{
73+
doc.Save(outputPath);
74+
Console.WriteLine($"PDF saved to '{outputPath}'.");
75+
}
76+
catch (TypeInitializationException ex) when (ContainsDllNotFound(ex))
77+
{
78+
Console.WriteLine("Warning: GDI+ (libgdiplus) is not available on this platform. PDF was not saved.");
79+
}
80+
}
81+
}
82+
}
83+
84+
// Helper that walks the inner‑exception chain looking for a DllNotFoundException
85+
private static bool ContainsDllNotFound(Exception? ex)
86+
{
87+
while (ex != null)
88+
{
89+
if (ex is DllNotFoundException)
90+
return true;
91+
ex = ex.InnerException;
92+
}
93+
return false;
94+
}
95+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Text;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
// Input and output file paths
11+
const string inputPdf = "input.pdf";
12+
const string outputPdf = "output.pdf";
13+
14+
// Ensure the input file exists
15+
if (!File.Exists(inputPdf))
16+
{
17+
Console.Error.WriteLine($"Input file not found: {inputPdf}");
18+
return;
19+
}
20+
21+
// Open the existing PDF document
22+
using (Document doc = new Document(inputPdf))
23+
{
24+
// Get the first page (pages are 1‑based)
25+
Page page = doc.Pages[1];
26+
27+
// Create a table and add it to the page
28+
Table table = new Table
29+
{
30+
// Optional: set table position and column widths
31+
Left = 50,
32+
Top = 700,
33+
ColumnWidths = "200"
34+
};
35+
page.Paragraphs.Add(table);
36+
37+
// Add a row to the table
38+
Row row = table.Rows.Add();
39+
40+
// Add a cell to the row
41+
Cell cell = row.Cells.Add();
42+
43+
// Create a paragraph (TextFragment) with centered alignment
44+
TextFragment paragraph = new TextFragment("Centered text in cell")
45+
{
46+
// HorizontalAlignment is defined in BaseParagraph
47+
HorizontalAlignment = HorizontalAlignment.Center
48+
};
49+
50+
// Add the paragraph to the cell's Paragraphs collection
51+
cell.Paragraphs.Add(paragraph);
52+
53+
// Save the modified PDF
54+
doc.Save(outputPdf);
55+
}
56+
57+
Console.WriteLine($"PDF saved to '{outputPdf}'.");
58+
}
59+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Text;
5+
using Aspose.Pdf.Tagged;
6+
using Aspose.Pdf.LogicalStructure;
7+
8+
class Program
9+
{
10+
static void Main()
11+
{
12+
const string inputPath = "input.pdf";
13+
const string outputPath = "output_footnotes.pdf";
14+
15+
if (!File.Exists(inputPath))
16+
{
17+
Console.Error.WriteLine($"File not found: {inputPath}");
18+
return;
19+
}
20+
21+
// Load the source PDF
22+
using (Document doc = new Document(inputPath))
23+
{
24+
// Enable tagged content and set basic properties
25+
ITaggedContent tagged = doc.TaggedContent;
26+
tagged.SetLanguage("en-US");
27+
tagged.SetTitle("Document with footnotes");
28+
29+
// Work with the first page
30+
Page page = doc.Pages[1];
31+
32+
// Create a simple table (2 columns)
33+
Table table = new Table
34+
{
35+
ColumnWidths = "200 200"
36+
};
37+
page.Paragraphs.Add(table);
38+
39+
// Header row
40+
Row header = table.Rows.Add();
41+
header.Cells.Add("Item");
42+
header.Cells.Add("Description");
43+
44+
// Data row with a footnote reference in the second cell
45+
Row dataRow = table.Rows.Add();
46+
dataRow.Cells.Add("Widget A"); // first cell
47+
48+
// Second cell – contains superscript number and a footnote
49+
Cell footnoteCell = dataRow.Cells.Add();
50+
51+
// TextFragment with superscript "1" (Unicode U+00B9)
52+
TextFragment tf = new TextFragment("Widget A description\u00B9");
53+
tf.TextState.Font = FontRepository.FindFont("Helvetica");
54+
tf.TextState.FontSize = 12;
55+
56+
// Create the footnote content
57+
Note footNote = new Note("This widget is a prototype used for testing.");
58+
59+
// Associate the footnote with the TextFragment
60+
tf.FootNote = footNote;
61+
62+
// Add the TextFragment to the cell
63+
footnoteCell.Paragraphs.Add(tf);
64+
65+
// Save the modified PDF
66+
doc.Save(outputPath);
67+
}
68+
69+
Console.WriteLine($"Saved PDF with footnotes to '{outputPath}'.");
70+
}
71+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Annotations;
5+
using Aspose.Pdf.Text;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
const string inputPath = "input.pdf";
12+
const string outputPath = "output.pdf";
13+
14+
if (!File.Exists(inputPath))
15+
{
16+
Console.Error.WriteLine($"File not found: {inputPath}");
17+
return;
18+
}
19+
20+
// Load the existing PDF
21+
using (Document doc = new Document(inputPath))
22+
{
23+
// Get the first page (1‑based indexing)
24+
Page page = doc.Pages[1];
25+
26+
// Create a table and add it to the page
27+
Table table = new Table();
28+
page.Paragraphs.Add(table);
29+
30+
// Add a single row and a single cell
31+
Row row = table.Rows.Add();
32+
Cell cell = row.Cells.Add();
33+
34+
// Add visible text to the cell
35+
cell.Paragraphs.Add(new TextFragment("Click here for more info"));
36+
37+
// Define the rectangle area for the hyperlink.
38+
// Coordinates are in points, origin is bottom‑left of the page.
39+
// Adjust these values to fit the actual cell position as needed.
40+
Aspose.Pdf.Rectangle linkRect = new Aspose.Pdf.Rectangle(
41+
llx: 100, // left
42+
lly: 500, // bottom
43+
urx: 250, // right
44+
ury: 520 // top
45+
);
46+
47+
// Create a LinkAnnotation that points to an external URL
48+
LinkAnnotation link = new LinkAnnotation(page, linkRect)
49+
{
50+
// Use GoToURIAction for external web links (preferred over Hyperlink property)
51+
Action = new GoToURIAction("https://www.example.com")
52+
};
53+
54+
// Add the annotation to the cell's paragraph collection.
55+
// LinkAnnotation derives from BaseParagraph, so it can be added here.
56+
cell.Paragraphs.Add(link);
57+
58+
// Save the modified PDF
59+
doc.Save(outputPath);
60+
}
61+
62+
Console.WriteLine($"PDF with hyperlink saved to '{outputPath}'.");
63+
}
64+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.InteropServices;
4+
using Aspose.Pdf;
5+
using Aspose.Pdf.Text;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
const string outputPath = "multiline_cell.pdf";
12+
13+
// Create a new PDF document
14+
using (Document doc = new Document())
15+
{
16+
// Add a page
17+
Page page = doc.Pages.Add();
18+
19+
// Create a table and add it to the page
20+
Table table = new Table
21+
{
22+
// Optional: set column widths (single column in this example)
23+
ColumnWidths = "200"
24+
};
25+
page.Paragraphs.Add(table);
26+
27+
// Add a row to the table
28+
Row row = table.Rows.Add();
29+
30+
// Add a cell to the row
31+
Cell cell = row.Cells.Add();
32+
33+
// First line of text
34+
TextFragment line1 = new TextFragment("First line of text");
35+
line1.TextState.FontSize = 12;
36+
line1.TextState.Font = FontRepository.FindFont("Helvetica");
37+
line1.TextState.ForegroundColor = Color.Black;
38+
39+
// Line break fragment (empty text with a newline)
40+
TextFragment lineBreak = new TextFragment("\n");
41+
42+
// Second line of text
43+
TextFragment line2 = new TextFragment("Second line of text");
44+
line2.TextState.FontSize = 12;
45+
line2.TextState.Font = FontRepository.FindFont("Helvetica");
46+
line2.TextState.ForegroundColor = Color.Black;
47+
48+
// Third line of text
49+
TextFragment line3 = new TextFragment("Third line of text");
50+
line3.TextState.FontSize = 12;
51+
line3.TextState.Font = FontRepository.FindFont("Helvetica");
52+
line3.TextState.ForegroundColor = Color.Black;
53+
54+
// Add the fragments to the cell, separating them with line‑break fragments
55+
cell.Paragraphs.Add(line1);
56+
cell.Paragraphs.Add(lineBreak);
57+
cell.Paragraphs.Add(line2);
58+
cell.Paragraphs.Add(lineBreak);
59+
cell.Paragraphs.Add(line3);
60+
61+
// Save the document (guard against missing GDI+ on non‑Windows platforms)
62+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
63+
{
64+
doc.Save(outputPath);
65+
Console.WriteLine($"PDF saved to '{outputPath}'.");
66+
}
67+
else
68+
{
69+
try
70+
{
71+
doc.Save(outputPath);
72+
Console.WriteLine($"PDF saved to '{outputPath}'.");
73+
}
74+
catch (TypeInitializationException ex) when (ContainsDllNotFound(ex))
75+
{
76+
Console.WriteLine("Warning: GDI+ (libgdiplus) is not available on this platform. PDF was not saved.");
77+
}
78+
}
79+
}
80+
}
81+
82+
// Helper to detect a nested DllNotFoundException
83+
static bool ContainsDllNotFound(Exception ex)
84+
{
85+
while (ex != null)
86+
{
87+
if (ex is DllNotFoundException)
88+
return true;
89+
ex = ex.InnerException;
90+
}
91+
return false;
92+
}
93+
}

0 commit comments

Comments
 (0)