The following example demonstrates how to load an existing PDF document, get text from a specific area of that document & then output that text into a newly created PDF document.
| Get text located in a specific area of a PDF document (C#) |
Copy Code |
|---|---|
public static void GetTextFromArea() { Console.WriteLine( "=== GET TEXT FROM AN AREA ===" ); var outputFileName = "GetTextFromArea.pdf"; var outputPath = TextsSample.TextsSampleOutputDirectory + outputFileName; // Loads a PdfDocument. using( var pdfInput = PdfDocument.Load( TextsSampleResourcesDirectory + @"Two Page Text Only - from libre office.pdf" ) ) { // Gets the first Page of the input document. var page = pdfInput.Pages[ 0 ]; // Gets the text located in a specific area of the first Page. var areaText = page.GetTextFromArea( new Rectangle( 297, 77, 75, 12 ) ); // Creates an output PdfDocument to display AreaText. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Gets the first Page of the output document. var outputPage = pdfoutput.Pages[ 0 ]; // Sets the title. var titleFont = pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ); outputPage.AddParagraph( "Get Text From Area", TextStyle.WithFont( titleFont, 15 ), new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Displays the AreaText. var textStyle = TextStyle.WithFont( titleFont, 12 ); outputPage.AddText( $"The text found in the area (297, 77, 375, 88) is: \"{areaText}\".", new Point( 110, 145 ), textStyle ); // Saves the output document. pdfoutput.Save(); Console.WriteLine( $"Created: {outputFileName}" ); } } } | |