
Sub crea_pdf
	pdf.Initialize
	pdf.StartPage(595, 842) 'A4 size
	Pdf_righe=0
	Pdf_NumPagina=1
	Pdf_MaxRighe=45
	pdf_testa
	pdf_corpo
	pdf_piede
	pdf.FinishPage
	Dim out As OutputStream = File.OpenOutput(shared, "documento.pdf", False)
	pdf.WriteToStream(out)
	out.Close
	pdf.Close
End Sub

Sub pdf_testa
	pdf.Canvas.DrawRect( rettangolo(1,0,594,120), Colors.black,False,0dip)	
	pdf.Canvas.DrawText(Main.FlgIntesta1,10,20, Typeface.DEFAULT_BOLD, 12 / GetDeviceLayoutValues.Scale , Colors.RED, "LEFT")
	pdf.Canvas.DrawText(Main.FlgIntesta2,10,32, Typeface.DEFAULT, 10 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText(Main.FlgIntesta3,10,44, Typeface.DEFAULT, 10 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText(Main.FlgIntesta4,10,56, Typeface.DEFAULT, 10 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText(Main.FlgIntesta5,10,68, Typeface.DEFAULT, 10 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")	
	pdf.Canvas.DrawText("Spett.le",300,20, Typeface.DEFAULT_BOLD, 8 / GetDeviceLayoutValues.Scale , Colors.BLUE, "LEFT")
	pdf.Canvas.DrawText(lbl_Cliente.text,300,32, Typeface.DEFAULT, 10 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText(lbl_indirizzo.text,300,44, Typeface.DEFAULT, 10 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText(lbl_citta.text ,300,56, Typeface.DEFAULT, 10 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("P. Iva : " & Main.dbCurDocumenti.GetString("pariva") & " Cod. Fis.: " & Main.dbCurDocumenti.GetString("codfis")  ,300,68, Typeface.DEFAULT, 10 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("documento :" ,10,100, Typeface.DEFAULT , 10 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText(EmiDes_TipoDocumento ,80,100, Typeface.DEFAULT_BOLD,10 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Numero Documento : " & Main.dbCurDocumenti.getint("numdoc") & "/ " & Main.dbCurDocumenti.GetString("alfadoc") & " del : " & Funzioni.DATAITALIANA(Main.dbCurDocumenti.GetString("datadoc")) ,300,100, Typeface.DEFAULT, 10 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Pagamento : " & spn_PagamentoDoc.SelectedItem  & "                                                             Cod. Agente : " & Main.CodiceAgente ,10,115, Typeface.DEFAULT, 10 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Pagina Numero : " & Pdf_NumPagina ,585,115, Typeface.DEFAULT, 10 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")
	pdf.Canvas.DrawRect(rettangolo(0,120,594,720), Colors.black,False,0dip)	
	pdf.Canvas.DrawText("Codice articolo"  ,12 ,130, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Descrizione    "  ,185 ,130, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Um"  ,350 ,130, Typeface.DEFAULT, 7 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Q.tà "  ,380 ,130, Typeface.DEFAULT,7 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Prezzo "  ,425 ,130, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Sc. 1 "  ,475 ,130, Typeface.DEFAULT, 6 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Sc. 2 "  ,495 ,130, Typeface.DEFAULT, 6/ GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("% Iva  "  ,517 ,130, Typeface.DEFAULT, 6 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Imponibile"  ,545 ,130, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")	
	pdf.Canvas.DrawRect( rettangolo(1,120,594,135), Colors.black,False,1)	
	pdf.Canvas.DrawLine(85,120,85,720,Colors.black,0dip)
	pdf.Canvas.DrawLine(348,120,348,720,Colors.black,0dip)
	pdf.Canvas.DrawLine(368,120,368,720,Colors.black,0dip)
	pdf.Canvas.DrawLine(413,120,413,720,Colors.black,0dip)
	pdf.Canvas.DrawLine(473,120,473,720,Colors.black,0dip)
	pdf.Canvas.DrawLine(493,120,493,720,Colors.black,0dip)
	pdf.Canvas.DrawLine(515,120,515,720,Colors.black,0dip)
	pdf.Canvas.DrawLine(535,120,535,720,Colors.black,0dip)
End Sub

Sub pdf_corpo		
	Private tmpqta,tmpprezzo As Double
	pos=145
	For i=0 To curCorpoDoc.RowCount-1
		Pdf_righe=Pdf_righe+1
		If Pdf_righe>Pdf_MaxRighe Then
			pdf_piede_intermedio
			pdf.StartPage(595, 842) 'A4 size
			Pdf_righe=0
			Pdf_NumPagina=Pdf_NumPagina+1
			pdf_testa
			pos=145
		End If
		curCorpoDoc.Position =i
		tmpqta= curCorpoDoc.GetDouble("qta")
		tmpprezzo=curCorpoDoc.getdouble("prezzo")
		Dim tmpTotale As Double =curCorpoDoc.getdouble("totimponibile")
		pdf.Canvas.DrawText(curCorpoDoc.getstring("codicearticolo") ,2 ,pos, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
		pdf.Canvas.DrawText(curCorpoDoc.getstring("descrizione") ,87 ,pos, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
		pdf.Canvas.DrawText(curCorpoDoc.getstring("um") ,352 ,pos, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
		pdf.Canvas.DrawText(sf.Pad( NumberFormat2(tmpqta,1,2,0,True).Replace(",","#").Replace(".",",").Replace("#",".")," ",9,False)  ,410 ,pos, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")
		pdf.Canvas.DrawText(sf.Pad( NumberFormat2(tmpprezzo,1,5,2,True)," ",15,False).Replace(",","#").Replace(".",",").Replace("#",".")  ,470 ,pos, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")
		If curCorpoDoc.getdouble("sconto1")>0 Then
			pdf.Canvas.DrawText(sf.Pad( NumberFormat2(curCorpoDoc.getdouble("sconto1"),1,5,0,False) ," ",7,False)  ,490 ,pos, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")
		End If
		If curCorpoDoc.getdouble("sconto2")>0 Then
			pdf.Canvas.DrawText(sf.Pad( NumberFormat2(curCorpoDoc.getdouble("sconto2"),1,5,0,False) ," ",7,False)  ,513 ,pos, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")
		End If
		pdf.Canvas.DrawText(sf.Pad(curCorpoDoc.getint("cod_iva")," ",2,False) ,532,pos, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")
		pdf.Canvas.DrawText(sf.Pad(NumberFormat2(tmpTotale,1,2,2,True).trim," ",18,False).Replace(",","#").Replace(".",",").Replace("#",".") ,590 ,pos, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")
		pos=pos+13
	Next	
End Sub

Sub pdf_piede	
	pdf.Canvas.DrawText("% Iva"  ,12 ,731, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Imponibile"  ,55 ,731, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Imposta"  ,125 ,731, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Totale"  ,205 ,731, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")	
	pdf.Canvas.DrawLine(1,735,594,735,Colors.black,0dip)
	pdf.Canvas.DrawLine(40,722,40,840,Colors.black,0dip)
	pdf.Canvas.DrawLine(110,722,110,840,Colors.black,0dip)
	pdf.Canvas.DrawLine(180,722,180,840,Colors.black,0dip)
	pdf.Canvas.DrawLine(260,722,260,840,Colors.black,0dip)
	pos=745	
	For ali = 0 To TotDocCodIva.Size-1
		pdf.Canvas.DrawText( sf.Pad(NumberFormat2(TotDocCodIva.get(ali),1,0,0,True)," ",5,False).Replace(",","#").Replace(".",",").Replace("#",".") ,35 ,pos, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")
		pdf.Canvas.DrawText( sf.Pad(NumberFormat2(TotDocImponib .get(ali),1,2,2,True)," ",15,False).Replace(",","#").Replace(".",",").Replace("#",".") ,105 ,pos, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")
		pdf.Canvas.DrawText( sf.Pad(NumberFormat2(TotDocImposta.get(ali),1,2,2,True)," ",17,False).Replace(",","#").Replace(".",",").Replace("#",".") ,175 ,pos, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")
		pdf.Canvas.DrawText( sf.Pad(NumberFormat2(TotDocTotale.get(ali),1,2,2,True)," ",17,False).Replace(",","#").Replace(".",",").Replace("#",".") ,255 ,pos, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")
		pos=pos+13
	Next
	pdf.Canvas.DrawLine(260,770,594,770,Colors.black,0dip)	
	pdf.Canvas.DrawText("Totale Imponibile",285,745, Typeface.DEFAULT , 7/ GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Totale Imposta",410,745, Typeface.DEFAULT, 7/ GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Totale Documento",515,745, Typeface.DEFAULT_BOLD, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")

	pdf.Canvas.DrawText(sf.Pad(NumberFormat2(TotaleImponibile,1,2,2,True)," ",10,False).Replace(",","#").Replace(".",",").Replace("#","."),345,765, Typeface.DEFAULT , 10 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")
	pdf.Canvas.DrawText(sf.Pad(NumberFormat2(TotaleImposta,1,2,2,True)," ",10,False).Replace(",","#").Replace(".",",").Replace("#","."),465,765, Typeface.DEFAULT , 10 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")
	pdf.Canvas.DrawText(sf.Pad(NumberFormat2(TotaleDocumento,1,2,2,True)," ",12,False).Replace(",","#").Replace(".",",").Replace("#","."),590,765, Typeface.DEFAULT_BOLD , 12 / GetDeviceLayoutValues.Scale , Colors.Black, "RIGHT")	
	pdf.Canvas.DrawLine(260,795,594,795,Colors.black,0dip)
	pdf.Canvas.DrawText("Totale a pagare",355,785, Typeface.DEFAULT, 7/ GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	TotaleSaldo   =TotaleDocumento-TotaleAcconto
	If Round2(TotaleAcconto,2)=Round2(TotaleDocumento,2) Then
		pdf.Canvas.DrawText("Incassato",265,785, Typeface.DEFAULT , 7/ GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
		pdf.Canvas.DrawText(sf.Pad(NumberFormat2(TotaleAcconto,1,2,2,True)," ",10,False).Replace(",","#").Replace(".",",").Replace("#","."),340,787, Typeface.DEFAULT , 10 / GetDeviceLayoutValues.Scale , Colors.Blue, "RIGHT")
	Else
		pdf.Canvas.DrawText("Acconto",265,785, Typeface.DEFAULT , 7/ GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
		pdf.Canvas.DrawText(sf.Pad(NumberFormat2(TotaleAcconto,1,2,2,True)," ",10,False).Replace(",","#").Replace(".",",").Replace("#","."),345,787, Typeface.DEFAULT , 10 / GetDeviceLayoutValues.Scale , Colors.Blue, "RIGHT")
		pdf.Canvas.DrawText(sf.Pad(NumberFormat2(TotaleSaldo,1,2,2,True)," ",10,False).Replace(",","#").Replace(".",",").Replace("#","."),465,787, Typeface.DEFAULT , 10 / GetDeviceLayoutValues.Scale , Colors.red, "RIGHT")
	End If
	pdf.Canvas.DrawRect(rettangolo(0,722,594,840), Colors.black,False,0dip)
End Sub

Sub pdf_piede_intermedio	
	pdf.Canvas.DrawText("% Iva"  ,12 ,731, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Imponibile"  ,55 ,731, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Imposta"  ,125 ,731, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawText("Totale"  ,205 ,731, Typeface.DEFAULT, 8 / GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")	
	pdf.Canvas.DrawLine(1,735,594,735,Colors.black,0dip)
	pdf.Canvas.DrawLine(40,722,40,840,Colors.black,0dip)
	pdf.Canvas.DrawLine(110,722,110,840,Colors.black,0dip)
	pdf.Canvas.DrawLine(180,722,180,840,Colors.black,0dip)
	pdf.Canvas.DrawLine(260,722,260,840,Colors.black,0dip)
	pdf.Canvas.DrawLine(260,770,594,770,Colors.black,0dip)
	pdf.Canvas.DrawLine(260,795,594,795,Colors.black,0dip)
	pdf.Canvas.DrawText("Segue",480,755, Typeface.DEFAULT, 15/ GetDeviceLayoutValues.Scale , Colors.Black, "LEFT")
	pdf.Canvas.DrawRect(rettangolo(0,722,594,840), Colors.black,False,0dip)
	pdf.FinishPage 
End Sub

Sub rettangolo(t,l,b,r) As Rect
	Dim rett As Rect
	rett.Initialize(t,l,b,r)
	Return rett
End Sub
