问题描述
是否可以跟踪打开PDF的时间?也许通过将一些脚本嵌入pdf本身?
Is there a way to track when a PDF is opened? Perhaps by embedding some script into the pdf itself?
我看到了下面的问题,我想对于javascript,答案是否",但是我想知道这是否完全可能.
I saw the question below, and I suppose the answer is "no" for javascript, but I am wondering if this is possible at all.
将Google Analytics(分析)跟踪代码插入pdf文件
推荐答案
PDF标准包括对JavaScript的支持,但是正如@Wes Hardaker指出的那样,并不是每个PDF阅读器都支持它.但是,有时候有些总比没有好.
The PDF standard includes support for JavaScript but as @Wes Hardaker pointed out, not every PDF reader supports it. However, sometimes some is better than none.
这是Adobe的官方 Acrobat JavaScript脚本指南.您可能最感兴趣的是doc
对象,该对象具有一种称为getURL()
的方法.要使用它,您只需致电:
Here's Adobe's official Acrobat JavaScript Scripting Guide. What's probably most interesting to you is the doc
object which has a method called getURL()
. To use it you'd just call:
app.doc.getURL('http://www.google.com/');
将该事件绑定到文档的open事件,您将获得一个跟踪器.我对从Adobe Acrobat中创建事件不太熟悉,但是从代码中非常容易.下面的代码是使用开放源代码库iTextSharp(5.1.1.0)的完整运行VS2010 C#WinForms应用程序.它会创建PDF并将JavaScript添加到open事件中.
Bind that event to the document's open event and you've got a tracker. I'm not too familiar with creating events from within Adobe Acrobat but from code its pretty easy. The code below is a full working VS2010 C# WinForms app that uses the open source library iTextSharp (5.1.1.0). It creates a PDF and adds the JavaScript to the open event.
一些注意事项:每当文档访问外部资源时,Adobe Acrobat和Reader都会向用户发出警告.其他大多数PDF阅读器可能也会这样做. 这很烦人,因此仅出于这个原因就不应该这样做. 就我个人而言,我不在乎有人跟踪我的文档是否打开,我只是不想每次都有提示.其次,仅重申一下,此代码适用于Adobe Acrobat和Adobe Reader,可能早在V6之前,但在其他PDF阅读器中可能适用,也可能不适用.第三,没有安全的方法来唯一标识用户.这样做将需要您创建和存储等效的"cookie",这将需要您写入用户的文件系统,这将被认为是不安全的.这意味着您只能检测打开次数,而不能检测到唯一打开.第四,这可能并非到处都是合法的.有些司法管辖区要求您在跟踪用户时通知您,并为他们提供一种查看所收集信息的方式.
Some notes: Adobe Acrobat and Reader will both warn the user whenever a document accesses an external resource. Most other PDF readers will probably do the same. This is very annoying so for this reason alone it shouldn't be done. Personally I don't care if someone tracks my document opens, I just don't want to get a prompt every time. Second, just to reiterate, this code works for Adobe Acrobat and Adobe Reader, probably as far back as at least V6, but may or may not work in other PDF readers. Third, there's no safe way to uniquely identify the user. Doing so would require you to create and store some equivalent of a "cookie" which would require you writing to the user's file system which would be considered unsafe. This means that you could only detect the number of opens, not unique opens. Fourth, this might not be legal everywhere. Some jurisdictions require that you notify users if you are tracking them and provide for a way for them to see what information you are collecting.
但是,尽管如此,我不能仅仅因为我不喜欢它而给出答案.
But with all of the above, I can't not give an answer just because I don't like it.
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();
}
}
}
这篇关于将代码跟踪到PDF或PostScript文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!