PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Jul 27, 2024 6:23 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Apr 10, 2008 8:56 am 
Offline

Joined: Thu Apr 10, 2008 8:50 am
Posts: 2
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!


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 21, 2010 9:32 pm 
Offline

Joined: Mon Jun 21, 2010 9:06 pm
Posts: 5
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.


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 30, 2010 11:29 am 
Offline

Joined: Thu Dec 30, 2010 11:17 am
Posts: 1
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 50 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group