将代码跟踪到PDF或PostScript文件中

spe*_*ane 5 pdf pdf-generation tracking postscript

有没有办法跟踪何时打开PDF?也许通过将一些脚本嵌入到pdf本身中?

我看到下面的问题,我想javascript的答案是"不",但我想知道这是否可行.

Google Analytics(分析)跟踪代码插入pdf文件中

Chr*_*aas 12

PDF标准包括对JavaScript的支持,但正如@Wes Hardaker指出的那样,并非每个PDF阅读器都支持它.但是,有时候一些比没有好.

这是Adobe官方的Acrobat JavaScript脚本编写指南.你可能最感兴趣的是doc具有一个方法的对象getURL().要使用它你只需要打电话:

app.doc.getURL('http://www.google.com/');
Run Code Online (Sandbox Code Playgroud)

将该事件绑定到文档的公开事件,并且您有一个跟踪器.我不太熟悉从Adobe Acrobat中创建事件,但从代码中很容易.下面的代码是一个完整的VS2010 C#WinForms应用程序,它使用开源库iTextSharp(5.1.1.0).它创建一个PDF并将JavaScript添加到open事件中.

一些注意事项:只要文档访问外部资源,Adobe Acrobat和Reader都会警告用户.大多数其他PDF阅读器可能也会这样做.这非常令人讨厌,因此仅靠这个原因不应该这样做.我个人并不关心是否有人跟踪我的文档打开,我只是不想每次都得到提示.其次,重申一下,此代码适用于Adobe Acrobat和Adobe Reader,可能至少可以使用V6,但在其他PDF阅读器中可能有效,也可能无效.第三,没有安全的方法来唯一地识别用户.这样做会要求您创建和存储一些"cookie",这需要您写入用户的文件系统,这将被视为不安全.这意味着您只能检测打开的数量,而不是唯一的打开数量.第四,这可能在任何地方都不合法.某些司法管辖区要求您在跟踪用户时通知用户,并为他们提供了查看您收集的信息的方法.

但综合以上所述,我不能仅仅因为我不喜欢而给出答案.

using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //File that we will create
            string OutputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Events.pdf");

            //Standard PDF creation setup
            using (FileStream fs = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(PageSize.LETTER))
                {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
                    {
                        //Open our document for writing
                        doc.Open();

                        //Create an action that points to the built-in app.doc object and calls the getURL method on it
                        PdfAction act = PdfAction.JavaScript("app.doc.getURL('http://www.google.com/');", writer);

                        //Set that action as the documents open action
                        writer.SetOpenAction(act);

                        //We need to add some content to this PDF to be valid
                        doc.Add(new Paragraph("Hello"));

                        //Close the document
                        doc.Close();
                    }
                }
            }

            this.Close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)