I am trying to reproduce the below XPS XAML using the GDI+:
<FixedPage xmlns="http://schemas.microsoft.com/xps/2005/06" xmlns:x="http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key"
xml:lang="und" Width="816" Height="1056">
<Path Fill="#FFFFFFFF" Data="M0,0L816,0 816,1056 0,1056Z" />
<Canvas Clip="M0,0L816,0 816,1056 0,1056Z">
<Path Fill="#FFFFFFFF" Data="M0,0L816,0 816,1056 0,1056Z" />
<Path Fill="#FF008000" Data="M0,0L816,0 816,1056 0,1056Z" />
<Canvas RenderTransform="1,0,0,1,10,10">
<Path Fill="#FFFFFF00" Data="M0,0L796,0 796,120 0,120Z" />
<Canvas Clip="M0,0L796,0 796,120 0,120Z">
<Glyphs OriginX="0" OriginY="87.5866666666667" FontRenderingEmSize="96" FontUri="/Resources/1057478c-ad6c-46da-af29-3e00c349c111.ODTTF" UnicodeString="Rutger
Koperdraad" Fill="#FF000000" xml:lang="en-us" />
</Canvas>
</Canvas>
</Canvas>
</FixedPage>
I manage to get the rectangles and the vertical text position correctly printed to the XPS document writer using the code below:
PrivateSub OnPrintPageEx(senderAsObject, e
AsPrintPageEventArgs)
If m_intPageNumber < m_objPaginator.PageCountThen
Using objGraphics
As System.Drawing.Graphics = e.Graphics
' Correction factor between WPF and printer units
Dim f As
Single = 100 / 96
' Set the graphics quality
objGraphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.None
objGraphics.InterpolationMode = Drawing.Drawing2D.InterpolationMode.NearestNeighbor
' Draw the green background
Dim stcRect1 AsNew Drawing.RectangleF(0, 0, 816 * f, 1056 * f)
Call objGraphics.FillRectangle(Drawing.Brushes.DarkGreen, stcRect1)
' Draw the yellow rectangle
Dim stcRect2 AsNew Drawing.RectangleF(0, 0, 796 * f, 120 * f)
Call objGraphics.TranslateTransform(10 * f, 10 * f)
Call objGraphics.FillRectangle(Drawing.Brushes.Yellow, stcRect2)
' Draw the text
Dim objFont AsNew Drawing.Font("Times New Roman", 72)
Dim intEmHeight AsInteger = objFont.FontFamily.GetEmHeight(objFont.Style)
Dim intCellAscent
AsInteger = objFont.FontFamily.GetCellAscent(objFont.Style)
Dim sngOffset AsSingle = 87.58667F - 96.0F / intEmHeight * intCellAscent
Dim stcRect3 AsNew Drawing.RectangleF(0, sngOffset * f, 796 * f, 120 * f)
Call stcRect3.Inflate(100 / 96, 100 / 96)
Call objGraphics.DrawString("Rutger Koperdraad", objFont, Drawing.Brushes.Black, stcRect3)
EndUsing
m_intPageNumber += 1
e.HasMorePages = (m_intPageNumber < m_objPaginator.PageCount)
EndIf
EndSub
However, the horizontal text positioning is still different. The first letter starts too far to the right and the overall text is wider. How can I reproduce the XAML correctly in the GDI+?
To give some background: I have a WPF application that can print using WPF/XPS technology. Since this print path gives quality problems on many legacy printer driver (bad image resolution due to a bug in the XPS to GDI+ conversion), I am building an
alternative print engine based on the GDI+. So effectively, I need to convert my WPF graphics to the GDI+.
Rutger Koperdraad.