简体   繁体   中英

Printing PDF without displaying them but displaying the printdialog

I have to print a pdf document, on the click of the print button. Also I need to show the print dialog, but should not display the actual pdf to the user. And based on the print dialog properties selected, the pdf should get printed in the user selected printer.

To the generated pdf , I am able to add the printdialog properties using the below code:-

  writer.SetOpenAction(new PdfAction(PdfAction.PRINTDIALOG));

But I am not able to get the pdf printed when the button is selected. can you please provide me some pointers to achieve this.

Full code:- (PDF is passed as a memorystream to the print button view).

using (MemoryStream m1 = new MemoryStream())
{
    // MemoryStream m1 = new MemoryStream();
    Int32 i = 0;
    PdfWriter writer = PdfWriter.GetInstance(document, m1);
    document.Open();
    PdfContentByte content = writer.DirectContent;

    document.NewPage();

    PdfImportedPage page = writer.GetImportedPage(reader, i + 1);
    content.AddTemplate(page, 0, 0);
    writer.SetOpenAction(new PdfAction(PdfAction.PRINTDIALOG));
    document.Close();

    return m1;

}

You can use Ghostscript to print the pdf file.

If yo need a Ghostscript wrapper for .NET you can take a look at Ghostscript.NET library.

Printing sample via Ghostscript.NET can be found here: https://ghostscriptnet.codeplex.com/discussions/470946

The print dialog is a common dialog, you can find more about how to show print dialog from the .NET here: Show Print Dialog before printing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM