String.Format alignment - vb.net

I'm having trouble aligning my four columns:
My string format for the titles works properly. When I read the next line and put it into a list, the String.Format takes the video name and creates the columns adjusting to the video names length. How can I fix this?
Private Sub pdPrint_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles pdPrint.PrintPage
e.Graphics.DrawString("Movie Collection", New Font("Courier", 20, FontStyle.Bold), Brushes.Black, 300, 10)
e.Graphics.DrawString(String.Format("{0, -20} {1,-20} {2, -20} {3,-20}", "VIDEO NAME", "YEAR PRODUCED", "RUNNING TIME", "RATING"), New Font("Courier", 12, FontStyle.Bold), Brushes.Black, 10, 70)
Using reader As New StreamReader("testing.txt")
Dim intVertPosition As Integer = 90
While Not reader.EndOfStream
Dim videoName As String = reader.ReadLine()
Dim yearProduced As String = reader.ReadLine()
Dim runningTime As String = reader.ReadLine()
Dim rating As String = reader.ReadLine()
Dim extraline As String = reader.ReadLine()
e.Graphics.DrawString(String.Format("{0, -30} {1,30} {2, 30} {3,30}", videoName.ToString(), yearProduced.ToString(), runningTime.ToString(), rating.ToString()), New Font("Courier", 12, FontStyle.Regular), Brushes.Black, 10, intVertPosition)
intVertPosition += 14
End While
End Using
End Sub

e.Graphics.DrawString(..., New Font("Courier", 20, FontStyle.Bold), ...)
This is where the problem started. This kind of formatting can only work correctly if the width a space is the same as the width of a letter. Or in other words, it requires a fixed-pitch font. But you can clearly tell from the screen-shot that you got a proportionally pitched font. So, roughly, the more text in the first column, the more it pushes out the next column.
This happened because you picked "Courier". It is the name of a legacy device font, useful only in programs that were written 27 years ago. Graphics.DrawString() requires a scalable font outline, a True-Type font to be specific, and cannot use device fonts. So the font mapper kicks in to provide an alternative, unfortunately it isn't smart enough to recognize that you wanted a fixed-pitch font.
Fix the problem by using "Courier New" instead.

Related

VB Net Graphics DrawString construct Fail

I have NO idea what is wrong with my construct in the Second Graphics.DrawString line of code
My Question is Why is the construction of that line of code FAILING and the line above it does Not Fail
This is the FAILING line of code
e.Graphics.DrawString(strTo, 12, Brushes.Red, 45, 50)
Private Sub pdDoc_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles pdDoc.PrintPage
Dim fontSize As Integer = 30
Dim labelFont As Font = New Font("Times New Roman", fontSize, FontStyle.Bold)
Dim strTo As String = "Kitchen"
Dim lblArray(2) As String
lblArray(0) = "Kitchen"
e.Graphics.DrawString(lblArray(0), labelFont, Brushes.Black, 500, 950)
e.Graphics.DrawString(strTo, 12, Brushes.Red, 45, 50)
After reading the two answers and Here is my updated FIX for the error's in the line of code above
Dim labelFont As Font = New Font("Times New Roman", fontSize, FontStyle.Bold)
Dim strTo As Object = "Kitchen"
e.Graphics.DrawString(strTo.ToString, labelFont, Brushes.Red, 300, 300)
I've taken the liberty of aligning your commas between the working and not:
e.Graphics.DrawString(lblArray(0), labelFont, Brushes.Black, 500, 950)
e.Graphics.DrawString(strTo , 12 , Brushes.Red , 45 , 50 )
^^^^^^^^^
What kind of Font is 12 ?
The first DrawString is OK, but the second one is incorrect.
public void DrawString (string s, System.Drawing.Font font, System.Drawing.Brush brush, float x, float y, System.Drawing.StringFormat format);
#CaiusJard has already identified the error, so I am clarifying more.
Here it accepts a Font object, not Integer. By 12 I think you have confused with size. First you can declare a font object with:
Dim myFont As Font = New Font("FONT NAME",12)
Then you can use myFont instead of 12.

VB.NET Possible to add outside outline to text (stroke)

Is it possible to add an outline, one that continues to expand outwards from the text?
Ie: the outside stroke from Photoshop is what I am looking for
I have found a way to have the outline go inside the text, but it is not what I am looking for.
I've looked around, but I haven't been able to find anyone on google who wanted an outter outline.
Thanks
Current inner outline:
Dim grp As Graphics = e.Graphics
Dim gp As New Drawing2D.GraphicsPath
Dim useFont As Font = New Font("Impact", 60, FontStyle.Regular)
grp.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
grp.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
gp.AddString("3000", useFont.FontFamily, FontStyle.Regular, 60, New Point(0, 0), StringFormat.GenericTypographic)
useFont.Dispose()
Dim orangeBrush As New SolidBrush(Color.FromArgb(226, 149, 0))
grp.FillPath(orangeBrush, gp)
'This is the stroke below
Dim blackpen As New Pen(Color.Black, 2)
grp.DrawPath(blackpen, gp)
gp.Dispose()
If you reverse the order in which you drawpath and fillpath you can get a outside border for your text.

muti line text with first line underline in datagridviewTextBoxCell Vb.net

I'm able to add the mutiline text in datagridViewtextboxcell by using Environment.NewLine while inserting. but I would like to add underline for 1st line. so how could i achive this in vb.net
now I'm handling the paint method of DGV for showing date in 2 color for date format(as like in first column).
Please find the attached image,
in that second last column for email and cell number i want to draw underline.
Thanks.
I'm answering my own question.
Handle the DataGridView1_CellPainting and call this method for specific cell/column.firstly seperate the string and render it with TextRenderer class 2 times with new location.
I don't know is this correct way, but it works.
Private Sub CellText_Underline(color As Color, e As DataGridViewCellPaintingEventArgs)
Dim text As String = e.FormattedValue.ToString()
Dim nameParts As String() = text.Split(" ")
Dim topLeft As New Point(e.CellBounds.X, CInt(e.CellBounds.Y + (e.CellBounds.Height / 4)))
Dim arialBold As New Font("Arial", 11, FontStyle.Regular Xor FontStyle.Underline)
TextRenderer.DrawText(e.Graphics, nameParts(0), arialBold, topLeft, color)
Dim topLeft_1 As New Point(e.CellBounds.X, CInt(e.CellBounds.Y + (e.CellBounds.Height / 4) + 5))
Dim s As Size = TextRenderer.MeasureText(nameParts(0), arialBold)
Dim p As New Point(topLeft_1.X, topLeft_1.Y + 12)
Dim arialBold_1 As New Font("Arial", 11, FontStyle.Regular)
TextRenderer.DrawText(e.Graphics, nameParts(1), arialBold_1, p, SystemColors.WindowText)
End Sub
Thanks.

Coloring in shapes in Visual Basic.Net?

I have a set of shapes created in visual basic that need to be colored in:
Public Class Form1
Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
Dim formSurface As Graphics = Me.CreateGraphics 'creates surface
Dim world As New Pen(Color.Black, 3)
formSurface.DrawRectangle(world, 250, 50, 300, 300) 'world
Dim roof As New Pen(Color.Black, 3)
Dim roof1 As New Point(325, 200)
Dim roof2 As New Point(475, 200)
Dim roof3 As New Point(400, 100)
Dim roof4 As New Point(400, 100)
Dim curvePoints As Point() = {roof1, roof2, roof3, roof4}
formSurface.DrawPolygon(roof, curvePoints) 'triangle roof
Dim body As New Pen(Color.Black, 3)
formSurface.DrawRectangle(body, 325, 200, 150, 150) 'square body
Dim sun As New Pen(Color.Black, 3)
formSurface.DrawEllipse(sun, 450, 75, 50, 50) 'sun
Dim door As New Pen(Color.Black, 3)
formSurface.DrawRectangle(door, 387, 300, 25, 50) 'door
End Sub
End Class
Everything works perfectly; the shapes are all generated where I want them to be. (I'm trying to draw a house) However, how do I color them in? Also, if I do, will the colors overlap (I want that to happen)?
Should I use some function like image.fill? I know that isn't right but I'm looking for something like that.
Thanks!
You want to use the formSurface.FillPolygon, FillRectangle, and FillEllipse commands. You can still use the Draw commands to create an outline with a different color than the fill.
The "Fill" commands need Brushes instead of pens. (Pens draw lines, brushes fill space.) The easiest type of brush to use would be like "Brushes.AliceBlue".

Setting the background color of a contentbyte itextsharp

MVC3 VB.NET application using Itextsharp. I have a section of code that generates a pdf file everything looks great but I would like to alternate the line colors in that pdf file between 2 color so that the values are easy to follow for the person looking at it. Is there a way to set the background color of a whole line based on font size to a set color? A function I would be using this in is below:
For Each _reg_ In _reg
Dim _registrant As reg_info = _reg_
If y_line1 <= 30 Then
doc.NewPage()
_Page = _Page + 1
y_line1 = 670
End If
If y_line1 = 670 Then
cb.BeginText()
cb.SetFontAndSize(BF_Times, 6)
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _datePrinted + " " + _timePrinted, 500, 770, 0)
cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, "Page Number" + " " + _Page, 600, 770, 0)
cb.SetFontAndSize(BF_Times, 8)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _reportHead + " Overrides ", 304, 720, 0)
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "First Name", 20, 700, 0)
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Last Name", 80, 700, 0)
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Last Four", 160, 700, 0)
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Email Address", 300, 700, 0)
cb.EndText()
End If
cb.BeginText()
cb.SetFontAndSize(BF_Times, 8)
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _registrant.first_name, 20, y_line1, 0)
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _registrant.last_name, 80, y_line1, 0)
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _registrant.last_four_social, 160, y_line1, 0)
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _registrant.email, 300, y_line1, 0)
_total += 1
cb.EndText()
y_line1 = y_line1 - 15
Next
I thought about just setting the background color of the line by using the y_line1 and using a modulus to determine if the color should be grey or white. But I have found no code samples anywhere about how to set a whole line background color.. Any ideas????
There is no concept of "background color" in the PDF spec in relation to text. Anything that looks like a background color, even a table, is just text drawn on top of a rectangle (or some other shape).
To draw a rectangle you just call the Rectangle method on your PdfContentByte object. It takes a lower left x,y and a width and a height. The color is determined by a previous call to one of the color fills such as SetColorFill().
When working with the raw canvas its recommended that you also use SaveState() and RestoreState(). Since the fill commands are shared between objects but mean different things these can help avoid confusion. SaveState() sets a flag allowing you to undo all graphics state changes when you call RestoreState().
The code below is a full working VB.Net 2010 WinForms app targeting iTextSharp 5.1.2.0 that shows off the above. It creates a sample file on the desktop with a line of text repeated 7 times. Each line toggles back and forth between two background colors. Additionally it draws a stroke around the line of text to simulate a border.
Option Strict On
Option Explicit On
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
''//Test file that we'll create
Dim TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TestFile.pdf")
''//Test String that we'll repeat
Dim TestString = "It was the best of times..."
''//Create an array of our test string
Dim TestArray = {TestString, TestString, TestString, TestString, TestString, TestString, TestString}
''//Create our generic font
Dim BF_Times = BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED)
''//Standard PDF setup, change as needed for your stream type
Using FS As New FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None)
Using Doc As New Document(PageSize.LETTER)
Using writer = PdfWriter.GetInstance(Doc, FS)
Doc.Open()
''//Grab the raw content object
Dim cb = writer.DirectContent
''//Set our starter Y coordinate
Dim y = 670
''//Loop through our string collection
For I = 0 To (TestArray.Count - 1)
''//Store the current graphics state so that we can unwind it later
cb.SaveState()
''//Set the fill color based on eve/odd
cb.SetColorFill(If(I Mod 2 = 0, BaseColor.GREEN, BaseColor.BLUE))
''//Optional, set a border
cb.SetColorStroke(BaseColor.BLACK)
''//Draw a rectangle. NOTE: I'm subtracting 5 from the y to account for padding
cb.Rectangle(0, y - 5, Doc.PageSize.Width, 15)
''//Draw the rectangle with a border. NOTE: Use cb.Fill() to draw without the border
cb.FillStroke()
''//Unwind the graphics state
cb.RestoreState()
''//Flag to begin text
cb.BeginText()
''//Set the font
cb.SetFontAndSize(BF_Times, 6)
''//Write some text
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, TestArray(I), 0, y, 0)
''//Done writing text
cb.EndText()
''//Decrease the y accordingly
y -= 15
Next
Doc.Close()
End Using
End Using
End Using
Me.Close()
End Sub
End Class