简体   繁体   English

无法使用 Aspose.Pdf 打印 jpg

[英]Can't print jpg with Aspose.Pdf

I want to load in a JPEG file and print it with Aspose.Pdf in C# (.net Framework 4.8).我想加载一个 JPEG 文件并在 C#(.net Framework 4.8)中使用 Aspose.Pdf 打印它。 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.我想打印而不先将文档保存为 PDF,然后尝试打印保存的 PDF。 Does anyone see what I am doing wrong?有谁看到我做错了什么?

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!现在图像打印正确!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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