Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions working-with-tables/add-auto-numbered-column-to-pdf-table.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using Aspose.Pdf;
using Aspose.Pdf.Text;

class Program
{
static void Main()
{
// Paths (adjust as needed)
const string outputPath = "AutoNumberedTable.pdf";

// Create a new PDF document
using (Document doc = new Document())
{
// Add a page to the document
Page page = doc.Pages.Add();

// Create a table with two columns
Table table = new Table
{
// Optional: set table position and width
Left = 50,
Top = 700,
ColumnWidths = "50 200" // first column 50 units, second column 200 units
};

// Sample data for the second column
string[] data = { "Apple", "Banana", "Cherry", "Date", "Elderberry" };

// Add rows: first column will be filled later with numbers
foreach (string item in data)
{
// Add a new row
Row row = table.Rows.Add();

// Add empty cell for the auto‑numbered column
row.Cells.Add(new Cell());

// Add cell with actual data
Cell dataCell = new Cell();
dataCell.Paragraphs.Add(new TextFragment(item));
row.Cells.Add(dataCell);
}

// Insert sequential numbers into the first cell of each row
for (int i = 0; i < table.Rows.Count; i++)
{
Row row = table.Rows[i];
// Ensure the first cell exists
if (row.Cells.Count > 0)
{
// Clear any existing content (if any) and add the number
row.Cells[0].Paragraphs.Clear();
row.Cells[0].Paragraphs.Add(new TextFragment((i + 1).ToString()));
}
}

// Add the table to the page
page.Paragraphs.Add(table);

// Save the PDF – guard against missing GDI+ (libgdiplus) on non‑Windows platforms
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
doc.Save(outputPath);
Console.WriteLine($"PDF saved to '{outputPath}'.");
}
else
{
try
{
doc.Save(outputPath);
Console.WriteLine($"PDF saved to '{outputPath}'.");
}
catch (TypeInitializationException ex) when (ContainsDllNotFound(ex))
{
Console.WriteLine("Warning: GDI+ (libgdiplus) is not available on this platform. PDF was not saved.");
}
}
}
}

// Helper that walks the inner‑exception chain looking for a DllNotFoundException
private static bool ContainsDllNotFound(Exception? ex)
{
while (ex != null)
{
if (ex is DllNotFoundException)
return true;
ex = ex.InnerException;
}
return false;
}
}
59 changes: 59 additions & 0 deletions working-with-tables/add-centered-paragraph-to-table-cell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Text;

class Program
{
static void Main()
{
// Input and output file paths
const string inputPdf = "input.pdf";
const string outputPdf = "output.pdf";

// Ensure the input file exists
if (!File.Exists(inputPdf))
{
Console.Error.WriteLine($"Input file not found: {inputPdf}");
return;
}

// Open the existing PDF document
using (Document doc = new Document(inputPdf))
{
// Get the first page (pages are 1‑based)
Page page = doc.Pages[1];

// Create a table and add it to the page
Table table = new Table
{
// Optional: set table position and column widths
Left = 50,
Top = 700,
ColumnWidths = "200"
};
page.Paragraphs.Add(table);

// Add a row to the table
Row row = table.Rows.Add();

// Add a cell to the row
Cell cell = row.Cells.Add();

// Create a paragraph (TextFragment) with centered alignment
TextFragment paragraph = new TextFragment("Centered text in cell")
{
// HorizontalAlignment is defined in BaseParagraph
HorizontalAlignment = HorizontalAlignment.Center
};

// Add the paragraph to the cell's Paragraphs collection
cell.Paragraphs.Add(paragraph);

// Save the modified PDF
doc.Save(outputPdf);
}

Console.WriteLine($"PDF saved to '{outputPdf}'.");
}
}
71 changes: 71 additions & 0 deletions working-with-tables/add-footnote-references-in-table-cells.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Text;
using Aspose.Pdf.Tagged;
using Aspose.Pdf.LogicalStructure;

class Program
{
static void Main()
{
const string inputPath = "input.pdf";
const string outputPath = "output_footnotes.pdf";

if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"File not found: {inputPath}");
return;
}

// Load the source PDF
using (Document doc = new Document(inputPath))
{
// Enable tagged content and set basic properties
ITaggedContent tagged = doc.TaggedContent;
tagged.SetLanguage("en-US");
tagged.SetTitle("Document with footnotes");

// Work with the first page
Page page = doc.Pages[1];

// Create a simple table (2 columns)
Table table = new Table
{
ColumnWidths = "200 200"
};
page.Paragraphs.Add(table);

// Header row
Row header = table.Rows.Add();
header.Cells.Add("Item");
header.Cells.Add("Description");

// Data row with a footnote reference in the second cell
Row dataRow = table.Rows.Add();
dataRow.Cells.Add("Widget A"); // first cell

// Second cell – contains superscript number and a footnote
Cell footnoteCell = dataRow.Cells.Add();

// TextFragment with superscript "1" (Unicode U+00B9)
TextFragment tf = new TextFragment("Widget A description\u00B9");
tf.TextState.Font = FontRepository.FindFont("Helvetica");
tf.TextState.FontSize = 12;

// Create the footnote content
Note footNote = new Note("This widget is a prototype used for testing.");

// Associate the footnote with the TextFragment
tf.FootNote = footNote;

// Add the TextFragment to the cell
footnoteCell.Paragraphs.Add(tf);

// Save the modified PDF
doc.Save(outputPath);
}

Console.WriteLine($"Saved PDF with footnotes to '{outputPath}'.");
}
}
64 changes: 64 additions & 0 deletions working-with-tables/add-hyperlink-to-table-cell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Annotations;
using Aspose.Pdf.Text;

class Program
{
static void Main()
{
const string inputPath = "input.pdf";
const string outputPath = "output.pdf";

if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"File not found: {inputPath}");
return;
}

// Load the existing PDF
using (Document doc = new Document(inputPath))
{
// Get the first page (1‑based indexing)
Page page = doc.Pages[1];

// Create a table and add it to the page
Table table = new Table();
page.Paragraphs.Add(table);

// Add a single row and a single cell
Row row = table.Rows.Add();
Cell cell = row.Cells.Add();

// Add visible text to the cell
cell.Paragraphs.Add(new TextFragment("Click here for more info"));

// Define the rectangle area for the hyperlink.
// Coordinates are in points, origin is bottom‑left of the page.
// Adjust these values to fit the actual cell position as needed.
Aspose.Pdf.Rectangle linkRect = new Aspose.Pdf.Rectangle(
llx: 100, // left
lly: 500, // bottom
urx: 250, // right
ury: 520 // top
);

// Create a LinkAnnotation that points to an external URL
LinkAnnotation link = new LinkAnnotation(page, linkRect)
{
// Use GoToURIAction for external web links (preferred over Hyperlink property)
Action = new GoToURIAction("https://www.example.com")
};

// Add the annotation to the cell's paragraph collection.
// LinkAnnotation derives from BaseParagraph, so it can be added here.
cell.Paragraphs.Add(link);

// Save the modified PDF
doc.Save(outputPath);
}

Console.WriteLine($"PDF with hyperlink saved to '{outputPath}'.");
}
}
93 changes: 93 additions & 0 deletions working-with-tables/add-multiline-text-to-table-cell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using Aspose.Pdf;
using Aspose.Pdf.Text;

class Program
{
static void Main()
{
const string outputPath = "multiline_cell.pdf";

// Create a new PDF document
using (Document doc = new Document())
{
// Add a page
Page page = doc.Pages.Add();

// Create a table and add it to the page
Table table = new Table
{
// Optional: set column widths (single column in this example)
ColumnWidths = "200"
};
page.Paragraphs.Add(table);

// Add a row to the table
Row row = table.Rows.Add();

// Add a cell to the row
Cell cell = row.Cells.Add();

// First line of text
TextFragment line1 = new TextFragment("First line of text");
line1.TextState.FontSize = 12;
line1.TextState.Font = FontRepository.FindFont("Helvetica");
line1.TextState.ForegroundColor = Color.Black;

// Line break fragment (empty text with a newline)
TextFragment lineBreak = new TextFragment("\n");

// Second line of text
TextFragment line2 = new TextFragment("Second line of text");
line2.TextState.FontSize = 12;
line2.TextState.Font = FontRepository.FindFont("Helvetica");
line2.TextState.ForegroundColor = Color.Black;

// Third line of text
TextFragment line3 = new TextFragment("Third line of text");
line3.TextState.FontSize = 12;
line3.TextState.Font = FontRepository.FindFont("Helvetica");
line3.TextState.ForegroundColor = Color.Black;

// Add the fragments to the cell, separating them with line‑break fragments
cell.Paragraphs.Add(line1);
cell.Paragraphs.Add(lineBreak);
cell.Paragraphs.Add(line2);
cell.Paragraphs.Add(lineBreak);
cell.Paragraphs.Add(line3);

// Save the document (guard against missing GDI+ on non‑Windows platforms)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
doc.Save(outputPath);
Console.WriteLine($"PDF saved to '{outputPath}'.");
}
else
{
try
{
doc.Save(outputPath);
Console.WriteLine($"PDF saved to '{outputPath}'.");
}
catch (TypeInitializationException ex) when (ContainsDllNotFound(ex))
{
Console.WriteLine("Warning: GDI+ (libgdiplus) is not available on this platform. PDF was not saved.");
}
}
}
}

// Helper to detect a nested DllNotFoundException
static bool ContainsDllNotFound(Exception ex)
{
while (ex != null)
{
if (ex is DllNotFoundException)
return true;
ex = ex.InnerException;
}
return false;
}
}
Loading
Loading