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+ }
0 commit comments