使用 javascript 从 PDF 获取信息(标题、主题、作者、制作人...)

发布于 2024-11-15 16:27:48 字数 2298 浏览 6 评论 0原文

我试图学习如何在 pdf 文件中使用 javascript,以及如何从 /Info 对象中获取信息,例如作者和标题。我发现了两种在 pdf 中使用 js 的方法,并且我整理了 2 个示例,第一个有效,第二个无效。它们都执行 javascript,但第二个代码无法访问 /Info 数据。我不明白为什么。

示例 1(此代码显示带有 /Title 内容的警报,工作正常):

%PDF-1.3
1 0 obj
<</Type/Catalog/Pages 5 0 R/OpenAction 3 0 R>>
endobj
2 0 obj
<<
/Producer (test)
/Subject (test)
/Title (test)
>>
endobj
3 0 obj
<</Type/Action/S/JavaScript/JS 4 0 R>>
endobj
4 0 obj
<</Length 17>>
stream
app.alert(title);
endstream
endobj
5 0 obj
<<
>>
endobj
xref
trailer
<<
/Root 1 0 R
/Info 2 0 R
>>
startxref
%%EOF

示例 2(此代码不显示任何警报,但如果我用字符串替换标题,它确实显示了警报);

%PDF-1.3
1 0 obj
<</Type/Catalog/Pages 5 0 R/AcroForm 3 0 R>>
endobj
2 0 obj
<<
/Producer (test)
/Subject (test)
/Title (test)
>>
endobj
3 0 obj
<</XFA [4 0 R]>>
endobj
4 0 obj
<</Length 767>>
stream
<?xml version="1.0" encoding="UTF-8" ?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<config xmlns="http://www.xfa.org/schema/xci/1.0/"><present>
<pdf><interactive>1</interactive><version>1.6</version>
</pdf><xdp><packets>*</packets></xdp><destination>pdf</destination></present></config>
<template xmlns="http://www.xfa.org/schema/xfa-template/2.5/">
    <subform layout="tb" locale="en_US" name="form">
        <subform>
            <field>
                <event activity="initialize" name="eventName">
                        <script contentType="application/x-javascript">
                        app.alert(title)
                        </script>
                </event>
            </field>
        </subform>
    </subform>
</template>
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data></xfa:data></xfa:datasets>
</xdp:xdp>
endstream
endobj
5 0 obj
<<
>>
endobj
xref
trailer
<<
/Root 1 0 R
/Info 2 0 R
>>
startxref
%%EOF

(两个 pdf 源都不是真正有效的 pdf,但 js 被执行,我删除了很多内容以使它们更易于阅读)

有人知道为什么吗app.alert(title) 没有处理第二个样品?

Im trying to learn how to use javascript inside pdf files, and how to grab information from the /Info object like author and title. I've found two ways to use js inside a pdf, and I've put together 2 samples, the first works, the second doesn't. They both execute javascript, but the second code can't access the /Info data. I can't figure out why.

Sample 1 (This code shows an alert with the contents of /Title, works fine):

%PDF-1.3
1 0 obj
<</Type/Catalog/Pages 5 0 R/OpenAction 3 0 R>>
endobj
2 0 obj
<<
/Producer (test)
/Subject (test)
/Title (test)
>>
endobj
3 0 obj
<</Type/Action/S/JavaScript/JS 4 0 R>>
endobj
4 0 obj
<</Length 17>>
stream
app.alert(title);
endstream
endobj
5 0 obj
<<
>>
endobj
xref
trailer
<<
/Root 1 0 R
/Info 2 0 R
>>
startxref
%%EOF

Sample 2 (this one doesn't show any alert, but if i replace title with a string, it does show the alert);

%PDF-1.3
1 0 obj
<</Type/Catalog/Pages 5 0 R/AcroForm 3 0 R>>
endobj
2 0 obj
<<
/Producer (test)
/Subject (test)
/Title (test)
>>
endobj
3 0 obj
<</XFA [4 0 R]>>
endobj
4 0 obj
<</Length 767>>
stream
<?xml version="1.0" encoding="UTF-8" ?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<config xmlns="http://www.xfa.org/schema/xci/1.0/"><present>
<pdf><interactive>1</interactive><version>1.6</version>
</pdf><xdp><packets>*</packets></xdp><destination>pdf</destination></present></config>
<template xmlns="http://www.xfa.org/schema/xfa-template/2.5/">
    <subform layout="tb" locale="en_US" name="form">
        <subform>
            <field>
                <event activity="initialize" name="eventName">
                        <script contentType="application/x-javascript">
                        app.alert(title)
                        </script>
                </event>
            </field>
        </subform>
    </subform>
</template>
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data></xfa:data></xfa:datasets>
</xdp:xdp>
endstream
endobj
5 0 obj
<<
>>
endobj
xref
trailer
<<
/Root 1 0 R
/Info 2 0 R
>>
startxref
%%EOF

(Both pdf sources are not really valid pdfs but the js gets executed, i removed a lot of stuff to make them easier to read)

Does someone knows why app.alert(title) doesn't work on the second sample?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2024-11-22 16:27:48

app.alert(event.target.title);

app.alert(event.target.title);

青春如此纠结 2024-11-22 16:27:48

这确实是一条评论,但出于格式和可读性原因,我使用答案。

http://corkami.googlecode.com/svn-history/r503/wiki /PDFTricks.wiki

如果您有

/Info <>

那么你可以做

app.alert(info.author + info.title + info. Producer);

在你的情况下,我猜你需要获取文档你排在第一位。我怀疑你能做你想做的事,因为你似乎在 pdf 中嵌入了 xml 文件。我没想到它会起作用

This is really a comment, but I use answer for formatting and readability reasons.

http://corkami.googlecode.com/svn-history/r503/wiki/PDFTricks.wiki

If you have

/Info <</Author(Hello) /Title( World) /Producer( !)>>

then you can do

app.alert(info.author + info.title + info.producer);

In your case I would guess you need to get the document you are in first. I doubt you can do what you want since you seem to be embedding an xml file inside the pdf. I would not expect it to work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文