Can't print jpg with Aspose.Pdf

636 Views Asked by At

I want to load in a JPEG file and print it with Aspose.Pdf in C# (.net Framework 4.8). The code I currently have is:

public void PrintImage(string fileToPrint, string printerName, string jobName)
    {
      System.Drawing.Image srcImage = System.Drawing.Image.FromFile(fileToPrint);
      int h = srcImage.Height;
      int w = srcImage.Width;

      var doc = new Document();
      var page = doc.Pages.Add();
      var image = new Image();
      image.File = (fileToPrint);

      page.PageInfo.Height = (h);
      page.PageInfo.Width = (w);
      page.PageInfo.Margin.Bottom = (0);
      page.PageInfo.Margin.Top = (0);
      page.PageInfo.Margin.Right = (0);
      page.PageInfo.Margin.Left = (0);

      page.Paragraphs.Add(image);

      var viewer = new PdfViewer(doc);
      PrintUsingViewer(viewer, printerName, jobName);
    }

    private static void PrintUsingViewer(PdfViewer viewer, string printerName, string jobName)
    {
      viewer.AutoResize = true;         // Print the file with adjusted size
      viewer.AutoRotate = true;         // Print the file with adjusted rotation
      viewer.PrintPageDialog = false;   // Do not produce the page number dialog when printing

      var ps = new System.Drawing.Printing.PrinterSettings();
      var pgs = new System.Drawing.Printing.PageSettings();

      ps.PrinterName = printerName;
      viewer.PrinterJobName = jobName;

      viewer.PrintDocumentWithSettings(pgs, ps);
      viewer.Close();
    }

When I save the document instead of printing and look at it, it seems fine (the image is added). However, when trying to print the image it is not printed and the page is just blank..

I would like to print without first saving the document as a PDF and then trying to print that saved PDF. Does anyone see what I am doing wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

The solution was adding this line of code

doc.ProcessParagraphs();

right after this line:

page.Paragraphs.Add(image);

So the code now becomes

public void PrintImage(string fileToPrint, string printerName, string jobName)
    {
      System.Drawing.Image srcImage = System.Drawing.Image.FromFile(fileToPrint);
      int h = srcImage.Height;
      int w = srcImage.Width;

      var doc = new Document();
      var page = doc.Pages.Add();
      var image = new Image();
      image.File = (fileToPrint);

      page.PageInfo.Height = (h);
      page.PageInfo.Width = (w);
      page.PageInfo.Margin.Bottom = (0);
      page.PageInfo.Margin.Top = (0);
      page.PageInfo.Margin.Right = (0);
      page.PageInfo.Margin.Left = (0);

      page.Paragraphs.Add(image);
      doc.ProcessParagraphs();

      var viewer = new PdfViewer(doc);
      PrintUsingViewer(viewer, printerName, jobName);
    }

    private static void PrintUsingViewer(PdfViewer viewer, string printerName, string jobName)
    {
      viewer.AutoResize = true;         // Print the file with adjusted size
      viewer.AutoRotate = true;         // Print the file with adjusted rotation
      viewer.PrintPageDialog = false;   // Do not produce the page number dialog when printing

      var ps = new System.Drawing.Printing.PrinterSettings();
      var pgs = new System.Drawing.Printing.PageSettings();

      ps.PrinterName = printerName;
      viewer.PrinterJobName = jobName;

      viewer.PrintDocumentWithSettings(pgs, ps);
      viewer.Close();
    }

Now the image is printed correctly!