I created some code to extract text and font height from a PDF file using iTextSharp, but does not handle text rotation. How can that information be extracted/computed?
Here is the code:
// Create PDF reader
var reader = new PdfReader("myfile.pdf");
for (var k = 1; k <= reader.NumberOfPages; ++k)
{
// Get page resources
var page = reader.GetPageN(k);
var pdfResources = page.GetAsDict(PdfName.RESOURCES);
// Create custom render listener, processor, and process page!
var listener = new FunnyRenderListener();
var processor = new PdfContentStreamProcessor(listener);
var bytes = ContentByteUtils.GetContentBytesForPage(reader, k);
processor.ProcessContent(bytes, pdfResources);
}
[...]
public class FunnyRenderListener : IRenderListener
{
[...]
void RenderText(TextRenderInfo renderInfo)
{
// Get text
var text = renderInfo.GetText();
// Get (computed) font size
var bottomLeftPoint = renderInfo.GetDescentLine().GetStartPoint();
var topRightPoint = renderInfo.GetAscentLine().GetEndPoint();
var rectangle = new Rectangle(
bottomLeftPoint[Vector.I1], bottomLeftPoint[Vector.I2],
topRightPoint[Vector.I1], topRightPoint[Vector.I2]
);
var fontSize = Convert.ToDouble(rectangle.Height);
Console.WriteLine("Text: {0}, FontSize: {1}", text, fontSize);
}
}