无法使用本地变量' pagefrom'在C#Visual Studio

时间:2018-03-01 09:18:49

标签: c# visual-studio

此代码的目的是: - 它将找到"结束余额" .pdf文件中的单词,它将分割/提取页面。

' __________________________________________ //通过找到的关键字提取此示例页面。

using System;
using Bytescout.PDFExtractor;

namespace SplittingExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputFile = @"C:\Users\trajan3\Desktop\Pdf Splitting Test\aa.pdf";
            int pagefrom = 1 + 0;

        // (!) page number in ExtractPage() is 1-based
        // Create Bytescout.PDFExtractor.TextExtractor instance
        TextExtractor extractor = new TextExtractor();
        extractor.RegistrationName = "demo";
        extractor.RegistrationKey = "demo";
        string searchString = "Ending Balance";


        // Load sample PDF document
        extractor.LoadDocumentFromFile(inputFile);

        int pageCount = extractor.GetPageCount();

        // Search each page for a keyword 
        for (int i = 0; i < pageCount; i++)
        {

            if (extractor.Find(i, searchString, false))
            {
                // Extract page
                using (DocumentSplitter splitter = new DocumentSplitter("demo", "demo"))
                {
                    splitter.OptimizeSplittedDocuments = true;

                    int pageNumber = i + 1;  // (!) page number in ExtractPage() is 1-based


                    string outputFile = @".\page" + pageNumber + ".pdf";
                    splitter.ExtractPageRange(inputFile, outputFile, pagefrom, pageNumber);

                    Console.WriteLine("Extracted page " + **pagefrom** + " to " + pageNumber + " to file \"" + outputFile + "\"");
                    int **pagefrom** = i + **pagefrom**;  // (!) page number in ExtractPage() is 1-based

                }
            }


        } 
        Console.WriteLine();
        Console.WriteLine("Press any key...");
        Console.ReadKey();          
    }
}

}

但是对于&#34; pagefrom&#39;变量是底层的红色,表示不能使用局部变量&#39; pagefrom&#39;在宣布之前。有人可以帮我这个。

1 个答案:

答案 0 :(得分:0)

// This example page extraction by found keyword.

using System;
using Bytescout.PDFExtractor;

namespace SplittingExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputFile = @"C:\Users\trajan3\Desktop\Pdf Splitting Test\aa.pdf";
            //int pagefrom = 1 + 0;

            // (!) page number in ExtractPage() is 1-based
            // Create Bytescout.PDFExtractor.TextExtractor instance
            TextExtractor extractor = new TextExtractor();
            extractor.RegistrationName = "demo";
            extractor.RegistrationKey = "demo";
            string searchString = "Ending Balance";
            int pagefrom = 1;

            // Load sample PDF document
            extractor.LoadDocumentFromFile(inputFile);

            int pageCount = extractor.GetPageCount();

            // Search each page for a keyword 
            for (int i = 0; i < pageCount; i++)
            {

                if (extractor.Find(i, searchString, false))
                {

                    // Extract page
                    using (DocumentSplitter splitter = new DocumentSplitter("demo", "demo"))
                    {

                        splitter.OptimizeSplittedDocuments = true;

                        int pageNumber = i + 1;  // (!) page number in ExtractPage() is 1-based


                        string outputFile = @".\page" + pageNumber + ".pdf";
                        splitter.ExtractPageRange(inputFile, outputFile, pagefrom, pageNumber);

                        Console.WriteLine("Extracted page " + pagefrom + " to " + pageNumber + " to file \"" + outputFile + "\"");
                        pagefrom = pageNumber + 1;  // (!) page number in ExtractPage() is 1-based

                    }

                }

            } 
            Console.WriteLine();
            Console.WriteLine("Press any key...");
            Console.ReadKey();          
        }
    }
}

现在它必须在pdf文件中搜索多个单词。目前字符串searchString =“结束余额”;它只搜索一个特定的单词,但它必须搜索2个单词“Total”&amp; “结束余额”如果此单词包含在pdf文件中的特定行项目中,则执行pdf拆分。