Calculate the height of a paragraph in PDF

None
Generate PDF
11/24/2010

Downloads

This code sample demonstrates how to use the event mechanism to calculate the height of a paragraph in a PDF. The program writes the height of the paragraph to the console.

1 using System; 2 using System.IO; 3 4 using TallComponents.PDF; 5 using TallComponents.PDF.Layout; 6 using TallComponents.PDF.Layout.Paragraphs; 7 8 namespace CalculateParagraphHeight 9 { 10 class Program 11 { 12 static double startY; 13 static double endY; 14 15 static void Main(string[] args) 16 { 17 Document document = new Document(); 18 Section section = document.Sections.Add(); 19 20 section.Paragraphs.PrintParagraph += 21 new PrintParagraphEventHandler(section_PrintParagraph); 22 23 using (FileStream file = 24 new FileStream(@"..\..\out.pdf", FileMode.Create, FileAccess.Write)) 25 { 26 document.Write(file); 27 } 28 29 Console.WriteLine("Height: {0:f1} points", startY - endY); 30 } 31 32 static void section_PrintParagraph( 33 ParagraphCollection sender, PrintParagraphEventArgs e) 34 { 35 RtfParagraph rtf = new RtfParagraph(); 36 rtf.Text = File.ReadAllText(@"..\..\sample.rtf"); 37 rtf.EndParagraph += new EndParagraphEventHandler(rtf_EndParagraph); 38 e.Paragraph = rtf; 39 startY = e.Top; 40 } 41 42 static void rtf_EndParagraph(Paragraph sender, EndParagraphEventArgs e) 43 { 44 endY = e.Bottom; 45 } 46 } 47 }