我正在开发一个项目,其中应将所选 PDF 中的文本复制到光标聚焦的输入文本框。
有 2 个窗户。
1:输入文本框在哪里 2:PDF在那里。
有没有办法复制从 PDF 窗口中选择的文本并使用 Javascript 粘贴到另一个窗口中的输入文本框?
下面是我尝试过的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<script>
function getSelectionText() {
var text = "";
var activeEl = document.activeElement;
var activeElTagName = activeEl ? activeEl.tagName.toLowerCase() : null;
if (
(activeElTagName == "textarea") || (activeElTagName == "input" &&
/^(?:text|search|password|tel|url)$/i.test(activeEl.type)) &&
(typeof activeEl.selectionStart == "number")
) {
text = activeEl.value.slice(activeEl.selectionStart, activeEl.selectionEnd);
} else if (window.getSelection) {
text = window.getSelection().toString();
}
return text;
}
document.onmouseup = document.onkeyup = document.onselectionchange = function () {
document.getElementById("sel").value = getSelectionText();
};
</script>
<body>
Selection:
<br>
<textarea id="sel" rows="3" cols="50"></textarea>
<p>Please select this text.</p>
<input value="Some text in a text input">
<iframe height="300" width="700" src="https://www.africau.edu/images/default/sample.pdf" >
</body>
</html>
任何帮助都会很棒。
您可以使用以下代码 但是你必须根据你的需要调整origin策略,这样代码才能工作
document.getElementById("Your_Iframe_Tag_ID").contentWindow.document.getSelection()