Derp Derp - 1 month ago 23
C# Question

How to override Custom Papersize in C#

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!

Answer

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;
Comments