The following example demonstrates how to load an existing PDF document, get information about its first 10 pages (page size, number of words, images & hyperlinks 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"; // Load a pdf document using( var pdfDoc = PdfDocument.Load( PagesSample.PagesSampleResourcesDirectory + filename ) ) { var outputFileName = "AnalysePageContent.pdf"; var outputPath = PagesSample.PagesSampleOutputDirectory + outputFileName; // Create a pdf document. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Get the first page of the document. var firstPage = pdfoutput.Pages.First(); // Add title. var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); firstPage.AddParagraph( "Analyze Page Content", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); var exportData = new ExportInfoToPdf(); // Add text. exportData.AddLine( pdfoutput, $"Printing Pages info from: {filename}\n\n", Color.Red ); exportData.AddLine( pdfoutput, $"Total pages: {pdfDoc.Pages.Count}\n" ); // Add Text for statistics of first 10 pages. foreach( var page in pdfDoc.Pages.Take( 10 ) ) { exportData.AddLine( pdfoutput, $"\nPage {page.Id}:" ); exportData.AddLine( pdfoutput, $" Size: {page.PageSettings.Width:F1} x {page.PageSettings.Height:F1} points" ); exportData.AddLine( pdfoutput, $" Words: {page.Words.Count()}" ); exportData.AddLine( pdfoutput, $" Images: {page.Images.Count()}" ); exportData.AddLine( pdfoutput, $" Hyperlinks: {page.Hyperlinks.Count()}" ); } // Save the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } } | |