+1 800 256 3608 (toll-free in North America) or +49 7531 90 60 10| service@combit.com

Ausdrucken eines Reports auf verschiedene Druckerschächte


(Guest) #1

Hallo zusammen

Ich habe mich jetzt mehr als einen Tag durch das Forum, die Entwicklerdoku und die FAQs gearbeitet und viel ausprobiert bis ich eine funktionierende Lösung gefunden habe. Um euch das zu ersparen poste ich hier mal meine Lösung dazu:

public static class LLPrintHelper
{
  private static PrinterSettings defaultPrinter = null;
  private static PaperSource defaultPrinterTray = null;
  private static PaperSource specialPrinterTray = null;

  private static IntPtr storageHandle;
  private static int page = 1;
  private static int nmbrOfPages;

  public static bool printInvoice(int auftragsnummer)
  {
    FileInfo outputFile = new FileInfo(Path.Combine(PathHelper.AppBaseDir, "output.pdf"));
    if (outputFile.Exists)
      outputFile.Delete();
    var headerData = EODBHelper.getInvoiceHeadData(auftragsnummer);
    var tableData = EODBHelper.getArtikelPos(auftragsnummer);
    var reportPath = Path.Combine(PathHelper.AppBaseDir, "ListLabelReport", "eo_bestaetigung.lst");

    using (var LL = new ListLabel())
    {
      LLHelper.licenceLL(LL);
      LLHelper.setCommonLLOptions(LL);
      LL.DataSource = tableData;
      addHeaderVariable(headerData, LL);
      LL.ExportOptions.Clear();
      var previewFile = new FileInfo(outputFile.FullName.Replace(outputFile.Extension, ".LL"));
      LL.ExportOptions.Add(LlExportOption.ExportTarget, "PRV");
      LL.ExportOptions.Add(LlExportOption.ExportPath, previewFile.Directory.FullName);
      LL.ExportOptions.Add(LlExportOption.ExportFile, previewFile.Name);
      LL.ExportOptions.Add(LlExportOption.ExportQuiet, "1");
      LL.AutoProjectFile = reportPath;
      LL.Print();
      page = 1;
      storageHandle = LlCore.LlStgsysStorageOpen(outputFile.FullName, null, true, true);
      nmbrOfPages = LlCore.LlStgsysGetPageCount(storageHandle);

      //PrinterSettings
      var settings = new PrinterSettings();
      settings.PrinterName = EOBackOfficeDBHelper.getConfigValue("PrinterSettings_DefaultPrinter");
      defaultPrinter = settings;

      //PrinterTrays
      var defaultTrayName = EOBackOfficeDBHelper.getConfigValue("PrinterSettings_SelectedTrayOfDefaultPrinter");
      var specialTrayName = EOBackOfficeDBHelper.getConfigValue("PrinterSettings_SelectedTrayOfSpecialPrinter");
      PrinterSettings.PaperSourceCollection collection = settings.PaperSources;
      for (int i = 0; i < collection.Count; i++)
      {
        var sourceName = collection[i].SourceName;
        if (sourceName == defaultTrayName)
          defaultPrinterTray = defaultPrinter.PaperSources[i];
        if (sourceName == specialTrayName)
          specialPrinterTray = defaultPrinter.PaperSources[i];
      }

      PrintDocument pd = new PrintDocument();
      pd.PrinterSettings = defaultPrinter;
      pd.DocumentName = "Rechnung Nr. " + auftragsnummer;
      pd.PrintPage += OnPrintPage;
      pd.QueryPageSettings += QueryPageSettings;
      pd.Print();

      LlCore.LlStgsysStorageClose(storageHandle);
      if (previewFile.Exists)
        previewFile.Delete();
    }
    return true;
  }

  static void OnPrintPage(object obj, PrintPageEventArgs ppea)
  {
    RECT r = new RECT();
    Single sLeft = -(ppea.PageSettings.PrintableArea.Left) * ppea.Graphics.DpiX / 100;
    Single sTop = -(ppea.PageSettings.PrintableArea.Top) * ppea.Graphics.DpiY / 100;
    r.left = Convert.ToInt32(sLeft);
    r.top = Convert.ToInt32(sTop);

    Single sWidth = (ppea.Graphics.VisibleClipBounds.Width + ppea.PageSettings.PrintableArea.Left) * ppea.Graphics.DpiX / 100;
    Single sHeight = (ppea.Graphics.VisibleClipBounds.Height + ppea.PageSettings.PrintableArea.Top) * ppea.Graphics.DpiY / 100;

    r.right = Convert.ToInt32(sWidth);
    r.bottom = Convert.ToInt32(sHeight);

    LlCore.LlStgsysDrawPage(storageHandle, ppea.Graphics.GetHdc(), IntPtr.Zero, false, r, page, false);

    ppea.HasMorePages = page < nmbrOfPages;
    page++;
  }

  static void QueryPageSettings(object sender, QueryPageSettingsEventArgs qpsea)
  {
    if (page < nmbrOfPages)
    {
      qpsea.PageSettings.PaperSource = defaultPrinterTray;
    }
    else
    {
      qpsea.PageSettings.PaperSource = specialPrinterTray;
    }
  }
}

Ich freue mich über Rückmeldungen falls man noch etwas optimieren kann.

Freundliche Grüsse
Philippe