Hello, I am doing a C# win. application, and using crystal report for printing bills on a custom paper size The width of the paper is unknown (the program has to get the paper size - if this is possible), the lenth of the paper is infinitive (its a roll paper).

What and how I do it, that the report it will be printed out?

The report is filled up with all the data needed, and the code already gets the printer name, ... so what now?

- How to get the paper width and print the report (the report was made that all goes onto such paper width)?
- And that the printer will stop puting the paper out the it when the report is printed?

This code I have now, and I didnt test it yet, so I am not sure if it will work:

string printerName = GetPrinterName();
    if (printerName != null)
    {
      cr.PrintOptions.PaperSource = PaperSource.Auto; 
      cr.PrintOptions.PaperSize = PaperSize.DefaultPaperSize;

     CrystalDecisions.Shared.PageMargins edges = new CrystalDecisions.Shared.PageMargins(1, 1, 1, 1);
     cr.PrintOptions.ApplyPageMargins(edges);

     crystalReportViewer1.ReportSource = cr;
     crystalReportViewer1.Refresh();

     cr.PrintOptions.PrinterName = printerName;
     cr.PrintToPrinter(1, false, 0, 0);
    }

Recommended Answers

All 4 Replies

If your printer can detect the paper size, then PrintOptions.PaperSize will have the value you are looking for. No matter what, though, your program will consider the paper to be of a specified length. Mostly this will determine how much it will attempt to print before it does a 'next page' command. If you can find one of the enumerated paper sizes that is the right width and length, I'd use that.

If you can find one of the enumerated paper sizes that is the right width and length, I'd use that.

The width is not a problem, but the lenght, as I said it varies. Depends how much items its on it. So in this case it hard (or almost impossible) to use enumerated paper size.

But lets say if I do use one, how would I do it?

Now I know the width of the paper. It`s 76mm (or 3 inches). But I DO NOT know the lenght. So how can I create a new custom paper if I dont know the lenght? As far as I know, the custom paper size consists of width and lenght, but there is one of them missing it is impossible to create it... or am I wrong?

I have created new new paper size in the windows (Print Server Properties > Create new form), with name: POPPaperSize (76mm x 150mm) - this has all know dimensions (width and height).

Then I did this code:

string printerName = GetPrinterName();
        if (printerName != null)
        {
          System.Drawing.Printing.PrintDocument doctoprint = new System.Drawing.Printing.PrintDocument();
          doctoprint.PrinterSettings.PrinterName = printerName;
          int rawKind = 0;
          for (int i = 0; i <= doctoprint.PrinterSettings.PaperSizes.Count - 1; i++)
          {
            if (doctoprint.PrinterSettings.PaperSizes[i].PaperName == "POSPaperSize")
            {
              rawKind = Convert.ToInt32(doctoprint.PrinterSettings.PaperSizes[i].GetType().GetField("kind",
                System.Reflection.BindingFlags.Instance |
                System.Reflection.BindingFlags.NonPublic).GetValue(doctoprint.PrinterSettings.PaperSizes[i]));
              break; // TODO: might not be correct. Was : Exit For
            }
          }
          cr.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)rawKind;
          CrystalDecisions.Shared.PageMargins pMargin = new CrystalDecisions.Shared.PageMargins(0, 0, 0, 0);
          cr.PrintOptions.ApplyPageMargins(pMargin);
          crystalReportViewer1.ReportSource = cr;
          cr.PrintToPrinter(1, false, 0, 0);
        }

What I would like to know is how to change the code in case if I do not know the height of the paper. There is difference in the height if put 1 or 100 items on the paper (like the bill in the store).

I heard that I have to use Graphics objects, but I have no clue how... so please if someone can help me out. I would really appreciate it.

Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.