The following example demonstrates how to load an existing PDF document, get information about its pages (how many words & images it contains) & print that information into a newly created PDF document.
| Get information about the pages of a PDF document (C#) |
Copy Code |
|---|---|
public static void AnalysePageContent() { Console.WriteLine( "=== ANALYZE PAGE CONTENT ===" ); string filename = "Animal_Welfare_Magazine_January_to_June_2023.pdf"; // Loads a PdfDocument using( var pdfDoc = PdfDocument.Load( PagesSample.PagesSampleResourcesDirectory + filename ) ) { var outputFileName = "AnalysePageContent.pdf"; var outputPath = PagesSample.PagesSampleOutputDirectory + outputFileName; // Creates a PdfDocument. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Gets the first Page of the document. var firstPage = pdfoutput.Pages.First(); // Adds a title. var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); firstPage.AddParagraph( "Analyze Page Content", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Adds red Courier text. var redTextStyle = TextStyle.WithFontAndColor( pdfoutput.Fonts.GetStandardFont( StandardFontType.Courier ), 12d, Brushes.Red ); firstPage.AddText( $"Printing Pages info from: {filename}", new Point( 25, 60 ), redTextStyle ); // Adds Courier text. var pageTextStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Courier ), 12d ); firstPage.AddText( $"Total pages: {pdfDoc.Pages.Count}", new Point( 25, 90 ), pageTextStyle ); // Adds text to display data about the Pages var counter = 0; foreach( var page in pdfDoc.Pages ) { firstPage.AddText( $"-Page {page.Id}: {page.Words.Count()} Words, {page.Images.Count()} Images.", new Point( 25, 120 + ( counter * 15 ) ), pageTextStyle ); counter++; } // Saves the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } } | |