iTextSharp PDF printing

后端 未结 2 1190
感情败类
感情败类 2021-01-06 08:32

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

相关标签:
2条回答
  • 2021-01-06 09:12

    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 讨论(0)
  • 2021-01-06 09:18

    You want to add Javascript to the PDF to open the print dialog, not the web page (unless you really do want the web page print dialog, not the PDF print dialog). I have done this before, but not with iTextSharp; but a quick Google search should tell you how to add Javascript using iTextSharp to open the print dialog.

    0 讨论(0)
提交回复
热议问题