Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8148261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:36:49+00:00 2026-06-06T14:36:49+00:00

I have a program which prints a multi-page document. The first page is pre-printed

  • 0

I have a program which prints a multi-page document. The first page is pre-printed paper, so it should print on the first side, and the rest of the pages should be duplexed.

My initial solution was to just print a blank sheet after the first page, but a lot (or maybe all) printers will use a different side of the pre-printed paper as the front (very bad), depending on whether it is set for duplexing or not.

So I’m stuck trying to convince the printer to change duplexing in the middle of the print job. I’ve been fighting with this without any luck, and using various code samples, this should theoretically work, but it doesn’t.

After each EndPage call, if the duplexing needs to change, it gets the device context from the printer (using reflection on a private field of an internal class, yuck), it uses Marshal to fill in values on the DEVMODE structure, and then calls ResetDC() with the device context and DEVMODE pointer.

I retrieve the DEVMODE structure from the pointer for verification purposes only, I want to make sure I’m setting the correct fields. DEVMODE gets populated correctly and sent to ResetDC() and PrinterSettings.SetHdevmode(), but when I re-retrieve PrinterSettings.GetHdevmode(), the changes I just made are gone. And the printer keeps the old duplex setting.

Edit: I found some issues with code I posted before, such as the fact that OnStartPage calls ResetDC. So I need to be modifying the DEVMODE structure held by StandardPrintController. But it’s still not working with these changes:

public class PrinterDuplexController : StandardPrintController
{
    public PrinterDuplexController()
    {
    }

    private static FieldInfo dcField = typeof(StandardPrintController)
        .GetField("dc",
        BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic);
    private static FieldInfo modeHandleField = typeof(StandardPrintController)
        .GetField("modeHandle",
        BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic);

    protected object dc
    {
        get
        {
            return dcField.GetValue(this);
        }
    }

    protected IntPtr Hdc
    {
        get
        {
            var dc = this.dc;
            return (IntPtr)(dc.GetType().GetProperty("Hdc").GetValue(dc, null));
        }
    }
    protected IntPtr modeHandle
    {
        get
        {
            object result = modeHandleField.GetValue(this);
            var field = result.GetType().GetField("handle", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic);
            return (IntPtr)field.GetValue(result);
        }
    }

    public override void OnEndPage(PrintDocument document, PrintPageEventArgs e)
    {
        base.OnEndPage(document, e);
        IntPtr pDEVMODE = GlobalLock(modeHandle);
        try
        {
            int[] flags = new int[1];
            Marshal.Copy(
                new IntPtr(40 + pDEVMODE.ToInt64()),
                flags,
                0,
                1);
            flags[0] |= (int)DM.Duplex;
            Marshal.Copy(
                flags,
                0,
                new IntPtr(40 + pDEVMODE.ToInt64()),
                1);



            Marshal.Copy(
                new short[] { (short)e.PageSettings.PrinterSettings.Duplex },
                0,
                new IntPtr(62 + pDEVMODE.ToInt64()),
                1);

            var debugDevMode = (DEVMODE)Marshal.PtrToStructure(pDEVMODE, typeof(DEVMODE));

            ResetDC(Hdc, pDEVMODE);
        }
        finally
        {
            GlobalUnlock(modeHandle);
        }
    }

    [DllImport("gdi32.dll")]
    //private static extern IntPtr ResetDC(IntPtr hdc, [In] ref DEVMODE lpInitData);
    private static extern int ResetDC(IntPtr hdc, IntPtr DevMode);

    [DllImport("gdi32.dll")]
    public static extern int StartPage(IntPtr hdc);

    [DllImport("gdi32.dll")]
    public static extern int EndPage(IntPtr hdc);

    [DllImport("kernel32.dll", ExactSpelling = true)]
    private static extern IntPtr GlobalFree(IntPtr handle);

    [DllImport("kernel32.dll", ExactSpelling = true)]
    private static extern IntPtr GlobalLock(IntPtr handle);

    [DllImport("kernel32.dll", ExactSpelling = true)]
    private static extern IntPtr GlobalUnlock(IntPtr handle);

    [Flags()]
    internal enum DM : int
    {
        Orientation = 0x1,
        PaperSize = 0x2,
        PaperLength = 0x4,
        PaperWidth = 0x8,
        Scale = 0x10,
        Position = 0x20,
        NUP = 0x40,
        DisplayOrientation = 0x80,
        Copies = 0x100,
        DefaultSource = 0x200,
        PrintQuality = 0x400,
        Color = 0x800,
        Duplex = 0x1000,
        YResolution = 0x2000,
        TTOption = 0x4000,
        Collate = 0x8000,
        FormName = 0x10000,
        LogPixels = 0x20000,
        BitsPerPixel = 0x40000,
        PelsWidth = 0x80000,
        PelsHeight = 0x100000,
        DisplayFlags = 0x200000,
        DisplayFrequency = 0x400000,
        ICMMethod = 0x800000,
        ICMIntent = 0x1000000,
        MediaType = 0x2000000,
        DitherType = 0x4000000,
        PanningWidth = 0x8000000,
        PanningHeight = 0x10000000,
        DisplayFixedOutput = 0x20000000
    }

    internal struct POINTL
    {
        public int x;
        public int y;
    }


    [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
    internal struct DEVMODE
    {
        public const int CCHDEVICENAME = 32;
        public const int CCHFORMNAME = 32;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
        [FieldOffset(0)]
        public string dmDeviceName;
        [FieldOffset(32)]
        public Int16 dmSpecVersion;
        [FieldOffset(34)]
        public Int16 dmDriverVersion;
        [FieldOffset(36)]
        public Int16 dmSize;
        [FieldOffset(38)]
        public Int16 dmDriverExtra;
        [FieldOffset(40)]
        public DM dmFields;

        [FieldOffset(44)]
        Int16 dmOrientation;
        [FieldOffset(46)]
        Int16 dmPaperSize;
        [FieldOffset(48)]
        Int16 dmPaperLength;
        [FieldOffset(50)]
        Int16 dmPaperWidth;
        [FieldOffset(52)]
        Int16 dmScale;
        [FieldOffset(54)]
        Int16 dmCopies;
        [FieldOffset(56)]
        Int16 dmDefaultSource;
        [FieldOffset(58)]
        Int16 dmPrintQuality;

        [FieldOffset(44)]
        public POINTL dmPosition;
        [FieldOffset(52)]
        public Int32 dmDisplayOrientation;
        [FieldOffset(56)]
        public Int32 dmDisplayFixedOutput;

        [FieldOffset(60)]
        public short dmColor;
        [FieldOffset(62)]
        public short dmDuplex;
        [FieldOffset(64)]
        public short dmYResolution;
        [FieldOffset(66)]
        public short dmTTOption;
        [FieldOffset(68)]
        public short dmCollate;
        [FieldOffset(72)]
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
        public string dmFormName;
        [FieldOffset(102)]
        public Int16 dmLogPixels;
        [FieldOffset(104)]
        public Int32 dmBitsPerPel;
        [FieldOffset(108)]
        public Int32 dmPelsWidth;
        [FieldOffset(112)]
        public Int32 dmPelsHeight;
        [FieldOffset(116)]
        public Int32 dmDisplayFlags;
        [FieldOffset(116)]
        public Int32 dmNup;
        [FieldOffset(120)]
        public Int32 dmDisplayFrequency;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:36:51+00:00Added an answer on June 6, 2026 at 2:36 pm

    Try making two print jobs, each with the different duplex settings. Fiddle with priorities of the jobs to get them in order instead of hacking around internal fields.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have program which prints all char from char_min to char_max here is code
I have written a multithreaded program which does some thinking and prints out some
I had a multi-thread program which execute from dos prompt, there are some prints
I have this program, which prints a sine wave but it's too fast, how
I have a program which prints (by printf) to the stdout some data and
I have a Python CGI program which prints datetime values. The timestamps are all
I have a Java program which prints to a HTML file. Another method reads
I have a program which reads contents from a file and prints it on
let us have a situation in which the following program prints some 10 lines
I have a program which I would like to run every X min the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.