Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a PDF that automatically prints

I have a ASP.NET Web application that generates a PDF. I am using iTextSharp. What happens is that you click a button and it downloads. My employer want to be able to click the button and have it open with the print dialog.

like image 442
Joe Tyman Avatar asked May 29 '11 13:05

Joe Tyman


2 Answers

Method 1: Using embedded javascript inside your PDF files You can try creating an iText PDFAction object with a javascript call this.print(false) (you can use new PdfAction(PdfAction.PRINTDIALOG) for this), and associate it with the OpenAction event of your pdf file.

The code in iText Java should look like this:

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("file.pdf"));
...
PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
writer.setOpenAction(action);
...

It should not be too diferent in C#.

As a side note, this is also possible with Amyuni PDF Creator .Net by setting the attribute "AutoPrint" to TRUE in the document class (usual disclaimer applies).

acPDFCreatorLib.Initialize();
acPDFCreatorLib.SetLicenseKey("Amyuni Tech.", "07EFCDA00...BC4FB9CFD");
Amyuni.PDFCreator.IacDocument document = pdfCreator1.Document;

// Open a PDF document from file
System.IO.FileStream file1 = new System.IO.FileStream("test_input.pdf", FileMode.Open, FileAccess.Read, FileShare.Read);

IacDocument document = new IacDocument(null);

if (document.Open(file1, ""))
{
    //Set AutoPrint
    document.Attribute("AutoPrint").Value = true;

    //Save the document
    System.IO.FileStream file2 = new System.IO.FileStream("test_output.pdf", System.IO.FileMode.Create, System.IO.FileAccess.Write);
    document.Save(file2, Amyuni.PDFCreator.IacFileSaveOption.acFileSaveView);
}

// Disposing the document before closing the stream clears out any data structures used by the Document object
document.Dispose();

file1.Close();

// terminate library to free resources
acPDFCreatorLib.Terminate();

This approach requires the PDF file to be opened in a reader that will take care of printing, and it has the drawback that if the file is saved locally, every time the file is opened later on it will show the print dialog.

Method 2: Using javascript from the browser to communicate with the reader that shows the file.
I found this other approach in this SO question that might worth trying:

<html>
<script language="javascript">
timerID = setTimeout("exPDF.print();", 1000);
</script>
<body>
<object id="exPDF" type="application/pdf" data="111.pdf" width="100%" height="500"/>
</body>
</html>

The idea is to use javascript in the browser to instruct the PDF reader to print the file. This approach will work on PDF files embedded in a HTML page.

like image 128
yms Avatar answered Oct 03 '22 01:10

yms


Another solution on this site... I use this solution and work great

I have a PDF Stream from Crystal report, and i add the openaction with pdfsharp

link : http://www.vo1dmain.info/pdfsharp-howto-inject-javascript-into-pdf-autoprinting-functionality#comments

public static MemoryStream AddAutoPrint(Stream pdfStream, bool ShowPrintDialog = true, int NumCopies = 1)
{
  PdfSharp.Pdf.PdfDocument doc = PdfSharp.Pdf.IO.PdfReader.Open(pdfStream, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import);
  PdfSharp.Pdf.PdfDocument outputDocument = new PdfSharp.Pdf.PdfDocument();

  for (int idx = 0; idx < doc.PageCount; idx++)
  {
    PdfSharp.Pdf.PdfPage p = doc.Pages[idx];
    outputDocument.AddPage(p);
  }

  outputDocument.Info.Author = "author name";

  string JSScript = string.Empty;
  JSScript += "var pp = this.getPrintParams(); ";

  if(NumCopies > 0)
  {
    JSScript += "pp.NumCopies = " + NumCopies.ToString() + "; ";
  }

  if(!ShowPrintDialog)
  {
     JSScript += "pp.interactive = pp.constants.interactionLevel.automatic; ";
  }


  JSScript += "this.print({printParams: pp}); ";


  PdfSharp.Pdf.PdfDictionary dictJS = new PdfSharp.Pdf.PdfDictionary();
  dictJS.Elements["/S"] = new PdfSharp.Pdf.PdfName("/JavaScript");
  //dictJS.Elements["/JS"] = new PdfSharp.Pdf.PdfStringObject(outputDocument, "print(true);");
  //dictJS.Elements["/JS"] = new PdfSharp.Pdf.PdfStringObject(outputDocument, "this.print({bUI: false, bSilent: true, bShrinkToFit: true});");
  //dictJS.Elements["/JS"] = new PdfSharp.Pdf.PdfStringObject(outputDocument, "var pp = this.getPrintParams(); pp.NumCopies = 2; pp.interactive = pp.constants.interactionLevel.automatic; this.print({printParams: pp});");
  dictJS.Elements["/JS"] = new PdfSharp.Pdf.PdfStringObject(outputDocument, JSScript);


  outputDocument.Internals.AddObject(dictJS);

  PdfSharp.Pdf.PdfDictionary dict = new PdfSharp.Pdf.PdfDictionary();
  PdfSharp.Pdf.PdfArray a = new PdfSharp.Pdf.PdfArray();
  dict.Elements["/Names"] = a;
  a.Elements.Add(new PdfSharp.Pdf.PdfString("EmbeddedJS"));
  a.Elements.Add(PdfSharp.Pdf.Advanced.PdfInternals.GetReference(dictJS));

  outputDocument.Internals.AddObject(dict);

  PdfSharp.Pdf.PdfDictionary group = new PdfSharp.Pdf.PdfDictionary();
  group.Elements["/JavaScript"] = PdfSharp.Pdf.Advanced.PdfInternals.GetReference(dict);

  outputDocument.Internals.Catalog.Elements["/Names"] = group;

  MemoryStream ms = new MemoryStream();

  outputDocument.Save(ms, false);

  return ms;
}
like image 22
solexD Avatar answered Oct 02 '22 23:10

solexD