export arabic data to pdf using itext - pdf

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[].

Related

ITextSharp exported PDF is not supported or damaged visual basic

I'm new to using ITextSharp and I'm trying to export a PDF as an attachment. The code below works fine
Dim pdfTemplate As String = "C:\Users\mrogers\Documents\ERCP_CA_Template2.pdf"
Dim newFile As String = "C:\Users\mrogers\Documents\ERCP_CA_Template3.pdf"
Dim pdfReader As New PdfReader(pdfTemplate)
Dim pdfStamper As New PdfStamper(pdfReader, New System.IO.FileStream(newFile, System.IO.FileMode.Create))
Dim pdfFormFields As AcroFields = pdfStamper.AcroFields
pdfFormFields.SetField("BUSINESS_SITE_ADDRESS_A5", "Demo")
pdfStamper.FormFlattening = True
pdfStamper.Close()
However, when I try to convert this to export an attachment, Adobe Acrobat Reader can't open the file because it's not a supported type or it's been damaged. My Code is below.
Dim ms As MemoryStream = New MemoryStream()
Dim pdfTemplate As String = "C:\Users\mrogers\Documents\ERCP_CA_Template2.pdf"
Dim pdfReader As New PdfReader(pdfTemplate)
Dim pdfStamper As New PdfStamper(pdfReader, ms)
Dim pdfFormFields As AcroFields = pdfStamper.AcroFields
pdfFormFields.SetField("BUSINESS_SITE_ADDRESS_A5", "Demo")
pdfStamper.FormFlattening = True
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache)
HttpContext.Current.Response.ContentType = "application/pdf"
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=testingPdf2.pdf")
HttpContext.Current.Response.Write(ms.ToArray())
HttpContext.Current.Response.BufferOutput = True
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.Close()
pdfStamper.Close()
I've been struggling to find any good examples or documentation on this. Any assistance on where I'm going wrong would be appreciated.
For anyone having a similar issue I figured it out.
Dim output As New MemoryStream()
Dim pdfTemplate As String = "C:\Users\mrogers\Documents\ERCP_CA_Template2.pdf"
Dim pdfReader As New PdfReader(pdfTemplate)
Dim pdfStamper As New PdfStamper(pdfReader, output)
Dim pdfFormFields As AcroFields = pdfStamper.AcroFields
pdfFormFields.SetField("BUSINESS_SITE_ADDRESS_A5", "Demo")
pdfStamper.FormFlattening = True
pdfStamper.Close()
Dim mergedBytes() As Byte = output.GetBuffer()
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=demo4.pdf")
HttpContext.Current.Response.AddHeader("Content-Length", mergedBytes.Length.ToString())
HttpContext.Current.Response.ContentType = "application/octet-stream"
HttpContext.Current.Response.BinaryWrite(mergedBytes.ToArray)
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.End()

Text over image in itextsharp

I need load a pdf, add text and save on a new file.
With this code look work but where i have some images the text in under the image, i can't see it.
What can i do ?
Dim oldFile As String = Application.StartupPath & "\old.pdf"
Dim newFile As String = Application.StartupPath & "\new.pdf"
Dim reader As New PdfReader(oldFile)
Dim size As Rectangle = reader.GetPageSizeWithRotation(1)
Dim document As New Document(size)
Dim wNnumberOfPages As Integer = reader.NumberOfPages
Dim fs As New FileStream(newFile, FileMode.Create, FileAccess.Write)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
document.Open()
Dim cb As PdfContentByte = writer.DirectContent
Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
Dim wPage As Integer
For wPage = 1 To wNnumberOfPages
Dim _text As String = "TEXT"
Dim Page As PdfImportedPage = writer.GetImportedPage(reader, wPage)
cb.BeginText()
cb.SetFontAndSize(bf, 10)
cb.ShowTextAligned(2, _text, 470, 760, 0)
cb.EndText()
cb.AddTemplate(Page, 0, 0)
document.NewPage()
Next
document.Close()
fs.Close()
writer.Close()
reader.Close()
You first draw the text and then draw the imported page:
Dim Page As PdfImportedPage = writer.GetImportedPage(reader, wPage)
cb.BeginText()
cb.SetFontAndSize(bf, 10)
cb.ShowTextAligned(2, _text, 470, 760, 0)
cb.EndText()
cb.AddTemplate(Page, 0, 0)
Thus, any content in the imported page at the location of your text will cover it.
If instead you first draw the imported page and then the text, your text will be above content from the imported page:
Dim Page As PdfImportedPage = writer.GetImportedPage(reader, wPage)
cb.AddTemplate(Page, 0, 0)
cb.BeginText()
cb.SetFontAndSize(bf, 10)
cb.ShowTextAligned(2, _text, 470, 760, 0)
cb.EndText()
That being said, if you want to merely stamp something onto pages of an existing PDF, you should use the PdfStamper instead of PdfWriter with GetImportedPage. The PdfStamper class explicitly is made for such tasks and copies everything from the source document very faithfully. Your approach, on the other hand, drops all interactive and metadata content from the original PDF.

Add Image And Text To Existing .pdf Using iText in VB.net

I've got the below code on a button click which work great. It adds an image to each existing .pdf and then combines several of the new .pdf's together and creates one .pdf. Again, this part of it works great.
The problem I'm having is now I want to keep the existing code but add some text to each page at the same point in the code where the image gets added. I've read a bunch of examples on how to add text to an existing .pdf but due to my inexperience in the area I cant figure out how to make any of the examples work with my existing code. Using VB.net.
I want to add a simple line of text "Example Of text" and position it on the page (300, 300).
Any help would greatly appreciated.
Dim tempFilename = IO.Path.GetTempFileName()
Dim tempFile As New IO.FileStream(tempFilename, IO.FileMode.Create)
' Set up iTextSharp document to hold merged PDF
Dim mergedDocument As New iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER)
Dim copier As New iTextSharp.text.pdf.PdfCopy(mergedDocument, tempFile)
mergedDocument.Open()
Dim pic1 As String = "C:\xxx\xxxx\xxx.png"
Using inputPdfStream As IO.Stream = New IO.FileStream(Server.MapPath(".") + "/xxx.pdf", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
Using inputImageStream As IO.Stream = New IO.FileStream(pic1, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
Using outputPdfStream As IO.Stream = New IO.FileStream(Server.MapPath(".") + "/xxx2.pdf", IO.FileMode.Create, IO.FileAccess.ReadWrite, IO.FileShare.None)
Dim reader1 = New iTextSharp.text.pdf.PdfReader(inputPdfStream)
Dim stamper = New iTextSharp.text.pdf.PdfStamper(reader1, outputPdfStream)
Dim pdfContentByte = stamper.GetOverContent(1)
Dim image__1 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(inputImageStream)
image__1.SetAbsolutePosition(527, 710)
image__1.ScaleAbsolute(60, 60)
pdfContentByte.AddImage(image__1)
stamper.Close()
reader1.Close()
outputPdfStream.Close()
inputImageStream.Close()
inputPdfStream.Close()
outputPdfStream.Dispose()
End Using
End Using
End Using
Dim reader As New iTextSharp.text.pdf.PdfReader(New iTextSharp.text.pdf.RandomAccessFileOrArray(Server.MapPath(".") + "/yyy/xxx3".pdf", True), Nothing)
For pageNum = 1 To reader.NumberOfPages
copier.AddPage(copier.GetImportedPage(reader, pageNum))
Next
PTime = PTime + 1
Loop
mergedDocument.Close()
tempFile.Dispose()
After a lot of trial and error I got it to work by adding the following code.
Dim bf As iTextSharp.text.pdf.BaseFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
pdfContentByte.SetColorFill(iTextSharp.text.BaseColor.DARK_GRAY)
pdfContentByte.SetFontAndSize(bf, 8)
pdfContentByte.BeginText()
Dim strX As String = "Here"
pdfContentByte.ShowTextAligned(1, strX, 500, 500, 0)
pdfContentByte.EndText()

ITextSharp include all pages from the input file

I used the following code by using itextsharp library to add text to a pdf.(got the code from link ITextSharp insert text to an existing pdf
Dim reader As New PdfReader(oldFile)
Dim size As iTextSharp.text.Rectangle = reader.GetPageSizeWithRotation(1)
Dim document As New iTextSharp.text.Document(size)
' open the writer
Dim fs As New FileStream(newFile, FileMode.Create, FileAccess.Write)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
document.Open()
' the pdf content
Dim cb As PdfContentByte = writer.DirectContent
' select the font properties
Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.ZAPFDINGBATS, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
'cb.SetColorFill(GrayColor.DARK_GRAY)
cb.SetFontAndSize(bf, 8)
cb.BeginText()
Dim Text As String = "l"
' put the alignment and coordinates here
cb.ShowTextAligned(2, Text, 84, 729, 0)
cb.EndText()
Dim bf1 As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
cb.SetFontAndSize(bf1, 8)
cb.BeginText()
Dim text1 As String = "Navaneeth A"
cb.ShowTextAligned(1, text1, 65, 690, 0)
cb.EndText()
' create the new page and add it to the pdf
Dim page As PdfImportedPage = writer.GetImportedPage(reader, 1)
cb.AddTemplate(page, 0, 0)
' close the streams and voilá the file should be changed :)
document.Close()
fs.Close()
writer.Close()
reader.Close()
Now the problem is source pdf has about 5 pages.But the output file generated by this code has only first page. So How can i include all pages of source file in output file?
Source pdf link is http://law.incometaxindia.gov.in/DITTaxmann/IncomeTaxRules/PDF/Ay-2012-2013/SAHAJ2012_14.pdf
Dim reader As New PdfReader(oldFile)
Using ms = New MemoryStream()
Dim stamper As New PdfStamper(reader, ms)
'Using stamper 'As New PdfStamper(reader, ms)
stamper.RotateContents = False
Dim canvas As PdfContentByte = stamper.GetOverContent(1)
ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, New Phrase("Hello people!"), 36, 540, 0)
'End Using
Dim result As Byte() = ms.ToArray()
File.WriteAllBytes(newFile, result)
System.Diagnostics.Process.Start(newFile)
End Using
I did the following changes,but its not working.result file was just a 1kb file.
You unfortunately found sample code that should not be used. To manipulate existing PDFs you should use a PdfStamper, not a PdfWriter.
Your code (even after correction to copy all pages) does not copy interactive features (forms, other annotations...). You should instead base your code on the Webified iTextSharp Example StampText.cs explained in chapter 6 of iText in Action — 2nd Edition:
PdfReader reader = new PdfReader(resource);
using (var ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, ms)) {
stamper.RotateContents = false;
PdfContentByte canvas = stamper.GetOverContent(1);
ColumnText.ShowTextAligned(
canvas,
Element.ALIGN_LEFT,
new Phrase("Hello people!"),
36, 540, 0
);
}
byte[] result = ms.ToArray();
}
You can control font and color, too, if you change that code like this:
[...]
Font FONT = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD, new GrayColor(0.75f));
PdfContentByte canvas = stamper.GetOverContent(1);
ColumnText.ShowTextAligned(
canvas,
Element.ALIGN_LEFT,
new Phrase("Hello people!", FONT),
36, 540, 0
);
[...]
PS Some details may vary if you for some reasons have to use some old iTextSharp version (other questions from you seem to imply that you use VB6...). Even then, though, you should switch to using PdfStamper.

itextsharp: how do i position a table?

I have created a simple table and i need to position. does anyone have experience with tables in itextsharp?
here's my code
Private Sub generate_PDF()
Directory.SetCurrentDirectory("C:\Users\alexluvsdanielle\Desktop\")
Console.WriteLine("Chapter 6 example 1: Adding a Wmf, Gif, Jpeg and Png-file using urls")
Dim document As Document = New Document
Try
Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream("Chap1002.pdf", FileMode.Create))
document.Open()
'Dim wmf As Image = Image.GetInstance("harbour.wmf")
'Dim gif As Image = Image.GetInstance("vonnegut.gif")
Dim jpeg As Image = Image.GetInstance("C:\Users\alexluvsdanielle\Desktop\test.jpg")
'Dim png As Image = Image.GetInstance("hitchcock.png")
'document.Add(wmf)
'document.Add(gif)
jpeg.ScalePercent(50)
'jpeg.Alignment = Image.TOP_BORDER
jpeg.SetAbsolutePosition(0, 562)
document.Add(jpeg)
'document.Add(png)
Dim cb As PdfContentByte = writer.DirectContent
cb.BeginText()
Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
cb.SetFontAndSize(bf, 12)
'Dim text As String = "Sample text for alignment"
'cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 250, 700, 0)
'cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right", 250, 650, 0)
'cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left", 250, 600, 0)
cb.SetTextMatrix(150, 652)
cb.ShowText(patient_name)
cb.SetTextMatrix(150, 637)
cb.ShowText(doc_name)
cb.SetFontAndSize(bf, 8)
cb.SetTextMatrix(150, 620)
cb.ShowText(lot__no)
cb.SetTextMatrix(150, 611)
cb.ShowText(patient_id)
Dim i As Integer
For i = 1 To 10
cb.SetTextMatrix(150, 600 - (i * 10))
cb.ShowText(DataGridView1.Item(3, i).Value)
Next
cb.EndText()
Dim aTable As Table = New Table(2, 2)
aTable.Offset = 10
aTable.Width = 100
aTable.AddCell("0.0")
aTable.AddCell("0.1")
aTable.AddCell("1.0")
aTable.AddCell("1.1")
document.Add(aTable)
Dim datatable As PdfPTable = New PdfPTable(12)
Dim page As Rectangle = document.PageSize
datatable.TotalWidth = 100
datatable.AddCell("hello")
datatable.WriteSelectedRows(0, -1, document.LeftMargin, document.BottomMargin, writer.DirectContent)
document.Add(datatable)
Catch de As DocumentException
Console.Error.WriteLine(de.Message)
MessageBox.Show(de.Message)
Catch ioe As IOException
Console.Error.WriteLine(ioe.Message)
MessageBox.Show(ioe.Message)
Catch e As Exception
Console.Error.WriteLine(e.Message)
MessageBox.Show(e.Message)
End Try
document.Close()
End Sub
the first table works but the second does not
You can do something like this:
PdfPTable foot = new PdfPTable(2);
foot.TotalWidth = page.Width - document.LeftMargin - document.RightMargin;
foot.WriteSelectedRows(0, -1, document.LeftMargin, document.BottomMargin,
writer.DirectContent);
Make sure your code is closing the document and initializing the PdfWriter.
Example of what I use (output path is a variable passed into the function in C#):
Document document = new Document();
var writer = PdfWriter.GetInstance(document, new FileStream(outputPath, FileMode.Create));
document.Open();
//write stuff here
document.Close();