Getting Info (title, subject, author, producer...) from PDF using javascript
Asked Answered
A

2

0

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?

Asymptotic answered 16/6, 2011 at 5:2 Comment(0)
A
0

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

Antilogarithm answered 16/6, 2011 at 5:40 Comment(3)
I tried info.title too but in the second sample that wont work, I guess you are right, it looks like it's not possible to do it from the xml.Asymptotic
can you try app.alert(document) app.alert(this) app.alert(parent)Antilogarithm
app.alert(this) and app.alert(parent) show this alert => [object XFAObject], document doesn't show alert so it's probably not defined. I also tried app.alert(app.doc) because I saw app.doc being used in the url you show me, app exist as an [object App], but both app.doc and app.document are undefined.Asymptotic
L
1

app.alert(event.target.title);

Longoria answered 12/8, 2011 at 22:47 Comment(0)
A
0

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

Antilogarithm answered 16/6, 2011 at 5:40 Comment(3)
I tried info.title too but in the second sample that wont work, I guess you are right, it looks like it's not possible to do it from the xml.Asymptotic
can you try app.alert(document) app.alert(this) app.alert(parent)Antilogarithm
app.alert(this) and app.alert(parent) show this alert => [object XFAObject], document doesn't show alert so it's probably not defined. I also tried app.alert(app.doc) because I saw app.doc being used in the url you show me, app exist as an [object App], but both app.doc and app.document are undefined.Asymptotic

© 2022 - 2024 — McMap. All rights reserved.