Printer , PrintDocument, HardMarginsX and HardMarginsY

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
Bath
Posts: 22
Joined: 17 Feb 2009, 19:08

Printer , PrintDocument, HardMarginsX and HardMarginsY

Post by Bath » 02 Dec 2019, 20:27

Greetings to all, I want to print to the maximum possible area, allowing the hard edges of the printer.

I want to determine the minimum margins (left, right, top and bottom) when printing horizontally. I use PrintDocument and calculate in PrintPage event, OriginAtMargins is set to False, Landscape = True.

If the paper orientation vertically my printer returns HardMarginX, HardMarginY equals 16 (unit 1/100 ").

If the paper orientation is horizontal (Landscape = True) returns HardMarginX = 16 and HardMarginY = 11.

The graphics automatically rotate by -90 ° (counterclockwise), but should I then consider HardMarginX as the top margin and HardMarginY as the left margin or consider how they are provided by the printer?

For soft margins, the values ​​change depending on the paper orientation, but hard margins are hardware settings and I am not sure. Alternatively, thank you if someone who is already clear about playing with the printer and leads me in the right direction.

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: Printer , PrintDocument, HardMarginsX and HardMarginsY

Post by support » 06 Dec 2019, 20:55

Hello,

I would recommend to check a PageSettings.PrintableArea property value which reflects the physical limitations of your printing device. PrintableArea (also known as "hard margins") will vary from printer to printer, from manufacturer to manufacturer. Because these are hardware measurements, they do not rotate when you set the page to landscape/portrait. The physical limitations won't change on the printer regardless of software print settings, so you need to make sure you apply them on the correct axis depending on your software settings for the print document (orientation).

Code: Select all

private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
    ...
    RectangleF printableArea = e.PageSettings.PrintableArea;
}
Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Bath
Posts: 22
Joined: 17 Feb 2009, 19:08

Re: Printer , PrintDocument, HardMarginsX and HardMarginsY

Post by Bath » 09 Dec 2019, 19:12

Hello Mikhail,
thanks for your answer.

I tried to print to printable area by converting drawing units to hundredths of inch (1/100"), right and bottom edges of the image were still invisible, then I set the graphic units to pixels, get printable area in pixels using code below, draw edges using graphics and the edges are visible on the printed page as expected.

You're right PageSettings.PrintableArea is not rotated when I set the page to landscape/portrait,
so I get the area :

Code: Select all

    <DllImport("gdi32.dll")>
    Private Shared Function GetDeviceCaps(ByVal hDC As IntPtr, ByVal nIndex As Integer) As Integer
    End Function
C# and VB.Net Signatures , Enum DeviceCap :
https://www.pinvoke.net/default.aspx/gd ... devicecaps

Code: Select all

        Dim hDC As IntPtr = e.Graphics.GetHdc()

        With PixelBounds  'Rectangle
            .X = GetDeviceCaps(hDC, DeviceCap.PHYSICALOFFSETX)
            .Y = GetDeviceCaps(hDC, DeviceCap.PHYSICALOFFSETY)
            .Width = GetDeviceCaps(hDC, DeviceCap.HORZRES)
            .Height = GetDeviceCaps(hDC, DeviceCap.VERTRES)
        End With

        With Dpi 'Point
            .X = GetDeviceCaps(hDC, DeviceCap.LOGPIXELSX)
            .Y = GetDeviceCaps(hDC, DeviceCap.LOGPIXELSY)
        End With
        e.Graphics.ReleaseHdc(hDC)
        
        With InchBounds 'RectangleF
            .X = PixelBounds.X * 100.0 / Dpi.X
            .Y = PixelBounds.Y * 100.0 / Dpi.Y
            .Width = PixelBounds.Width * 100.0 / Dpi.X
            .Height = PixelBounds.Height * 100.0 / Dpi.Y
        End With     
It seems to be working for my printer and printable area is returned correctly depending on Ladscape is False or True.
I will be grateful if anyone can verify it
Vladimir

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: Printer , PrintDocument, HardMarginsX and HardMarginsY

Post by support » 09 Dec 2019, 20:25

Hello Vladimir,

You may draw the printable area rectangle for verification, using the code below.

Code: Select all

private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
    Graphics g = e.Graphics;

    RectangleF printableArea = e.PageSettings.PrintableArea;
    RectangleF realPrintableArea = new RectangleF(
        (e.PageSettings.Landscape ? printableArea.Y : printableArea.X),
        (e.PageSettings.Landscape ? printableArea.X : printableArea.Y),
        (e.PageSettings.Landscape ? printableArea.Height : printableArea.Width),
        (e.PageSettings.Landscape ? printableArea.Width : printableArea.Height)
        );

    // Draw the printable area rectangle in red
    Rectangle printedPrintableArea = Rectangle.Truncate(realPrintableArea);
    printedPrintableArea.Width--;
    printedPrintableArea.Height--;
    g.DrawRectangle(Pens.Red, printedPrintableArea);
}
Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Bath
Posts: 22
Joined: 17 Feb 2009, 19:08

Re: Printer , PrintDocument, HardMarginsX and HardMarginsY

Post by Bath » 09 Dec 2019, 20:39

Hello,
It looks similar I tryied, but without Rectangle.Truncate.
I test it tomorrow, thanks again
Vladimir

Bath
Posts: 22
Joined: 17 Feb 2009, 19:08

Re: Printer , PrintDocument, HardMarginsX and HardMarginsY

Post by Bath » 10 Dec 2019, 21:07

Hello Mikhail,
Graphics must also be moved when print to the printer :

Code: Select all

private void printDocument_BeginPrint(object sender, PrintEventArgs e)
{
    printAction = e.PrintAction;
    printDocument.OriginAtMargins = false;
}

private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
    Graphics g = e.Graphics;

    if (printAction != PrintAction.PrintToPreview)
        g.TranslateTransform(-e.PageSettings.HardMarginX, -e.PageSettings.HardMarginY);

    RectangleF printArea = WhoGetBestPrintableArea(e);

    g.DrawRectangle(Pens.Red, printArea.X, printArea.Y, printArea.Width - 1, printArea.Height - 1);
}
You can also use PrintDocument.PrintController.IsPreview property.

May be interesting : PageSettings.cs source code in C# .NET
http://www.dotnetframework.org/default. ... ettings@cs


Beware of high paper usage
and protect our forests
Vladimir

DanielLisa
Posts: 2
Joined: 25 Jan 2023, 14:24

Re: Printer , PrintDocument, HardMarginsX and HardMarginsY

Post by DanielLisa » 26 Jan 2023, 18:18

This class contains a method called epson sticker printer WebControl that can print any control like a GridView, DataGrid, Panel, TextBox etc. The class makes a call to window. print() that simulates the print button.

DanielLisa
Posts: 2
Joined: 25 Jan 2023, 14:24

Re: Printer , PrintDocument, HardMarginsX and HardMarginsY

Post by DanielLisa » 29 Jan 2023, 17:21

Make sure you selected the correct paper size settings in your printing program and printer software. Make sure you selected the correct margins for your paper size in your printing program. Make sure your paper is positioned correctly for feeding into the epson sticker printer.

Post Reply