How to break pdf page in itextsharp pdf - vb.net

I have written the following code to generate pdf:
path = Server.MapPath("PDF-Files")
filename = path + "/mydata.pdf"
document = New iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 5.0F, 5.0F, 5.0F, 5.0F)
Dim bfTimes As BaseFont
Dim times As iTextSharp.text.Font
bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, False)
times = New iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.NORMAL)
Dim writer As PdfWriter
writer = PdfWriter.GetInstance(document, New FileStream(filename, FileMode.Create))
Dim ev As New itsEvents
writer.PageEvent = ev
If document.IsOpen Then
document.Close()
End If
document.Open()
Dim spacing As Integer
spacing = 0
Dim curY, lineHeight As Double
curY = document.Top
lineHeight = 0
Const maxPerLine As Integer = 3
For k As Integer = 0 To ds.Tables(0).Rows.Count - 1
Dim table As PdfPTable
table = New PdfPTable(3)
table.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER
table.TotalWidth = 200.0F
table.LockedWidth = True
Dim cell As PdfPCell
cell = New PdfPCell()
cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("id").ToString(), times))
cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("name").ToString() + " " + dsLabelTemp.Tables(0).Rows(k)("city").ToString() + "(" + dsLabelTemp.Tables(0).Rows(k)("post").ToString() + ")", times))
cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("userpersonal").ToString(), times))
cell.Colspan = 3
cell.HorizontalAlignment = 0
cell.Border = iTextSharp.text.Rectangle.NO_BORDER
cell.Padding = 20.0F
table.AddCell(cell)
table.WriteSelectedRows(0, -1, document.Left + spacing, curY, writer.DirectContent)
spacing = spacing + 200
lineHeight = Math.Max(lineHeight, table.TotalHeight)
If 0 = (k + 1) Mod maxPerLine Then
curY = curY - lineHeight
spacing = 0
lineHeight = 0
End If
Next
document.Close()
ShowPdf(filename)
When above code execute in perfect manner and gives the output but it is not showing the if page 1 completed .
In above image you can check that it is not showing full records.
I want to break the page if the page is full or increase the page hight.
How can i transfer data to second page if my 1st page is full ??

I got the solution.
Below is my new code:
Dim table As PdfPTable
table = New PdfPTable(4)
table.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER
table.TotalWidth = 400.0F
table.LockedWidth = True
For k As Integer = 0 To ds.Tables(0).Rows.Count - 1
Dim cell As PdfPCell
cell = New PdfPCell()
cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("id").ToString(), times))
cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("name").ToString() + " " + dsLabelTemp.Tables(0).Rows(k)("city").ToString() + "(" + dsLabelTemp.Tables(0).Rows(k)("post").ToString() + ")", times))
cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("userpersonal").ToString(), times))
cell.Colspan = 1
cell.HorizontalAlignment = 0
cell.Border = iTextSharp.text.Rectangle.NO_BORDER
cell.Padding = 20.0F
table.AddCell(cell)
Next
document.Add(table)
document.Close()
ShowPdf(filename)

Related

How to add a table at a fixed point after heading pages with VB and itextsharp

I have the following code which creates two header tables, and a table to follow.
I can't insert the table below immediately after the header.
They both start at the top of the page,
The second table should start immediately after the gray header grid.
Everything must be repeated on each page
Public Class Form1
Public Doc As Document
Public wri As PdfWriter
Public fntNormal12 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
Public TableGrid As PdfPTable '= New PdfPTable(5)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Doc = New Document(PageSize.A4, 20, 20, 20, 20)
Dim ev As _events = New _events() ' ora
wri = PdfWriter.GetInstance(Doc, New FileStream("simple2.pdf", FileMode.Create))
' evento per nuova pagina
wri.PageEvent = ev ' ora
Doc.Open()
TableGrid = New PdfPTable(5) ' diventata pubblica
TableGrid.WidthPercentage = 100 ' Table size is set to 100% of the page
TableGrid.HorizontalAlignment = 0 '0=left, 1=centre , 2 = right
TableGrid.SpacingAfter = 10 ' mette uno spazio di 10 , dopo
Dim TableGridWidths(4) As Single
TableGridWidths(0) = 150 ' se li metto tutti e 3 uguali è come se faccio il 33%
TableGridWidths(1) = 200
TableGridWidths(2) = 500
TableGridWidths(3) = 50
TableGridWidths(4) = 100
TableGrid.SetWidths(TableGridWidths) ' Set the column widths on table creation. Unlike HTML cells cannot be sized.
Try
For i = 0 To 100
'2° riga verde
Dim GrillCell1 As New PdfPCell(New Phrase("Pos." & i, fntNormal12))
'CellOne.Rotation = -90 ' inclina il testo
GrillCell1.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
TableGrid.AddCell(GrillCell1)
Dim GrillCell2 As New PdfPCell(New Phrase("PS" & i, fntNormal12))
'CellTwo.Border = 0
GrillCell2.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
TableGrid.AddCell(GrillCell2)
Dim GrillCell3 As New PdfPCell(New Phrase("DDDDDDDDDDDD" & i, fntNormal12))
GrillCell3.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
TableGrid.AddCell(GrillCell3)
Dim GrillCell4 As New PdfPCell(New Phrase(i, fntNormal12))
GrillCell4.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
TableGrid.AddCell(GrillCell4)
Dim GrillCell5 As New PdfPCell(New Phrase(i, fntNormal12))
GrillCell5.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
TableGrid.AddCell(GrillCell5)
Next i
Doc.Add(TableGrid)
Catch ex As Exception
MsgBox(ex.ToString)
Doc.Close()
End Try
Doc.Close()
Process.Start("simple2.pdf")
End Sub
End Class
Class _events
Inherits PdfPageEventHelper
Public Vvv = "VVV11111"
Public Ppp = "PPP 11111"
Public Vv = "VV 11111"
Public Ggg = "GGG 11111"
Public Pppp = "3333"
Public Ttt = "tttttttttt"
Public myTable As PdfPTable
Public myTableBC As PdfPTable
Public TableGridInt As PdfPTable '= New PdfPTable(5)
Public Logo = "tttttttttt"
'Public logo = iTextSharp.text.Image.GetInstance(Application.StartupPath & "\" & "MioLogo.jpg")
' settaggio intestazione
Public fntBOLD18 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 18, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
' altro settaggio verde
Public fntBOLD12 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
Public fntBOLD8 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
' altro settaggio verde
Public fntNormal18 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 18, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
Public fntNormal12 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
Public fntNormal8 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
Public Overrides Sub OnEndPage(ByVal writer As PdfWriter, ByVal document As Document)
myTable = New PdfPTable(4)
myTable.WidthPercentage = 100 ' Table size is set to 100% of the page
myTable.HorizontalAlignment = 0 '0=left, 1=centre , 2 = right
myTable.SpacingAfter = 10 ' mette uno spazio di 10 , dopo
Dim sglTblHdWidths(3) As Single
sglTblHdWidths(0) = 150 ' se li metto tutti e 3 uguali è come se faccio il 33%
sglTblHdWidths(1) = 580
sglTblHdWidths(2) = 140
sglTblHdWidths(3) = 130
myTable.SetWidths(sglTblHdWidths) ' Set the column widths on table creation. Unlike HTML cells cannot be sized.
myTable.AddCell(logo)
Dim CellTwoHdr As New PdfPCell(New Phrase("Mia Stampa", fntBOLD18)) ' font intestazione rosso
CellTwoHdr.HorizontalAlignment = 1
myTable.AddCell(CellTwoHdr)
Dim CellTreeHdr As New PdfPCell(New Phrase(("38 38 "), fntBOLD12)) ' font intestazione rosso
myTable.AddCell(CellTreeHdr)
Dim Cell4Hdr As New PdfPCell(New Phrase(("Mio canale: " & " 34"), fntNormal8)) ' font intestazione rosso
Cell4Hdr.HorizontalAlignment = 2
Cell4Hdr.VerticalAlignment = 2 ' non funziona
myTable.AddCell(Cell4Hdr)
'2° riga verde
Dim CellOne As New PdfPCell(New Phrase("PROVA", fntNormal12))
'CellOne.Rotation = -90 ' inclina il testo
myTable.AddCell(CellOne)
Dim CellTwo As New PdfPCell(New Phrase("", fntNormal8))
'CellTwo.Border = 0
myTable.AddCell(CellTwo)
Dim CellTree As New PdfPCell(New Phrase(Now, fntNormal8))
myTable.AddCell(CellTree)
Dim Cell4 As New PdfPCell(New Phrase("p di p ", fntNormal8))
Cell4.HorizontalAlignment = 2
myTable.AddCell(Cell4)
document.Add(myTable)
TableGridInt = New PdfPTable(5) ' diventata pubblica
TableGridInt.WidthPercentage = 100 ' Table size is set to 100% of the page
TableGridInt.HorizontalAlignment = 0 '0=left, 1=centre , 2 = right
TableGridInt.SpacingAfter = 10 ' mette uno spazio di 10 , dopo
Dim TableGridWidths(4) As Single
TableGridWidths(0) = 150 ' se li metto tutti e 3 uguali è come se faccio il 33%
TableGridWidths(1) = 200
TableGridWidths(2) = 500
TableGridWidths(3) = 50
TableGridWidths(4) = 100
TableGridInt.SetWidths(TableGridWidths) ' Set the column widths on table creation. Unlike HTML cells cannot be sized.
'2° riga verde
Dim IntCol1 As New PdfPCell(New Phrase("Point1", fntBOLD8))
'CellOne.Rotation = -90 ' inclina il testo
IntCol1.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
IntCol1.BackgroundColor = BaseColor.LIGHT_GRAY
TableGridInt.AddCell(IntCol1)
Dim IntCol2 As New PdfPCell(New Phrase("Pt2", fntBOLD8))
'CellTwo.Border = 0
IntCol2.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
IntCol2.BackgroundColor = BaseColor.LIGHT_GRAY
TableGridInt.AddCell(IntCol2)
Dim IntCol3 As New PdfPCell(New Phrase("Des", fntBOLD8))
IntCol3.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
IntCol3.BackgroundColor = BaseColor.LIGHT_GRAY
TableGridInt.AddCell(IntCol3)
Dim IntCol4 As New PdfPCell(New Phrase("Q.", fntBOLD8))
IntCol4.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
IntCol4.BackgroundColor = BaseColor.LIGHT_GRAY
TableGridInt.AddCell(IntCol4)
Dim IntCol5 As New PdfPCell(New Phrase("Point5", fntBOLD8))
IntCol5.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
IntCol5.BackgroundColor = BaseColor.LIGHT_GRAY
TableGridInt.AddCell(IntCol5)
document.Add(TableGridInt)
End Sub
I have already tried using the WriteSelectedRows method, but the result is that it never goes into the OnEndPage event and therefore at most I have about 28 or 30 table rows and not the others.
What could be the problem then?

vb Image grid detection

Im looking for a way to detect the center of grid squares when VB.net is feed an image
I want to start with an image of a grid with blue squares like this:
Grid
and i want the program to make an array of points in the center of each square like this (points aren't centered in picture)
Grid with Red points
i dont want to modify the image, i just want to get the points. I've tried getpixel for x and y but that just returns the same point
Dim search_color As Color = Color.FromArgb(255, 64, 128, 192)
Dim background_color As Color = Color.FromArgb(255, 240, 240, 240)
Dim grid_color As Color = Color.FromArgb(255, 144, 144, 144)
Dim pix As Color
Dim liney = 0, linex = 0
Dim loc, sloc, gloc As Point
For ch As Integer = 1 To 64
For y As Integer = liney To Bmp.Height - 1
For x As Integer = linex To Bmp.Width - 1
If Bmp.GetPixel(x, y) = search_color Then
sloc = New Point(x, y)
linex = x
liney = y
x = Bmp.Width - 1
y = Bmp.Height - 1
End If
Next
Next
Dim xloc = 0
For x As Integer = sloc.X To Bmp.Width - 1
If Bmp.GetPixel(x, sloc.Y) = grid_color Then
xloc = x - 1
End If
If Bmp.GetPixel(x, sloc.Y) = background_color Then
xloc = x - 1
End If
Next
For y As Integer = sloc.Y To Bmp.Height - 1
If Bmp.GetPixel(xloc, y) = grid_color Or Bmp.GetPixel(xloc, y) = background_color Then
gloc = New Point(xloc, y - 1)
End If
Next
loc = New Point((gloc.X + sloc.X) / 2, (gloc.Y + sloc.Y) / 2)
liney = gloc.Y
linex = gloc.X + 20
ListBox1.Items.Add(loc.ToString)
Next
Try this:
I added the following controls to a form to test the code:
pbImageToScan (PictureBox) - btnAnalyzeIMG (Button) - lbResult (ListBox)
Public Class Form1
Dim arrCenters() As Point
Dim bmpToAnalyze As Bitmap
Dim search_color As Color = Color.FromArgb(255, 64, 128, 192)
Dim background_color As Color = Color.FromArgb(255, 240, 240, 240)
Dim grid_color As Color = Color.FromArgb(255, 144, 144, 144)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
bmpToAnalyze = New Bitmap(Application.StartupPath & "\Image.bmp")
pbImageToScan.Image = Image.FromFile(Application.StartupPath & "\Image.bmp")
End Sub
Private Sub btnAnalyzeIMG_Click(sender As Object, e As EventArgs) Handles btnAnalyzeIMG.Click
FindCenters()
End Sub
Private Sub FindCenters()
bmpToAnalyze = New Bitmap(Application.StartupPath & "\Image.bmp")
pbImageToScan.Image = Image.FromFile(Application.StartupPath & "\Image.bmp")
'arrCenters is the array who will contains all centers data
ReDim arrCenters(0)
'arrCenters already starts with an element; this boolean is used to handle the first point insertion
Dim bFirstElementAddedToArray As Boolean
lbResult.Items.Clear()
Dim iIMGWidth As Integer = bmpToAnalyze.Width
Dim iIMGHeight As Integer = bmpToAnalyze.Height
'X, Y coordinates used for iterations
Dim iX As Integer = 0
Dim iY As Integer = 0
'Bitmap limits reached
Dim bExit As Boolean
'Used to skip a great part of Ys, if a match has been found along the current examinated line
Dim iDeltaYMax As Integer = 0
'Main cycle
Do While Not bExit
Dim colCurrentColor As Color = bmpToAnalyze.GetPixel(iX, iY)
If colCurrentColor = search_color Then
Dim iXStart As Integer = iX
Dim iYStart As Integer = iY
Dim iXEnd As Integer
Dim iYEnd As Integer
'Width of the Blue square
For iXEnd = iX + 1 To iIMGWidth - 1
Dim colColorSearchX As Color = bmpToAnalyze.GetPixel(iXEnd, iY)
If (colColorSearchX = background_color) Or (colColorSearchX = grid_color) Then
iXEnd -= 1
Exit For
End If
Next
'Height of the Blue square
For iYEnd = iY + 1 To iIMGHeight - 1
Dim colColorSearchY As Color = bmpToAnalyze.GetPixel(iXEnd, iYEnd)
If (colColorSearchY = background_color) Or (colColorSearchY = grid_color) Then
iYEnd -= 1
Exit For
End If
Next
iDeltaYMax = iYEnd - iYStart
'Blue square center coordinates
Dim pCenter As New Point((iXStart + iXEnd) / 2, (iYStart + iYEnd) / 2)
Dim iArrLenght As Integer = 0
If Not bFirstElementAddedToArray Then
bFirstElementAddedToArray = True
Else
iArrLenght = arrCenters.GetLength(0)
ReDim Preserve arrCenters(iArrLenght)
End If
arrCenters(iArrLenght) = pCenter
lbResult.Items.Add(pCenter.ToString)
iX = iXEnd
'Checks if the Width limit of the bitmap has been reached
If iX = (iIMGWidth - 1) Then
iX = 0
iY += iDeltaYMax + 1
iDeltaYMax = 0
Else
iX += 1
End If
Else
'Checks if the Width limit of the bitmap has been reached
If iX = (iIMGWidth - 1) Then
iX = 0
iY += iDeltaYMax + 1
iDeltaYMax = 0
Else
iX += 1
End If
End If
'Width and Height limit of the bitmap have been reached
If (iX = iIMGWidth - 1) And (iY = iIMGHeight - 1) Then
bExit = True
End If
Loop
'Draws a Red point on every center found
For Each P As Point In arrCenters
bmpToAnalyze.SetPixel(P.X, P.Y, Color.Red)
Next
pbImageToScan.Image = bmpToAnalyze
End Sub
End Class

Reduce latency when Embossing an Image in PictureBox

How would I go about reducing the latency of grabbing an image from Picturebox then emboss that image and return you picturebox as this is a camera handle and will need to capture/emboss at least 16 fps but i'm getting like 1 every 2.5 seconds
SwitchImageSave = 1
Button1.Enabled = False
StCam.StopTransfer(m_hCamera)
Dim nReval As Integer
Dim nLastErrorNo As Integer
Dim nBufferSize As Integer
Dim dwWidth As Integer
Dim dwHeight As Integer
Dim dwLinePitch As Integer
nReval = StCam.GetPreviewDataSize(m_hCamera, nBufferSize, dwWidth, dwHeight, dwLinePitch)
Dim dwPreviewPixelFormat As Integer
nReval = StCam.GetPreviewPixelFormat(m_hCamera, dwPreviewPixelFormat)
Dim pixelFormat As Imaging.PixelFormat = Imaging.PixelFormat.Format8bppIndexed
Select Case dwPreviewPixelFormat
Case StCam.STCAM_PIXEL_FORMAT_24_BGR
pixelFormat = Imaging.PixelFormat.Format24bppRgb
Case StCam.STCAM_PIXEL_FORMAT_32_BGR
pixelFormat = Imaging.PixelFormat.Format32bppRgb
End Select
Dim pbyteImageBuffer(nBufferSize) As Byte
Dim dwNumberOfByteTrans As Integer = 0
Dim pdwFrameNo(1) As Integer
Dim dwMilliseconds As Integer = 100
Dim gch As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(pbyteImageBuffer, System.Runtime.InteropServices.GCHandleType.Pinned)
Dim ptr As IntPtr = gch.AddrOfPinnedObject()
nReval = StCam.TakePreviewSnapShot(m_hCamera, ptr, nBufferSize, dwNumberOfByteTrans, pdwFrameNo, dwMilliseconds)
gch.Free()
Dim bitmap As Bitmap = New Bitmap(dwWidth, dwHeight, pixelFormat)
Select Case pixelFormat
Case Imaging.PixelFormat.Format8bppIndexed
Dim colorPalette As System.Drawing.Imaging.ColorPalette = bitmap.Palette
For pixelValue As Integer = 0 To 255
colorPalette.Entries(pixelValue) = Color.FromArgb(pixelValue, pixelValue, pixelValue)
Next
bitmap.Palette = colorPalette
End Select
Dim bitmapData As Imaging.BitmapData = bitmap.LockBits(New Rectangle(0, 0, dwWidth, dwHeight), Imaging.ImageLockMode.WriteOnly, pixelFormat)
Runtime.InteropServices.Marshal.Copy(pbyteImageBuffer, 0, bitmapData.Scan0(), nBufferSize)
bitmap.UnlockBits(bitmapData)
Dim bmap As Bitmap
bmap = New Bitmap(bitmap)
PictureBox2.Image = bmap
' PictureBox2.Image = bmap
Dim tempbmp As New Bitmap(bmap)
Dim i, j As Integer
Dim DispX As Integer = 1, DispY As Integer = 1
Dim red, green, blue As Integer
With tempbmp
For i = 0 To .Height - 2
For j = 0 To .Width - 2
Dim pixel1, pixel2 As System.Drawing.Color
pixel1 = .GetPixel(j, i)
pixel2 = .GetPixel(j + DispX, i + DispY)
red = Math.Min(Math.Abs(CInt(pixel1.R) - CInt(pixel2.R)) + 128, 255)
green = Math.Min(Math.Abs(CInt(pixel1.G) - CInt(pixel2.G)) + 128, 255)
blue = Math.Min(Math.Abs(CInt(pixel1.B) - CInt(pixel2.B)) + 128, 255)
bmap.SetPixel(j, i, Color.FromArgb(red, green, blue))
Next
Next
End With
PictureBox2.Image = bmap
PictureBox2.Refresh()
PictureBox2.BringToFront()
Dim bitmapData As Imaging.BitmapData = bitmap.LockBits(New Rectangle(0, 0, dwWidth, dwHeight), Imaging.ImageLockMode.WriteOnly, pixelFormat)
Runtime.InteropServices.Marshal.Copy(pbyteImageBuffer, 0, bitmapData.Scan0(), nBufferSize)
bitmap.UnlockBits(bitmapData)
PictureBox2.Image = bitmap
Dim temp As Bitmap = PictureBox2.Image
Dim raz As Integer = temp.Height / 4
Dim height As Integer = temp.Height
Dim width As Integer = temp.Width
Dim rect As New Rectangle(Point.Empty, temp.Size)
Dim bmpData As BitmapData = temp.LockBits(rect, ImageLockMode.[ReadOnly], temp.PixelFormat)
Dim bpp As Integer = If((temp.PixelFormat = PixelFormat.Format32bppArgb), 4, 3)
Dim size As Integer = bmpData.Stride * bmpData.Height
Dim data As Byte() = New Byte(size - 1) {}
'byte[] newdata = new byte[size];
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, data, 0, size)
'System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, newdata, 0, size);
Dim options = New ParallelOptions()
Dim maxCore As Integer = Environment.ProcessorCount - 1
options.MaxDegreeOfParallelism = If(maxCore > 0, maxCore, 1)
For y As Integer = 0 To height - 4
For x As Integer = 0 To width - 4
If True Then
Dim index As Integer = y * bmpData.Stride + x * bpp
'Blue
data(index) = CByte(Math.Min(Math.Abs(CInt(data(index)) - CInt(data(index + bpp + bmpData.Stride))) + 128, 255))
'Green
data(index + 1) = CByte(Math.Min(Math.Abs(CInt(data(index + 1)) - CInt(data(index + bpp + 1 + bmpData.Stride))) + 128, 255))
'Red
data(index + 2) = CByte(Math.Min(Math.Abs(CInt(data(index + 2)) - CInt(data(index + bpp + 2 + bmpData.Stride))) + 128, 255))
End If
Next
Next

Printing forms in vb.net with many listviews

is it possible to print a form in vb.net using different listviews? For example I have 10 listviews and I want it all to be printed is a single form.
This is my code for my print document (So far it works with 2 listviews, but when I tried printing the 3rd listview, it overlaps the 2nd listview)
'Texts
Dim time As String
time = String.Format("{0:T}", DateTime.Now)
Dim dateNow As String = DateString
' Dim totalcash As String = Convert.ToDecimal(p_cashTotal).ToString("N2")
Dim fontTitle As New Font("Arial", 15, FontStyle.Bold)
Dim font1 As New Font("Arial", 10, FontStyle.Bold)
Dim font2 As New Font("Arial", 9, FontStyle.Bold)
Dim font3 As New Font("Arial", 9, FontStyle.Regular)
e.Graphics.DrawString("Company name", fontTitle, Brushes.Black, 270, 20)
e.Graphics.DrawString("Report name", font1, Brushes.Black, 350, 40)
' e.Graphics.DrawImage(pbxLogo.Image, 160, 10)
' e.Graphics.DrawString("SALES REPORT - " + salesType, font2, Brushes.Black, 277, 55)
' e.Graphics.DrawString("Description: List of customer who wasn't able to return items", font3, Brushes.Black, 133, 80)
e.Graphics.DrawString("Date/Time Printed:", font2, Brushes.Black, 133, 80)
e.Graphics.DrawString(dateNow, font3, Brushes.Black, 250, 80)
e.Graphics.DrawString(time, font3, Brushes.Black, 320, 80)
e.Graphics.DrawString("Date/Time Printed:", font2, Brushes.Black, 133, 80)
' e.Graphics.DrawString("Total Earnings:", font2, Brushes.Black, 133, 95)
' e.Graphics.DrawString("Php " + totalcash, font3, Brushes.Black, 240, 95)
Dim CurrRow As Integer = 0
Dim Ratio As Single = 0
Dim c As ColumnHeader
Dim g As Graphics = e.Graphics
Dim l As Integer = 120 'stores current left
Dim iCount As Integer
Dim f As Font = LVLevel1.Font
Dim FontBold As New System.Drawing.Font("Arial", 10, FontStyle.Regular)
Dim b As Brush = Brushes.Black
Dim currentY As Integer = 0
Dim maxY As Integer = 0
Dim gap As Integer = 5
Dim lvsi As ListViewItem.ListViewSubItem
Dim colLefts(LVLevel1.Columns.Count) As Integer
Dim colWidths(LVLevel1.Columns.Count) As Integer
Dim idx As Integer = 0
Dim ii As Integer
Dim lr As RectangleF
'LVLevel2
Dim colLefts2(LVLevel2.Columns.Count) As Integer
Dim colWidths2(LVLevel2.Columns.Count) As Integer
'LVLevel4
Dim colLefts3(LVLevel3.Columns.Count) As Integer
Dim colWidths3(LVLevel3.Columns.Count) As Integer
e.HasMorePages = False
' g.DrawRectangle(Pens.Black, 115, 140, 430, 30)
Dim sfc As New StringFormat
sfc.LineAlignment = StringAlignment.Center
sfc.Alignment = StringAlignment.Center
'LVLevel1
'Headings
currentY = 150
For Each c In LVLevel1.Columns
maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
colLefts(idx) = l
colWidths(idx) = c.Width
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
If lr.Width > 0 Then g.DrawString(c.Text, FontBold, b, lr, sfc)
l += c.Width
idx += 1
Next
currentY += maxY + gap
'g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
currentY += gap
'Rows
iCount = LVLevel1.Items.Count - 1
For ii = CurrRow To iCount
If (currentY + maxY + maxY) > e.PageBounds.Height Then 'jump down another line to see if this line will fit
CurrRow = ii - 2
e.HasMorePages = True
currentY += maxY + gap
Exit For 'does next page
End If
l = 0
maxY = 0
idx = 0
For Each lvsi In LVLevel1.Items(ii).SubItems
maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths(idx)).Height)
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
If LVLevel1.Columns(idx).Text <> "Name" Then
If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr, sfc)
Else
If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
End If
idx += 1
Next
currentY += maxY + gap
Next
'LVLEvel2
currentY = 150
For Each c In LVLevel2.Columns
maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
colLefts2(idx) = l
colWidths2(idx) = c.Width
lr = New RectangleF(colLefts2(idx), currentY, colWidths2(idx), maxY)
If lr.Width > 0 Then g.DrawString(c.Text, FontBold, b, lr, sfc)
l += c.Width
idx += 1
Next
currentY += maxY + gap
'g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
currentY += gap
'Rows
iCount = LVLevel2.Items.Count - 1
For ii = CurrRow To iCount
If (currentY + maxY + maxY) > e.PageBounds.Height Then 'jump down another line to see if this line will fit
CurrRow = ii - 2
e.HasMorePages = True
currentY += maxY + gap
Exit For 'does next page
End If
l = 0
maxY = 0
idx = 0
For Each lvsi In LVLevel2.Items(ii).SubItems
maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths2(idx)).Height)
lr = New RectangleF(colLefts2(idx), currentY, colWidths(idx), maxY)
If LVLevel2.Columns(idx).Text <> "Name" Then
If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr, sfc)
Else
If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
End If
idx += 1
Next
currentY += maxY + gap
Next
this are the additional variables
Protected WithEvents pd As Printing.PrintDocument 'used by Print sub
Protected Ratio As Single = 0, CurrRow As Integer = 0
this are my codes for the button PrintPreview, which opens print preview dialog
pdocDlines.DefaultPageSettings.Landscape = True
ppreviewDlines.PrintPreviewControl.Zoom = 1
ppreviewDlines.ShowDialog()
I just got this codes from some forums, what I want to do is to position the data from different listviews properly. Thanks

listview printing

I have found some code using google for listview printing. I modified the code base on my needs. I have some problem if the list view more than one pages. It will not stop counting "Generating Previews" of my document. If I press the cancel it was display the data in multiple pages but the same content.
Any suggestion would greatly appreciated.
Thanks in advance
Here is the code
Public Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim pd As New PrintDocument
Dim CurrRow As Integer = 0
Dim Ratio As Single = 0
Dim c As ColumnHeader
Dim g As Graphics = e.Graphics
Dim l As Integer = 0 'stores current left
Dim iCount As Integer
Dim f As Font = lsvToPrint.Font
Dim FontBold As New System.Drawing.Font("Microsoft Sans Serif", 10, FontStyle.Bold)
Dim b As Brush = Brushes.Black
Dim currentY As Integer = 0
Dim maxY As Integer = 0
Dim gap As Integer = 5
Dim lvsi As ListViewItem.ListViewSubItem
Dim colLefts(lsvToPrint.Columns.Count) As Integer
Dim colWidths(lsvToPrint.Columns.Count) As Integer
Dim idx As Integer = 0
Dim ii As Integer
Dim lr As RectangleF
e.HasMorePages = False
'Page Settings
Dim PSize As Integer = lsvToPrint.Items.Count
Dim PHi As Double
With pd.DefaultPageSettings
Dim Ps As PaperSize
PHi = PSize * 20 + 350
Ps = New PaperSize("Cust", 800, PHi)
.Margins.Top = 15
.Margins.Bottom = 20
.PaperSize = Ps
End With
Dim sfc As New StringFormat
sfc.LineAlignment = StringAlignment.Center
sfc.Alignment = StringAlignment.Center
'Title
Dim headfont As Font
Dim X1 As Integer
Dim Y As Integer
headfont = New Font("Courier New", 16, FontStyle.Bold)
X1 = pd.DefaultPageSettings.Margins.Left
Y = pd.DefaultPageSettings.Margins.Top + 5
With pd.DefaultPageSettings
e.Graphics.DrawLine(Pens.Black, 0, Y + 70, e.PageBounds.Width, Y + 70)
End With
'Headings
currentY = 100
For Each c In lsvToPrint.Columns
maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
colLefts(idx) = l
colWidths(idx) = c.Width
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
If lr.Width > 0 Then g.DrawString(c.Text, FontBold, b, lr, sfc)
l += c.Width
idx += 1
Next
currentY += maxY + gap
g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
currentY += gap
'Rows
iCount = lsvToPrint.Items.Count - 1
For ii = CurrRow To iCount
If (currentY + maxY + maxY) > e.PageBounds.Height Then 'jump down another line to see if this line will fit
CurrRow = ii - 1
e.HasMorePages = True
currentY += maxY + gap
Exit For 'does next page
End If
l = 0
maxY = 0
idx = 0
For Each lvsi In lsvToPrint.Items(ii).SubItems
maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths(idx)).Height)
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
If lsvToPrint.Columns(idx).Text <> "Name" Then
If lr.Width > 0 Then g.DrawString(lvsi.Text , f, b, lr, sfc)
Else
If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
End If
idx += 1
Next
currentY += maxY + gap
Next
End Sub
Make sure that you compensate the height of the MaxY for the new page. On the first page it's fine, but then when it gets to the next page, CurrentY + MaxY might already be bigger than the height of the page.