1

I have an ASPX page which builds a report. I have a print button which builds a pdf file using ITextSharp. Now I want to print that file.

I have two questions:

How do I print it with out even saving the file ?

and If I can't do this, can I at least print the saved file ?

Thanks in advance.

Jonny
  • 2,787
  • 10
  • 40
  • 62

4 Answers4

5

You cannot use iTextSharp to print PDF document. iTextSharp can be only used for reading or building PDF's.

What you can do is to show it to the user and then he can choose to print it or not.

Here is a sample how to push PDF document to user via C# ASP.NET: How To Write Binary Files to the Browser Using ASP.NET and Visual C# .NET

HABJAN
  • 9,212
  • 3
  • 35
  • 59
  • He wants to print the pdf on the webserver using c#, not send it to the client for printing. – Peter Apr 01 '11 at 11:12
  • It works, thanks, but it doesn't suit my needs. I've found another way around it I'll post my implementation later on – Jonny Apr 01 '11 at 11:23
0

@Jared. Well what we did was to start the acrobat reader with printing parameters after we saved it on the file system. Something like:

   ProcessStartInfo newProcess = new ProcessStartInfo(pdfPath, dfArguments);
   newProcess.CreateNoWindow = true;
   newProcess.RedirectStandardOutput = true;
   newProcess.UseShellExecute = false;

   Process pdfProcess = new Process();
   pdfProcess.StartInfo = newProcess;
   pdfProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
   pdfProcess.Start();
   pdfProcess.WaitForExit();

(please note this is not the actual code we used I got this from here) this should get you started.

For initializing adobe acrobat with printing parameters see this.

Hope it helps.

Community
  • 1
  • 1
Jonny
  • 2,787
  • 10
  • 40
  • 62
-1

In ASP.NET you don't print anything, user does. Most you can do is bring up print dialog, but I personally find it very annoying when a web page suddenly opens a modal dialog.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
  • No that is not what I want. This isn't a fancy website it is more sort of an Intranet for a manufacturing company. Basically my page builds a report and through certain data on the report I build a "Ticket" which they can stick to a container and I generate a barcode. I'm building this "ticket" using ITextSharp and I want to print that pdf – Jonny Apr 01 '11 at 11:07
-1
    #region "GeneratePdf"
    [HttpPost("GeneratePdf")]
    public IActionResult GeneratePdf ([FromBody] GeneratePdf ip)
    {

        Document document = new Document(PageSize.A4, 36, 36, 25, 25);
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Sample1.pdf", FileMode.Create));
        document.Open();

        string imagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images\\Logo.jpg");
        Image image = Image.GetInstance(imagePath);
        image.ScaleAbsolute(100f, 60f);

        float documentWidth = document.PageSize.Width;
        float imageWidth = image.ScaledWidth;
        float documentHeight = document.PageSize.Height;
        float xCoordinate = (documentWidth - imageWidth) - 30f;
        float yCoordinate = (documentHeight - imageWidth) + 30f;
        image.SetAbsolutePosition(xCoordinate, yCoordinate);
        document.Add(image);

        Paragraph paragraph = new Paragraph(ip.Body);
        paragraph.SpacingBefore = 55;
        Font font = new Font(Font.FontFamily.TIMES_ROMAN, 10);
        paragraph.Font = font;
        document.Add(new Paragraph(paragraph));


        Paragraph paragraph2 = new Paragraph("“Enter Whatever Text you Want”.");
        paragraph2.Alignment = Element.ALIGN_CENTER;
        paragraph2.SpacingAfter = 10;
        paragraph2.SpacingBefore = 40;
        document.Add(new Paragraph(paragraph2));

        string imagePath2 = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images\\Footer.png");
        Image image2 = Image.GetInstance(imagePath2);
        image2.ScaleAbsolute(documentWidth, 100f);
        float imageWidth2 = image2.ScaledWidth;
        float x = (documentWidth - imageWidth2);
        image2.SetAbsolutePosition(x, 0f);
        document.Add(image2);

        document.Close();

        Response.ContentType = "application/pdf";
        Response.Headers.Add("Content-Disposition", "attachment; filename=Sample.pdf");
        Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
        Response.Headers.Add("Pragma", "no-cache");
        Response.Headers.Add("Expires", "0");

        FileStream fileStream = new FileStream("Sample.pdf", FileMode.Open, FileAccess.Read);
        return new FileStreamResult(fileStream, "application/pdf");

        
    }
    #endregion