IEnumerable <string> foreach返回result中的所有页面; foreach而不是for循环

时间:2018-06-04 08:35:17

标签: c# foreach ienumerable

我想返回3页的字符串输出,而是程序输出一页3次。我已经意识到“for loop”实现有效,但我想使用foreach。

namespace Itext5
{
    class Program
    {
        static void Main(string[] args)
        {
            // PdfReader reader = new PdfReader(@"D:\500indycar-race-results.pdf");
            PdfReader reader = new PdfReader(@"D:\191.pdf");                       
            IEnumerable<string> GetColumnText(float llx, float lly, float urx, float ury)

            {
                int get_PageNum = reader.NumberOfPages;
                var rect = new iTextSharp.text.Rectangle(llx, lly, urx, ury);
                var renderFilter = new RenderFilter[1];
                renderFilter[0] = new RegionTextRenderFilter(rect);
                var textExtractionStrategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), renderFilter);

                for (int pagecount = 1; pagecount <= get_PageNum; pagecount++)
                {
                    var text = PdfTextExtractor.GetTextFromPage(reader, pagecount, textExtractionStrategy);
                    yield return text;
                }
            }

            foreach (string result in GetColumnText(0, 0, 500, 500000))
            {
                Console.Write("{0} ", result);
                Console.ReadLine();
            }

        }
    }
}

1 个答案:

答案 0 :(得分:-2)

using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.Collections.Generic;
using System.IO;

namespace Itext5
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfReader reader = new PdfReader(@"D:\191.pdf");
            IEnumerable<string> GetColumnText(float llx, float lly, float urx, float ury)

            {
                int get_PageNum = reader.NumberOfPages;

                for (int pagecount = 1; pagecount <= get_PageNum; pagecount++)
                {
                    var rect = new iTextSharp.text.Rectangle(llx, lly, urx, ury);
                    var renderFilter = new RenderFilter[1];
                    renderFilter[0] = new RegionTextRenderFilter(rect);
                    var textExtractionStrategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), renderFilter);
                    var text = PdfTextExtractor.GetTextFromPage(reader, pagecount, textExtractionStrategy);
                    yield return text;
                }
            }

            foreach (string result in GetColumnText(0, 0, 500, 500000))
            {
                Console.Write("{0} ", result);
                Console.ReadLine();
            }

        }
    }
}