Using itext to get height and width? - vb.net

I have tried but couldn't get proper output.Code has been placed below.
Dim Height, Width As Double
Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
Dim doc As New iTextSharp.text.Document()
reader = New iTextSharp.text.pdf.PdfReader(FILENAME)
iTextSharp.text.pdf.PdfWriter.GetInstance(doc, New FileStream(FILENAME,FileMode.Open))
doc.Open()
Height = doc.PageSize.Height
Width = doc.PageSize.Width
Height = Math.Round(Height)
Width = Math.Round(Width)
doc.Close()

Related

How to introduce new line characters with iTextSharp

I have written following code to write text in PDF and i want to break the line after some text .
Dim document As Document
document = New Document(PageSize.A4, 5.0F, 20.0F, 20.0F, 20.0F)
Try
Dim writer As PdfWriter
writer = PdfWriter.GetInstance(document, New FileStream(filename, FileMode.Create))
document.Open()
Dim spacing As Integer
spacing = 0
Dim curY, lineHeight As Double
curY = document.Top
lineHeight = 0
Const maxPerLine As Integer = 3
For i As Integer = 0 To 5
Dim table As PdfPTable
table = New PdfPTable(4)
table.DefaultCell.Border = Rectangle.NO_BORDER
table.TotalWidth = 200.0F
table.LockedWidth = True
Dim cell As PdfPCell
cell = New PdfPCell(New Phrase("hello \n" + i + "\n" + "wass up ?" ))
cell.Colspan = 4
cell.HorizontalAlignment = 0
cell.Border = Rectangle.NO_BORDER
cell.Padding = 30.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 = (i + 1) Mod maxPerLine Then
curY = curY - lineHeight
spacing = 0
lineHeight = 0
End If
Next
Catch ex As Exception
Finally
document.Close()
End Try
i have tried with Paragraph but still i am not able to enter texts into new line.
I have read the doc of iTextSharp they have written if you want to break line then use "\n" but it is not working.
How can i break line after some text ?
The easiest way, is to use PdfPCell in composite mode (you're using text mode). Composite mode gets into play when you use AddElement:
Dim cell As PdfPCell
cell = New PdfPCell()
cell.AddElement(New Paragraph("line 1"))
cell.AddElement(New Paragraph("line 2"))
Note that you can't set the alignment at the level of the PdfPCell in this case. When using composite mode, you have to set the alignment at the level of the elements (in this case at the level of the Paragraph).

convert group of points to create an image vb.net

I have a group of points.
I know how to draw the polygon in the panel
but now I don't know how to make it an image.
tried googling it but I can't find the answer
how can I make it an image?
is it possible?
Dim points(5) As Point
points(0) = New Point(50, 10)
points(1) = New Point(90, 30)
points(2) = New Point(75, 70)
points(3) = New Point(25, 70)
points(4) = New Point(10, 30)
points(5) = New Point(50, 10)
Dim img As New Bitmap(100, 100)
Dim gfx As Graphics = Graphics.FromImage(img)
gfx.DrawLines(Pens.Black, points)
Dim strFilename As String = "C:\Junk\Junk.png"
img.Save(strFilename, System.Drawing.Imaging.ImageFormat.Png)
gfx.Dispose()
img.Dispose()
Process.Start(strFilename)
Assuming you want the image to come from the Panel on which you've been drawing, then you can use DrawToBitmap:
Dim Bmp As New Bitmap(Panel1.Width, Panel1.Height)
Dim Clip As New Rectangle(New Point(0, 0), Panel1.Size)
Panel1.DrawToBitmap(Bmp, Clip)
Which you can then, for instance, set in another panel:
Panel2.BackgroundImage = Bmp
Or save:
Bmp.Save("MyPanel.bmp")

ITextSharp Margin

I am working with itexsharp and I have problem because it does not assign the margin of the document
This is the code.
Dim pdfw As PdfWriter
Dim documentoPDF As New Document(iTextSharp.text.PageSize.A4.Rotate(), 20, 20, 20, 20) 'Creamos el objeto documento PDF
documentoPDF.SetMargins(0.0F, 0.0F, 10.0F, 10.0F)
pdfw = PdfWriter.GetInstance(documentoPDF, New FileStream(urlFija & "\" & "Manifiesto-" & Manifiesto & ".pdf", FileMode.Create))
documentoPDF.Open()
documentoPDF.NewPage()
Dim aTable = New iTextSharp.text.pdf.PdfPTable(3)
Dim Ancho0 As Single() = {0.75F, 1.45F, 0.75F}
'aTable.DefaultCell.Border = BorderStyle.None
Dim Imagen As iTextSharp.text.Image
Imagen = iTextSharp.text.Image.GetInstance(path & "Ministerio-3.jpg")
Imagen.ScalePercent(25)
Imagen.SetAbsolutePosition(25.0F, 25.0F)
Dim Img = New PdfPCell
Img.Border = Rectangle.NO_BORDER
Img.AddElement(Imagen)
aTable.AddCell(Img)
Dim C1 = New PdfPCell(New Paragraph("Formato", FontFactory.GetFont(FontFactory.TIMES, 13, iTextSharp.text.Font.BOLD)))
C1.HorizontalAlignment = 1
C1.VerticalAlignment = 2
C1.Border = Rectangle.NO_BORDER
aTable.AddCell(C1)
Dim C2 = New PdfPCell(New Paragraph("Prueba", FontFactory.GetFont(FontFactory.TIMES, 7, iTextSharp.text.Font.NORMAL)))
C2.HorizontalAlignment = 3
C2.Border = Rectangle.NO_BORDER
aTable.AddCell(C2)
aTable.SetWidths(Ancho0)
documentoPDF.Add(aTable)
documentoPDF.AddAuthor(Session("IDUsuario").ToString)
documentoPDF.AddTitle("Manifiesto")
documentoPDF.AddCreationDate()
documentoPDF.Close()
After this I added a table with the information, move me just the top margin
As documented, the width of a PdfPTable takes only 80% of the available width by default when you add it to a page (unless you define an absolute width instead of a relative width). It will be centered, so you will have a left and a right margin of 10% of the available width.
If you want the table to span 100%, you need to add this line:
aTable.WidthPercentage = 100;
Now the table will span the full width.

Display image in DataGridView not working properly

I'm trying to display image in DataGridView control, I used this code:
Dim img As Bitmap
img = New Bitmap("http://cache.images.globalsportsmedia.com/soccer/teams/75x75/1241.png")
' Create the DGV with an Image column
Dim dt As New DataGridView()
Me.Controls.Add(dt)
Dim imageCol As New DataGridViewImageColumn()
dt.Columns.Add(imageCol)
' Add a row and set its value to the image
dt.Rows.Add()
dt.Rows(0).Cells(0).Value = img
but in this line there is an error: img = New Bitmap("http://cache.images.globalsportsmedia.com/soccer/teams/75x75/1241.png")
the compiler show me this message: URI Format doesn't supported.
I am trying to show in the table of the images on the site, how can I overcome this?
try this :
Dim request as WebRequest = WebRequest.Create("http://cache.images.globalsportsmedia.com/soccer/teams/75x75/1241.png")
Dim response = request.GetResponse()
Dim responseStream as Stream = response.GetResponseStream()
Dim bitmap2 as Bitmap = new Bitmap(responseStream)
' Create the DGV with an Image column
// proceed with your code.

export arabic data to pdf using itext

I want to export gridview data to PDF, using iTextsharp. The gridview data contains Persian/Arabic data. It results in just a blank PDF file... what could be the reason?
Following is my code:
GridView1.DataSource = Session("dt")
GridView1.AllowPaging = False
GridView1.DataBind()
Dim bf As BaseFont = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") & "\fonts\tahoma.ttf", BaseFont.IDENTITY_H, True)
Dim font As New iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL)
Dim table As New iTextSharp.text.pdf.PdfPTable(GridView1.Columns.Count)
Dim widths As Integer() = New Integer(GridView1.Columns.Count - 1) {}
For x As Integer = 0 To GridView1.Columns.Count - 1
widths(x) = CInt(GridView1.Columns(x).ItemStyle.Width.Value)
Dim cellText As String = Server.HtmlDecode(GridView1.HeaderRow.Cells(x).Text)
Dim cell As New iTextSharp.text.pdf.PdfPCell(New Phrase(12, cellText, font))
cell.BackgroundColor = New BaseColor(GridView1.HeaderStyle.BackColor)
'cell.BackgroundColor = New BaseColor(System.Drawing.ColorTranslator.FromHtml("#008000"))
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL
table.AddCell(cell)
Next
table.SetWidths(widths)
For i As Integer = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then
For j As Integer = 0 To GridView1.Columns.Count - 1
Dim cellText As String = Server.HtmlDecode(GridView1.Rows(i).Cells(j).Text)
Dim cell As New iTextSharp.text.pdf.PdfPCell(New Phrase(12, cellText, font))
'Set Color of Alternating row
If i Mod 2 <> 0 Then
'cell.BackgroundColor = New BaseColor(System.Drawing.ColorTranslator.FromHtml("#C2D69B"))
cell.BackgroundColor = New BaseColor(GridView1.RowStyle.BackColor)
End If
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL
table.AddCell(cell)
Next
End If
Next
'Create the PDF Document
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
pdfDoc.Add(table)
pdfDoc.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Write(pdfDoc)
Response.[End]()
Your question is somewhat misleading because the problem you are experiencing is not caused by the fact that you are using Arabic. If you would write the document to a file, you would notice that the file is OK.
There is a serious error in these lines:
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
...
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Write(pdfDoc)
Response.[End]()
First you use the Response.OutputStream to write PDF bytes to it, then you use the Response object to set content headers. This is illegal syntax: you should always define your headers before sending any bytes to the output stream.
Furtermore: you should write PDF bytes to the outputstream, not a Document object. The following line is complete non sense:
Response.Write(pdfDoc)
For an example on how to send content bytes to the Response.OutputStream, read the answers to the following question: iTextSharp generated PDF: How to send the pdf to the client and add a prompt?
If you don't know how to create content byte, take a look at this code snippet:
using (MemoryStream myMemoryStream = new MemoryStream()) {
Document document = new Document();
PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream);
document.Open();
...
document.Close();
byte[] content = myMemoryStream.ToArray();
...
}
Never write an object such as document to an output stream, always use a byte[].