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();

    }
} 
3
  • Sigh. Who showed you this manipulating an existing document using PdfWriter and all these getImportedPage calls? Why don't you simply use PdfStamper? Commented Mar 5, 2015 at 23:20
  • I followed the code on this link as an example, it's pretty old though. Seems difficult finding documentation for itextsharp. endlessobsession.com/blog/… Commented Mar 6, 2015 at 0: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"... Commented Mar 6, 2015 at 7:03

1 Answer 1

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();
} 
Sign up to request clarification or add additional context in comments.

3 Comments

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, 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: asp.net/web-api/overview/advanced/….
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.