PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Add Document Javascript to PDF
https://forum.pdfsharp.net/viewtopic.php?f=2&t=368
Page 1 of 1

Author:  Patrick [ Thu Apr 10, 2008 8:56 am ]
Post subject:  Add Document Javascript to PDF

Hi there.

I guess this is a simple task, but I cannot find the needed information anywhere. Perhaps someone can help.

I like to add document-javascript-code to an existing pdf document. For example lets just add 'this.print(true);'

How could this be done? Is there an complete API documentation?

Thanks for any help!

Author:  jlemon [ Mon Jun 21, 2010 9:32 pm ]
Post subject:  Re: Add Document Javascript to PDF

I had a similar requirement to yours. I figured I would share what changes I had to make to get it to work. I started out by hand editing a PDF and added an OpenAction command to the PDF file's catalog definition.

/OpenAction << /S /JavaScript /JS (var pp = this.getPrintParams\(\); pp.pageHandling = pp.constants.handling.none; pp.printerName = 'LaserJet Printer'; this.print\(pp\);) >>

This tells the PDF reader to run these javascript commands when the reader opens the document. It gets the print parameters, sets the page handling type to none, sets the printer to a known printer on my system, and then calls the print command.

I made the follwing changes in the source code of PDFsharp v1.3:

Added this function to the PDFDocument.cs file in the PdfSharp.Pdf namespace

/// <summary>
/// Gets or sets a JavaScript string to execute when opening the pdf.
/// </summary>
public string JavaScriptOpenAction
{
get { return Catalog.JavascriptOpenAction; }
set
{
if (!CanModify)
throw new InvalidOperationException(PSSR.CannotModify);
Catalog.JavascriptOpenAction = value;
}
}

Added this function to the PDFCatalog.cs file in the PdfSharp.Pdf.Advanced namespace

/// <summary>
/// Implementation of JavaScriptOpenAction
/// </summary>
public string JavascriptOpenAction
{
get { return Elements.GetString(Keys.OpenAction); }
set
{
if (value == null)
Elements.Remove(Keys.OpenAction);
else
Elements.SetJavaScript(Keys.OpenAction, value);
}
}

Added these functions to the PDFDictionary.cs file in the PdfSharp.Pdf namespace

/// <summary>
/// Converts the specified value to JavaScript String.
/// If the value not exists, the function returns the empty string.
/// </summary>
public string GetJavaScript(string key, bool create)
{
object obj = this[key];
if (obj == null)
{
if (create)
this[key] = new PdfJavaScript();
return "";
}

if (obj is PdfJavaScript)
return ((PdfJavaScript)obj).Value;

throw new InvalidCastException("GetJavaScript: Object is not a JavaScript string.");
}

/// <summary>
/// Converts the specified value to a JavaScript String.
/// If the value not exists, the function returns the empty string.
/// </summary>
public string GetJavaScript(string key)
{
return GetJavaScript(key, false);
}

/// <summary>
/// Sets the entry to a JavaScript string value;
/// </summary>
public void SetJavaScript(string key, string value)
{
this[key] = new PdfJavaScript(value);
}

Added a new file called PdfJavascript.cs in the PdfSharp.Pdf namespace

using System;
using System.Globalization;
using System.Collections;
using System.Text;
using System.IO;
using PdfSharp.Pdf.IO;
using PdfSharp.Internal;


namespace PdfSharp.Pdf
{
/// <summary>
/// Represents a javascript script.
/// </summary>
public sealed class PdfJavaScript : PdfItem
{
/// <summary>
/// Initialized a new instance of the <see cref="PdfJavaScript"/> class.
/// </summary>
public PdfJavaScript()
{ }

/// <summary>
/// Initializes a new instance of the <see cref="PdfJavaScript"/> class.
/// </summary>
public PdfJavaScript(string value)
{
this.value = value;
}

/// <summary>
/// Gets the value as a string.
/// </summary>
public string Value
{
//This class must behave like a value type. Therefore it cannot be changed (like System.String).
get { return this.value; }
}
string value;

/// <summary>
/// Returns the string.
/// </summary>
public override string ToString()
{
return this.value.ToString();
}

internal override void WriteObject(PdfWriter writer)
{
writer.Write(this);
}
}
}

Added this function to the PDFWriter.cs file in the PdfSharp.Pdf.IO namespace

/// <summary>
/// Writes the specified value to the PDF stream.
/// </summary>
public void Write(PdfJavaScript value)
{
WriteRaw("<< /S /JavaScript /JS (" + value.Value.ToString() + ") >>");
}

Then I am able to set the JavaScriptOpenAction property when I am creating my PDF file to set the needed javascript.

MemoryStream ms = null;
PdfDocument pdfPrintDoc = new PdfDocument();
PdfSharp.PageSize pdfSize = PdfSharp.PageSize.Letter;
pdfPrintDoc.JavaScriptOpenAction = "var pp = this.getPrintParams(); pp.pageHandling = pp.constants.handling.none; pp.printerName = 'LaserJet Printer'; this.print(pp);";

//Generate PDF Content Here. . .

pdfPrintDoc.Save(ms, false);

I hope this helps you with your requirement. I looked on the forums for an answer to this question, but did not find one provided by the PDFsharp project already. I know this may not fulfill your exact requirement, but it may provide you with a starting point on how to change PDFsharp to complete your task.

Author:  voidmain [ Thu Dec 30, 2010 11:29 am ]
Post subject:  Re: Add Document Javascript to PDF

Hello,

Patrick wrote:
Hi there.
I guess this is a simple task, but I cannot find the needed information anywhere. Perhaps someone can help.
I like to add document-javascript-code to an existing pdf document. For example lets just add 'this.print(true);'
How could this be done? Is there an complete API documentation?
Thanks for any help!


I have described full solution to this here: http://www.vo1dmain.info/pdfsharp-howto ... ctionality

And by the way, great job, PdfSharp guys, you've made the library really extensible, such custom things as this can be implemented in a non-hacky way.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/