PrintableArea in C # - error?

I have a problem with PageSettings.PrintableArea width and height values. The Width, Height, and Size properties claim to "get or set" values. In addition, the inflate () function claims to resize based on the values ​​passed in.

However, all these attempts to change the meaning did not work. Inflate () is ignored (no error, just passes as if it worked, but the values ​​remain unchanged.

Trying to set the height, width, or size gives a compiler error: "Unable to change the return value" System.Drawing.Printing.PageSettings.PrintableArea "because it is not a variable."

I get the feeling that this means that the “or given” part of the description is a lie.

Why I want to know this: (Someone always asks ...) I have a print application (C #, WinForm), which works pretty well for most things. I can configure printer settings and page settings to control what is displayed in the properties of the print dialog box. However, with the Microsoft Office Document Image Writer, these options are sometimes ignored, and the paper size is returned as 0, 0, even when it displays something else. All I really want is WYSIWYG, as far as the displayed values ​​go, so I change the paper size to what it should be, but the print area, if it is wrong, makes the resulting image uncomfortable. The resulting image is the size of the printable area instead of the value in the document. Just wondering if there is a reason for this or a way to make him not do it.

Thanks in advance.:)

UPDATE:

//ignored PrintDocument.DefaultPageSettings.PrintableArea.Inflate(XOffset, YOffset); //causes compiler error PrintDocument.DefaultPageSettings.PrintableArea.Size = new SizeF((float)DimensionsPaperSize.Width, (float)DimensionsPaperSize.Height); PrintDocument.DefaultPageSettings.PrintableArea.Height = DimensionsPaperSize.Height; PrintDocument.DefaultPageSettings.PrintableArea.Width = DimensionsPaperSize.Width; 

UPDATE 2:

For my (non-standard) printers that print correctly, when I change, PaperSize, PrintableArea, and PageBounds automatically change to fit them. When I change PaperSize to MDIW, only PageBounds change. I do not understand what it means.

CONCLUSION:

Nobugz did an excellent job explaining why PrintableArea cannot be installed (and usually should never be) and why its inflate () function is ignored, so I mark this as an answer.

Regarding the ongoing problem that prompted me to ask this question, I am still out of order. PaperSize and Graphics objects already have the correct values ​​in response to the ScaleTranform clause, so using these values ​​is unlikely to help. I suspect that most of all I could do along this route by resizing the image with the correct size to the garbage values ​​that I get for PrintableArea. I assume this is due to an error when explicit setting of PaperSize does not allow to change PrintableArea accordingly.

He was disappointed that I, it seems, is the only person who has encountered this problem. At least so far I have observed this behavior only for MODIW. For any reference, and therefore there is as much information as possible; I am running 32-bit Win7 being developed in VS2008. To reproduce the problem, follow these steps:

 PrintDialog PrintDlg = new PrintDialog(); PrintDocument PrintDoc = new PrintDocument(); PrintDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(DocumentPrintPage); PrintDlg.PrinterSettings.PrinterName = printerName; //MODIW PrintDoc = AlterPaperSize(PrintDoc); //A function that just changes the papersize PrintDlg.Document = PrintDoc; PrintDlg.PrinterSettings = PrintDoc.PrinterSettings; if (PrintDlg.ShowDialog() == DialogResult.OK) { if ((PrintDoc.DefaultPageSettings.PaperSize.Width == 0) && (PrintDoc.DefaultPageSettings.PaperSize.Height == 0)) { PrintDoc.DefaultPageSettings.PaperSize = DimensionsPaperSize; } PrintDoc.Print(); } 
+4
source share
3 answers

This is a fairly fundamental problem with .NET programming; every programmer has to deal with it at least once. The type of the PrintableArea property is RectangleF. This is a structure, a type of value. When you use a property, you return a copy of the value.

The compiler will notice that you are trying to change the member of the copy, for example, when you try to assign the Height property. But it becomes a nod when you use the Inflate () method. You inflate a copy, not the original value, and the compiler is not smart enough to notice.

The main problem is that the PrintableArea property has only getter, it does not have setter. This means that you cannot change it. What makes sense if you think about it, you cannot resize a sheet of paper or change the design of a printer. You probably want to use the Margins property.

+6
source

Ok, I know this is a bit dated, so the confinement, but I had the same problem, and figured out how to set the paper sizes correctly so that PrintableArea was right, as this was one of the few messages that came when I "searched "The problem, I thought I'd add how I got it to work here, so the next person stumbling over it gets an answer.

When you set PaperSize = New PaperSize (...) you create a custom size, even if you call it "A4" or "A5". Instead, you need to set the paper size to one of the standard sizes stored in PrinterSettings.PaperSizes.

Below is some C # .NET 3.5 code showing how I get A4 and A5 sizes as variables, which I can then use, as shown on the last line, when I set PaperSize, PrintableArea will now be correct.

 IEnumerable<PaperSize> paperSizes = settings.PaperSizes.Cast<PaperSize>(); PaperSize sizeA5 = paperSizes.First<PaperSize>(size => size.Kind == PaperKind.A5); PaperSize sizeA4 = paperSizes.First<PaperSize>(size => size.Kind == PaperKind.A4); settings.DefaultPageSettings.PaperSize = sizeA5; 
+1
source

Recently, I was able to figure it out myself. When assigning a new paper size: A) you must specify "Custom", B) There are restrictions on the paper size. I did not understand them, and they may be printer dependent. If you have the wrong size, the printable area becomes the default value of 8.5x11. Perhaps they should be a multiple of 10.

 .DefaultPageSettings.PaperSize = New PaperSize("Custom", 1100, 2200) 

does not work:

 .DefaultPageSettings.PaperSize = New PaperSize("Custom", 1093, 2290) 

Let me know if you find out more.

0
source

Source: https://habr.com/ru/post/1306292/


All Articles