Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override Custom Papersize in C#

Tags:

c#

printing

I'm working on a project in C#. I have a labelprinter which needs to print a document that I send. The printer prints, however, I'm not able to override the following values of the Custom Paper format (Papierformaat in Dutch) seen here: https://gyazo.com/e350ed1e355b45b8cae24196d2b5869b. If I make the new PaperSize(); its height smaller or equal to 300 it works, but if I try to make it bigger, say 500, it cuts it down at 300. Why does this happpen? It seems like I can't override the values from the link's picture (which is 300).

public void Printing()
{
    try
    {
        streamToPrint = new StreamReader(filePath);
        try
        {
            PrinterSettings settings = new PrinterSettings();

            printFont = new Font("Arial", 10);
            PrintDocument pd = new PrintDocument();

            PaperSize paperSize = new PaperSize("Test", 315, 300);
            paperSize.RawKind = (int)PaperKind.Custom;


            pd.DefaultPageSettings.PaperSize = paperSize;
            pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);

            pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
            pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);

            pd.PrintPage += (sender, args) => Console.Out.WriteLine("Printable Area for printer {0} = {1}", args.PageSettings.PrinterSettings.PrinterName, args.PageSettings.PrintableArea);
            Console.Out.WriteLine("My paper size: " + pd.DefaultPageSettings.PaperSize); 


            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            // Print the document.
            pd.Print();
        }
        finally
        {
            streamToPrint.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

The output in the console is the following:

My paper size: [PaperSize Test Kind=Custom Height=500 Width=315]
Printable Area for printer xxx = {X=0,Y=0,Width=400,Height=300}

EDIT

For the people wondering, I'm dealing with a Label Printer with a continuous roll. So technically I could print a document with infinite height and a set width of 80mm. But I can't seem to override the Custom 300 value from the dialog settings.

I also want to point out that there are 2 other programs which are actually able to go over the 300 value and extend the PrintableArea. Who can help?

EDIT 2

After Shakir Ahamed's comment I got a little further:

gyazo.com/3298e480b77c5ba837b071b2ec4f7b8d I get this, which is a lot more than I used to get with using your last solution. But when I print it, the page cuts off at 300 again like it used to, it always cuts off at the value given in the dialog box (the box with the 300 and 400 value)

I feel like it won't work with the basic printing options, because I think that the driver overrides the page values and just cuts them off without caring about the PaperSizes. I read something about a DEVMODE structure, what's all possible with that? Can I override printer driver settings in here and print infinitely long prints with the continuous roll?

EDIT 3 (Solved, 20 oct. 2016)

For anyone interested, Some other problems occured with my printer and it started to act weird (like not printing print jobs). After all I guess something went wrong with installing the drivers. I deleted the drivers and reïnstalled everything according to the driver CD and now my initially posted code just seems to work fine in the first place. Kind of a bummer since I wasted so much time coding with just a bad driver installation. Now I'm able to print over the 300 units and I'm able to print with a continuous roll for more than 25cm if I want to. Thanks to everyone who was thinking with me to solve this problem!

like image 782
Markinson Avatar asked Aug 29 '16 14:08

Markinson


3 Answers

Try like this instead of your settings,before set the custom setting assign the instance of PrinterSettings to instance of PrintDocument

PrinterSettings ps = new PrinterSettings();
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings = ps; 

printDoc.DefaultPageSettings.PaperSize = new PaperSize("Custom", 315, 300);

or try this way I hope this will work

PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.PaperSize = new PaperSize("Custom", 315, 300);
pd.DefaultPageSettings.PaperSize.RawKind = 119;
pd.PrinterSettings.DefaultPageSettings.PaperSize.RawKind = 119;
pd.DefaultPageSettings.Landscape = false;
like image 77
Shakir Ahamed Avatar answered Sep 30 '22 05:09

Shakir Ahamed


If you like to print on PDF with custom size then this piece of code will help you.

Add DLL File of

iTextSharp.

protected void btn_SaveAs_Click(object sender, EventArgs e)
    {
string FileName = "Image_" + System.DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") + ".pdf"; // Download File Name here.
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=" + FileName);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        pnlPrint.RenderControl(hw); // In which panal name that want to  convert in PDF
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(new RectangleReadOnly(1500, 1500), 5, 5, 5, 5); // Pge size Chgnge Using RectangleReadOnly(1500, 1500) You can put on size value.
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    }

where line

Document pdfDoc = new Document(new RectangleReadOnly(1500, 1500), 5, 5, 5, 5);

show height and width of Page size and padding on page.

like image 34
Gautam Kumar Sahu Avatar answered Sep 30 '22 04:09

Gautam Kumar Sahu


Just an idea: you're setting the RawKind-Property to PaperKind.Custom which is = 0. The documentation says:

A value equal to 48 or 49 or larger than 118 indicates a custom paper size

and RawKind = 0 isn't defined there.

Maybe the printer driver can't handle it properly and you may want to try setting RawKind = 119 or s.th. like this.

Regards

like image 42
Yosh Avatar answered Sep 30 '22 05:09

Yosh