1

I want to embed a javascript snippet inside of a pdf file so that it will immediately print when it's opened from a browser window. To try and achieve this I'm following this example here.

I have created a helper class that has a static method to handle this task. I already have the pdf file path string ready to pass into the method. What I don't understand is how the output stream portion of this works. I would like the updated pdf to be saved to my servers hard drive. I do not want to stream it back to my browser. Any guidance would be greatly appreciated.

public class PdfHelper
{
    public static void AddPrintFunction(string pdfPath, Stream outputStream)
    {
        PdfReader reader = new PdfReader(pdfPath);
        int pageCount = reader.NumberOfPages;
        Rectangle pageSize = reader.GetPageSize(1);

        // Set up Writer 
        PdfDocument document = new PdfDocument();

        PdfWriter writer = PdfWriter.GetInstance(document, outputStream);

        document.Open();

        //Copy each page 
        PdfContentByte content = writer.DirectContent;

        for (int i = 0; i < pageCount; i++)
        {
            document.NewPage();
            // page numbers are one based 
            PdfImportedPage page = writer.GetImportedPage(reader, i + 1);
            // x and y correspond to position on the page 
            content.AddTemplate(page, 0, 0);
        }

        // Inert Javascript to print the document after a fraction of a second to allow time to become visible.
        string jsText = "var res = app.setTimeOut(‘var pp = this.getPrintParams();pp.interactive = pp.constants.interactionLevel.full;this.print(pp);’, 200);";

        //string jsTextNoWait = “var pp = this.getPrintParams();pp.interactive = pp.constants.interactionLevel.full;this.print(pp);”;
        PdfAction js = PdfAction.JavaScript(jsText, writer);
        writer.AddJavaScript(js);

        document.Close();

    }
} 
Stavros_S
  • 2,145
  • 7
  • 31
  • 75
  • Sigh. Who showed you this manipulating an existing document using PdfWriter and all these getImportedPage calls? Why don't you simply use PdfStamper? – mkl Mar 05 '15 at 23:20
  • I followed the code on this link as an example, it's pretty old though. Seems difficult finding documentation for itextsharp. http://endlessobsession.com/blog/add-javascript-to-a-pdf-document-with-itextsharp/ – Stavros_S Mar 06 '15 at 00:48
  • Well, as the author of that blog says "the code may be a little rough. Its the sort of code that works, but isn’t fully understood"... – mkl Mar 06 '15 at 07:03

1 Answers1

0

For how to accomplish this task, please take a look at this and this SO posts.

Basically you should have something like this:

var pdfLocalFilePath = Server.MapPath("~/sourceFile.pdf");
var outputLocalFilePath = Server.MapPath("~/outputFile.pdf");
using (var outputStream = new FileStream(outputLocalFilePath, FileMode.CreateNew))
{
    AddPrintFunction(pdfLocalFilePath, outputStream);
    outputStream.Flush();
} 
Community
  • 1
  • 1
Ramanagom
  • 329
  • 2
  • 8
  • This code is not in a web app but rather in a web api controller on my server, therefore; Server.MapPath() is not available to me. – Stavros_S Mar 06 '15 at 02:04
  • @Stavros_S, if you're using Web API for ASP.NET, then HttpContext.Current.Server.MapPath does exist for sure... Here is an example of how to upload a file (and also store it locally on the web server) using Web API for ASP.NET: http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-2. – Ramanagom Mar 06 '15 at 07:05
  • To the reader who downvoted my comment/post - I'd appreciate if you'd like to explain little bit more why you do not agree with what I'm saying... – Ramanagom Dec 04 '15 at 08:33