tags:

views:

244

answers:

2

We have a strange problem printing images and text. When we print FOR THE FIRST TIME on a machine, ONLY the background is shifted up and left, and is printed at the wrong location. The text is printed correctly. When we print the same again, everything is fine .... both bg and text .. ? Uninstalling and reinstalling the app or restarting the machine gives no change, everything keeps printing fine.

So, because only the bg image prints wrong, we suspect the problem lies with the HardMargin property. (Only that method uses HardMargin)

Why the problem only occurs on first execution is a mystery for us ... ?? Does anyone have any clue whatsoever ?

    private void DrawBackground(PrintPageEventArgs ev, string bgFilePath, float offsetX, float offsetY)
    {
        try
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(bgFilePath);
            float w = (float)Width;
            float h = (float)Height;
            float x = ev.PageSettings.HardMarginX + (offsetX);
            float y = ev.PageSettings.HardMarginY + (offsetY);
            ev.Graphics.DrawImage(img, x, y, w, h);
        }
        catch (Exception ex)
        {
            //Errors.CatchError
        }
    }



//Method for printing text ( on top of background )

RectangleF printRect = new RectangleF();
printRect.X = offsetX + (rtfPrintscale * (float)v.X);
printRect.Y = offsetY + (rtfPrintscale * (float)v.Y);
printRect.Width = rtfPrintscale * (float)v.Width;
printRect.Height = rtfPrintscale * (float)v.Height;

............................................................ In response to answer 1 :

Hi JDMX,

what you're saying makes sense. I was thinking in the same path.

I'll explain what exactly happened :

I installed the app on my laptop for testing. On this laptop, I recently installed a printer driver for printing on a Wireless printer.

When generating a print preview, i noticed the background image was placed incorrectly. It had negative x & y values. I thought this was a bug in code, so went back to the development machine and tested ... everything worked fine. So I tested it on more machines, ... still everything worked fine.

So I went back to the laptop, and made a printout. The print was the same as the print preview. Baffled, I again generated a print preview, and now everything was placed correct ???

So I thought this was an issue with appsettings, so I uninstalled an reistalled ... still everything was printing correct. So I rebooted, with no avail..

Our problem is that we even can't reproduce or debug the error ...

Is there maybe a printer driver just for testing out there or something we can work with ??

BTW : I'm setting margins in multiple ways:

ThePrintDocument.OriginAtMargins = true;
ThePrintDocument.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0);

...

ThePrintDocument.DefaultPageSettings.Margins.Left = 0;
ThePrintDocument.DefaultPageSettings.Margins.Top = 0;
ThePrintDocument.DefaultPageSettings.Margins.Bottom = 0;
ThePrintDocument.DefaultPageSettings.Margins.Right = 0;

... EDIT : The solution: ............................................

Well, here's what we think is going on:

1) Printersettings have a default margin of 100

2) You set unit to millimeter

3) you set OriginArMargins to true

4) Set margins to 0

What actually happened is that the "Origin" is set to the default margin of 100, which is interpreted as 100 mm. So your content is now printed with a 100 mm margin.

What you need to do is first set margins to 0, then set originatmargins to true.

HTH.

+1  A: 

First time on a machine or the first time against a newly installed print driver ( not reinstalled... settings could still be there )

If it is against the print driver, then what I would assume is that dotNet starts with 0 as the setting for the HardMarginX and HardMarginY. After the first print, the printer sends back to dotNet the settings it wants to use for those 2 values. At which point, everything prints correctly.

One thing not in the source is any manipulation of the PageSettings.Margins value. Are you setting those or using the defaults.

JDMX
+1  A: 

When you use OriginAtMargins = true

Then PrintController get HardMargins and use apropiate TranslateTransform over Ev.Graphics.

Note that Graphics.Unit = Display (at this Point)

When you change Graphics.Unit, Graphics.Transform is not transformed. You can do some coordinates transformation:

  Protected Sub AdjustPageUnit(ByVal g As Graphics, PageUnit As GraphicsUnit)
        If g.PageUnit <> PageUnit AndAlso g.PageUnit = GraphicsUnit.Display Then
           g.PageUnit = PageUnit
           If OriginAtMargins Then
              If PageUnit = GraphicsUnit.Pixel Then
                 g.TranslateTransform(g.Transform.OffsetX * ((g.DpiX / 100.0F) - 1.0F), _
                                      g.Transform.OffsetY * ((g.DpiY / 100.0F) - 1.0F))
              Else
                 Dim fx = FactorInchToUnit(PageUnit) / 100.0F - 1.0F
                 g.TranslateTransform(g.Transform.OffsetX * fx, g.Transform.OffsetY * fx)
              End If
           End If
        End If
  End Sub

  Public Function FactorInchToUnit(ByVal U As GraphicsUnit) As Single
       Select Case U
           'Case GraphicsUnit.World  'No sé que hacer...
           Case GraphicsUnit.Millimeter : Return 25.4F
           Case GraphicsUnit.Display : Return 100.0F
           Case GraphicsUnit.Document : Return 300.0F
           Case GraphicsUnit.Point : Return 72.0F
           Case GraphicsUnit.Inch : Return 1.0F
           Case Else : Throw New NotImplementedException("FactorInchToUnit Factor")
       End Select
  End Function

Note that all Printing structures use 1/100 inch unit (margins, PageBounds, MarginBounds )

x77