Skip to content

Commit ef41a74

Browse files
Add 85 example(s) for Pages
1 parent be4889a commit ef41a74

87 files changed

Lines changed: 6145 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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
5+
namespace AddBatesNumberingExample
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
// Define the PDF files to process. In a real scenario these could be read from a directory.
12+
string[] inputFiles = new string[] { "input1.pdf", "input2.pdf" };
13+
14+
foreach (string inputPath in inputFiles)
15+
{
16+
// Ensure the source file exists before processing.
17+
if (!File.Exists(inputPath))
18+
{
19+
Console.WriteLine($"File not found: {inputPath}");
20+
continue;
21+
}
22+
23+
// Open the PDF document inside a using block for deterministic disposal.
24+
using (Document doc = new Document(inputPath))
25+
{
26+
// Add Bates numbering to each page.
27+
// StartNumber is set to 5; each subsequent page will increase by 1.
28+
// To achieve an increment of 5 per page, we start at 5 and later could adjust the prefix if needed.
29+
doc.Pages.AddBatesNumbering(artifact =>
30+
{
31+
artifact.StartNumber = 5; // Starting number.
32+
artifact.NumberOfDigits = 6; // Pad with leading zeros to 6 digits.
33+
artifact.Prefix = "BN-"; // Optional prefix.
34+
// The default increment is 1. For a custom step of 5, you would need to generate custom text per page.
35+
});
36+
37+
// Build the output file name.
38+
string outputFileName = Path.GetFileNameWithoutExtension(inputPath) + "_bates.pdf";
39+
// Save the modified document.
40+
doc.Save(outputFileName);
41+
Console.WriteLine($"Bates numbering added: {outputFileName}");
42+
}
43+
}
44+
}
45+
}
46+
}

pages/add-bates-numbering-stamp.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using Aspose.Pdf;
3+
4+
public class Program
5+
{
6+
public static void Main()
7+
{
8+
// Create a new PDF document with a single blank page
9+
using (Document document = new Document())
10+
{
11+
document.Pages.Add();
12+
13+
// Add Bates numbering to every page
14+
document.Pages.AddBatesNumbering(delegate (BatesNArtifact artifact)
15+
{
16+
// Start numbering at 1000
17+
artifact.StartNumber = 1000;
18+
// Append a dash after each number
19+
artifact.Suffix = "-";
20+
// Optional: center the stamp and set a bottom margin
21+
artifact.ArtifactHorizontalAlignment = HorizontalAlignment.Center;
22+
artifact.BottomMargin = 10f;
23+
});
24+
25+
// Save the modified PDF
26+
document.Save("output.pdf");
27+
}
28+
}
29+
}

pages/add-bates-numbering.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.InteropServices;
4+
using Aspose.Pdf;
5+
using Aspose.Pdf.Text;
6+
7+
public class Program
8+
{
9+
public static void Main()
10+
{
11+
// Create a sample PDF document
12+
using (Document doc = new Document())
13+
{
14+
// Add a page
15+
Page page = doc.Pages.Add();
16+
17+
// Add some sample text to the page
18+
TextFragment tf = new TextFragment("Sample PDF for Bates numbering.");
19+
page.Paragraphs.Add(tf);
20+
21+
// Configure and add Bates numbering to all pages
22+
doc.Pages.AddBatesNumbering(artifact =>
23+
{
24+
artifact.Prefix = "DOC";
25+
artifact.Suffix = "-2026";
26+
artifact.StartNumber = 1;
27+
artifact.NumberOfDigits = 4; // optional, defines the width of the number
28+
artifact.StartPage = 1;
29+
artifact.EndPage = 0; // 0 means no upper limit (all pages)
30+
artifact.IsBackground = false;
31+
artifact.ArtifactHorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
32+
artifact.ArtifactVerticalAlignment = Aspose.Pdf.VerticalAlignment.Bottom;
33+
artifact.BottomMargin = 20;
34+
});
35+
36+
// Save the document – guard against missing GDI+ on non‑Windows platforms
37+
string outputPath = "output.pdf";
38+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
39+
{
40+
doc.Save(outputPath);
41+
Console.WriteLine($"PDF saved to {outputPath}");
42+
}
43+
else
44+
{
45+
try
46+
{
47+
doc.Save(outputPath);
48+
Console.WriteLine($"PDF saved to {outputPath}");
49+
}
50+
catch (TypeInitializationException ex) when (ContainsDllNotFound(ex))
51+
{
52+
Console.WriteLine("GDI+ (libgdiplus) is not available on this platform. PDF not saved.");
53+
}
54+
}
55+
}
56+
}
57+
58+
private static bool ContainsDllNotFound(Exception ex)
59+
{
60+
Exception? current = ex;
61+
while (current != null)
62+
{
63+
if (current is DllNotFoundException)
64+
return true;
65+
current = current.InnerException;
66+
}
67+
return false;
68+
}
69+
}

pages/add-bates-numbering__v2.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Text;
5+
6+
namespace AddBatesNumberingExample
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
// Create a sample PDF document
13+
using (Document doc = new Document())
14+
{
15+
// Add a page with some sample text
16+
Page page = doc.Pages.Add();
17+
TextFragment fragment = new TextFragment("Sample PDF page content.");
18+
page.Paragraphs.Add(fragment);
19+
20+
// Configure and add Bates numbering with alphanumeric prefix
21+
doc.Pages.AddBatesNumbering(artifact =>
22+
{
23+
artifact.Prefix = "PRJ-";
24+
artifact.StartNumber = 100;
25+
artifact.NumberOfDigits = 5;
26+
artifact.BottomMargin = 20;
27+
artifact.ArtifactHorizontalAlignment = HorizontalAlignment.Center;
28+
artifact.ArtifactVerticalAlignment = VerticalAlignment.Bottom;
29+
});
30+
31+
// Save the resulting PDF – guard against missing GDI+ (libgdiplus) on non‑Windows platforms
32+
string outputPath = "output.pdf";
33+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
34+
{
35+
doc.Save(outputPath);
36+
Console.WriteLine($"PDF saved to '{outputPath}'.");
37+
}
38+
else
39+
{
40+
try
41+
{
42+
doc.Save(outputPath);
43+
Console.WriteLine($"PDF saved to '{outputPath}'. (non‑Windows platform, libgdiplus may be required)");
44+
}
45+
catch (TypeInitializationException ex) when (ContainsDllNotFound(ex))
46+
{
47+
Console.WriteLine("Warning: GDI+ (libgdiplus) is not available on this platform. " +
48+
"The PDF was not saved, but the program ran without crashing.");
49+
}
50+
}
51+
}
52+
}
53+
54+
// Helper that walks the inner‑exception chain to detect a missing native GDI+ library
55+
private static bool ContainsDllNotFound(Exception? ex)
56+
{
57+
while (ex != null)
58+
{
59+
if (ex is DllNotFoundException)
60+
return true;
61+
ex = ex.InnerException;
62+
}
63+
return false;
64+
}
65+
}
66+
}

pages/add-bates-numbering__v3.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using Aspose.Pdf;
3+
4+
public class Program
5+
{
6+
public static void Main()
7+
{
8+
// Create a sample PDF with a single blank page
9+
using (Document document = new Document())
10+
{
11+
Page page = document.Pages.Add();
12+
13+
// Add Bates numbering to each page with the format "2026-####"
14+
document.Pages.AddBatesNumbering((BatesNArtifact artifact) =>
15+
{
16+
artifact.Prefix = "2026-";
17+
artifact.NumberOfDigits = 4;
18+
artifact.StartNumber = 1;
19+
});
20+
21+
// Save the PDF with Bates numbers applied
22+
document.Save("output.pdf");
23+
}
24+
}
25+
}

pages/add-blank-page-with-label.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using Aspose.Pdf;
3+
4+
namespace AddBlankPageWithLabel
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
// Create a new PDF document
11+
using (Document document = new Document())
12+
{
13+
// Insert a blank page at the beginning (page number 1, 1‑based indexing)
14+
Page insertedPage = document.Pages.Insert(1);
15+
16+
// Create a page label that will display a lowercase Roman numeral "i"
17+
PageLabel pageLabel = new PageLabel();
18+
pageLabel.NumberingStyle = NumberingStyle.NumeralsRomanLowercase;
19+
pageLabel.StartingValue = 1;
20+
pageLabel.Prefix = "";
21+
22+
// Apply the label to the first page (zero‑based index)
23+
document.PageLabels.UpdateLabel(0, pageLabel);
24+
25+
// Save the resulting PDF
26+
document.Save("output.pdf");
27+
}
28+
}
29+
}
30+
}

pages/add-bold-uppercase-header.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Text;
5+
6+
public class Program
7+
{
8+
public static void Main()
9+
{
10+
const string outputPath = "output.pdf";
11+
12+
using (Document doc = new Document())
13+
{
14+
// Add a page
15+
Page page = doc.Pages.Add();
16+
17+
// Add sample body text
18+
TextFragment body = new TextFragment("This is a sample PDF document.");
19+
page.Paragraphs.Add(body);
20+
21+
// Create header text fragment (bold and uppercase)
22+
TextFragment headerFragment = new TextFragment("SECTION HEADING");
23+
headerFragment.TextState.Font = FontRepository.FindFont("Arial");
24+
headerFragment.TextState.FontSize = 12;
25+
headerFragment.TextState.FontStyle = FontStyles.Bold;
26+
headerFragment.TextState.ForegroundColor = Color.Black;
27+
28+
// Create HeaderFooter and assign the header fragment
29+
HeaderFooter header = new HeaderFooter();
30+
header.Paragraphs.Add(headerFragment);
31+
header.Margin = new MarginInfo(0, 0, 20, 0); // top margin
32+
33+
// Apply the header to the current page (and any future pages you add manually)
34+
page.Header = header;
35+
36+
// Save the document with a guard for platforms lacking GDI+ (libgdiplus)
37+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
38+
{
39+
doc.Save(outputPath);
40+
Console.WriteLine($"PDF saved to '{outputPath}'.");
41+
}
42+
else
43+
{
44+
try
45+
{
46+
doc.Save(outputPath);
47+
Console.WriteLine($"PDF saved to '{outputPath}' (non‑Windows platform).");
48+
}
49+
catch (TypeInitializationException ex) when (ContainsDllNotFound(ex))
50+
{
51+
Console.WriteLine("Warning: GDI+ (libgdiplus) is not available on this platform. PDF was not saved.");
52+
}
53+
}
54+
}
55+
}
56+
57+
private static bool ContainsDllNotFound(Exception ex)
58+
{
59+
while (ex != null)
60+
{
61+
if (ex is DllNotFoundException)
62+
return true;
63+
ex = ex.InnerException;
64+
}
65+
return false;
66+
}
67+
}

pages/add-centered-page-numbers.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using Aspose.Pdf;
3+
using Aspose.Pdf.Text;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
// Create a sample PDF with three pages
10+
using (Document sampleDoc = new Document())
11+
{
12+
// Add three blank pages
13+
sampleDoc.Pages.Add();
14+
sampleDoc.Pages.Add();
15+
sampleDoc.Pages.Add();
16+
sampleDoc.Save("input.pdf");
17+
}
18+
19+
// Open the PDF and add page numbers
20+
using (Document doc = new Document("input.pdf"))
21+
{
22+
// Iterate through all pages
23+
for (int pageNumber = 1; pageNumber <= doc.Pages.Count; pageNumber++)
24+
{
25+
Page page = doc.Pages[pageNumber];
26+
PageNumberStamp pageNumberStamp = new PageNumberStamp();
27+
pageNumberStamp.StartingNumber = 1;
28+
pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Center;
29+
pageNumberStamp.VerticalAlignment = VerticalAlignment.Bottom;
30+
page.AddStamp(pageNumberStamp);
31+
}
32+
33+
doc.Save("output.pdf");
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)