Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7068297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:18:24+00:00 2026-05-28T05:18:24+00:00

I’m trying to create a method that will send a PDF file directly to

  • 0

I’m trying to create a method that will send a PDF file directly to my printer (causing the print dialog to appear).

Below is the code I’ve been working on – most of it found in the forums here. It works fine if I use iTextSharp to create a new PDF document, but as soon as I try to inject some JavaScript into an existing file, I get an exception when calling the print() method saying

Object doesn’t support property or method ‘print’

<script type="text/javascript">
    function load() {
        try {
            var x = document.getElementById("frame1");
            x.print();
        }
        catch (err) {
        }
    }
</script>

<body onload="load();">
    <form id="form1" runat="server">
    <div>
       <iframe id="frame1" src="C:/1686850_1.pdf"  runat="server" frameborder="0" style="height: 0px; width: 0px;" />
    </div>
    </form>
</body>
</html>

.CS file

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

public partial class Print : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SetPDF(File.ReadAllBytes("C:\\1686850.pdf"), "C:\\1686850_1.pdf"); //test files
    }

    private void SetPDF(byte[] file, string outputPath)
    {
        PdfReader reader = new PdfReader(file);
        int pageCount = reader.NumberOfPages;
        Rectangle pageSize = reader.GetPageSize(1);

        Document pdf = new Document(pageSize);
        PdfWriter writer = PdfWriter.GetInstance(pdf, new FileStream(outputPath, FileMode.Create));
        pdf.Open();

        //This action leads directly to printer dialogue
        PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer);
        writer.AddJavaScript(jAction);
        //Omitting this loop and simply adding some text to the file produces the behavior I want.
        for (int i = 0; i < pageCount; i++)
        {
            pdf.NewPage();
            PdfImportedPage page = writer.GetImportedPage(reader, i + 1);
            writer.DirectContent.AddTemplate(page, 0, 0);
        }
        pdf.Close();

        //Open the pdf in the frame
        frame1.Attributes["src"] = outputPath;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:18:25+00:00Added an answer on May 28, 2026 at 5:18 am

    I found a way to do this here: http://wskidmore.com/2011/03/pdf-initial-view-settings-itextsharp/

    Based on that, I wrote this code:

    private void PrintMenu()
    {
        ...
        var notUriPath = Server.MapPath("~/" + filePathName);
    
        var doc = new Document();
        var reader = new PdfReader(notUriPath);
        using (var memoryStream = new MemoryStream())
        {
            var writer = PdfWriter.GetInstance(doc, memoryStream);
            doc.Open();
    
            // this action leads directly to printer dialogue
            var jAction = PdfAction.JavaScript("this.print(true);\r", writer);
            writer.AddJavaScript(jAction);
    
            var cb = writer.DirectContent;
            doc.AddDocListener(writer);
    
            for (var p = 1; p <= reader.NumberOfPages; p++)
            {
                doc.SetPageSize(reader.GetPageSize(p));
                doc.NewPage();
                var page = writer.GetImportedPage(reader, p);
                var rot = reader.GetPageRotation(p);
                if (rot == 90 || rot == 270)
                    cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height);
                else
                    cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0);
            }
    
            reader.Close();
            doc.Close();
            File.WriteAllBytes(notUriPath, memoryStream.ToArray());
        }
    
        theIframeForPrint.Attributes.Add("src", fullFilePath);
    }
    

    I hope it helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.