简体   繁体   中英

Get selected text from PDF in web browser control in windows form

I need to know if I can get the selected text from a pdf that has been loaded into a web browser control that is in a windows form. I am using C# and Visual Studio 2008 with .net 3.5 and down. I have the pdf showing in the control but I can not figure out how to get access to the selected text within that document. The mshtml document is null.

Thanks in advance!

To display a PDF you need to render it as an image or a different graphical language like EPS, EMF/WMF etc. So unless some work has been done to overlay the text from the PDF on top of the image, you won't be able to physically select the text.

Have you been able to select text from the PDF when it is shown in the web browser control? If you haven't, then you might need to consider trying to access the text directly from the PDF through a library of some sort.

try

webBrowser2.Document.ExecCommand("SelectAll", false, null);
webBrowser2.Document.ExecCommand("Copy", false, null);

Let me know if it works !!

Document is null so you cannot call ExecCommand on it. The text can be selected, however it resides inside a .PDF viewer embedded in the web-browser.

Cut and paste of the text can be accomplished manually, but how do we do it programmatically?

That`s easy with below code:

public static string GetAllText(WebBrowser webBrowser)
{
            webBrowser.Focus();
            SendKeys.Send("^a");
            SendKeys.Send("^c");
            return ClipBoard.GetText();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM