首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PDF中查找特定文本,然后将在PDF中找到的页码返回到另一节注意:使用Docotic.pdf

在PDF中查找特定文本,然后将在PDF中找到的页码返回到另一节注意:使用Docotic.pdf
EN

Stack Overflow用户
提问于 2013-03-01 23:28:17
回答 1查看 1.6K关注 0票数 1

在下面的代码中,用户将输入搜索字符串(barcodedata)。然后,该字符串将被截断为前5个字符,并用作作业编号。jobnumber,也是我将扫描条形码数据的pdf的名称。

我想要的是“找到它”按钮来执行下面的代码,然后将找到的值返回到startpagedata。

我不知道是程序实际上没有扫描PDF中的搜索字符串,还是值没有返回给程序。

代码语言:javascript
复制
using BitMiracle.Docotic.Pdf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
using Acrobat;

namespace BarCodeReader
{
    public partial class Form1 : Form
    {
        public string stringsToFind;
        public string pathtofile;

        public Form1()
        {
            InitializeComponent();
        }

        private void barcodedata_TextChanged(object sender, EventArgs e)
        {
            stringsToFind=barcodedata.Text;
            pathtofile = "C:\\" + StringTool.Truncate(barcodedata.Text, 5) + ".pdf";
            jobnumberdata.Text = StringTool.Truncate(barcodedata.Text, 5);
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void jobnumberdata_TextChanged(object sender, EventArgs e)
        {
            jobnumberdata.Text = jobnumberdata.Text.TrimStart('0');
            Console.WriteLine(jobnumberdata.Text);
        }

        private void startpagedata_TextChanged(object sender, EventArgs e)
        {
            Console.WriteLine(startpagedata.Text);
        }

        private void piecesdata_TextChanged(object sender, EventArgs e)
        {
        }

        private void FindIt_Click(object sender, EventArgs e)
        {
            PdfDocument pdf = new PdfDocument(pathtofile);
            for (int i = 0; i < pdf.Pages.Count; i++)
            {
                string pageText = pdf.Pages[i].GetText();
                int count = 0;
                int lastStartIndex = pageText.IndexOf(stringsToFind, 0, StringComparison.CurrentCultureIgnoreCase);
                while (lastStartIndex != -1)
                {
                    count++;
                    lastStartIndex = pageText.IndexOf(stringsToFind, lastStartIndex + 1, StringComparison.CurrentCultureIgnoreCase);
                }

                if (count != 0)
                    startpagedata.Text = Convert.ToString(lastStartIndex);
            }

        }
    }

    public static class StringTool
    {
        /// <summary>
        /// Get a substring of the first N characters.
        /// </summary>
        public static string Truncate(string source, int length)
        {
            if (source.Length > length)
            {
                source = source.Substring(0, length);
            }
            return source;
        }

        /// <summary>
        /// Get a substring of the first N characters. [Slow]
        /// </summary>
        public static string Truncate2(string source, int length)
        {
            return source.Substring(0, Math.Min(length, source.Length));
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2013-04-23 20:03:26

为了使用Docotic.pdf,您可以使用Acrobat.dll来查找当前页码。首先打开pdf文件并使用以下命令搜索字符串

代码语言:javascript
复制
Acroavdoc.open("Filepath","Temperory title") 

代码语言:javascript
复制
Acroavdoc.FindText("String").

如果在此pdf文件中找到该字符串,则光标移动到特定页面,搜索到的字符串将突出显示。现在我们使用Acroavpageview.GetPageNum()来获取当前页码。

代码语言:javascript
复制
Dim AcroXAVDoc As CAcroAVDoc
Dim Acroavpage As AcroAVPageView
Dim AcroXApp As CAcroApp

AcroXAVDoc = CType(CreateObject("AcroExch.AVDoc"), Acrobat.CAcroAVDoc)
AcroXApp = CType(CreateObject("AcroExch.App"), Acrobat.CAcroApp)

AcroXAVDoc.Open("File path", "Original document")
AcroXAVDoc.FindText("String is to searched", True, True, False)

Acroavpage = AcroXAVDoc.GetAVPageView()

Dim x As Integer = Acroavpage.GetPageNum
MsgBox("the string found in page number" & x) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15161209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档