I have read a number of threads that discuss this and none have come up with a solution for this problem. Most of them revolve around using a suitable font. I have tried every single one of them with no success. I know this string is UTF-8 and Vietnamese because if i paste it into Notepad++ as an ASCII string and then change encoding to UTF-8 it works.
The input string looks like this;
"Có sẵn dịch vụ thông dịch miễn phà khi bạn yêu cầu."
The output string should look like this;
"Có sẵn dịch vụ thông dịch miễn phí khi bạn yêu cầu."
My code just produces the first string.
Any help is greatly appreciated. I am tearing my hair out here
Here is my code;
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO
Imports System.Text
Imports System.Collections.Generic
Module TextToPdf
Dim pdfWrite As PdfWriter
Dim pdfDoc As Document
Dim pdfFont As Font
Sub Main()
pdfDoc = New Document(PageSize.LETTER)
pdfFont = New Font(BaseFont.CreateFont(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "vuArial.ttf"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 15)
pdfWrite = PdfWriter.GetInstance(pdfDoc, New FileStream("../tmp/vietnamese.pdf", FileMode.Create))
pdfDoc.Open()
pdfDoc.Add(New Paragraph("Có sẵn dịch vụ thông dịch miễn phà khi bạn yêu cầu.", pdfFont))
pdfDoc.Close()
End Sub
End Module
You could probably just save your source code as UTF-8 and paste in the "output" (UTF-8) string, but assuming the source code is ANSI encoding, maybe you can convert it to UTF-8 manually:
Dim ansi = Encoding.Default.GetBytes("Có sẵn dịch vụ thông dịch miễn phà khi bạn yêu cầu.")
Dim utf8 = Encoding.UTF8.GetString(ansi)
pdfDoc.Add(New Paragraph(utf8, pdfFont))
Related
I have search the web and have not found a solution to do this.
I have a VB.net form with a Dataset of costumers, and i want to take the contact information in the textbox.text and fill out a PDF form, and print it.
Itextsharp is mention every where to create a pdf, not to fill out one and print it, and i found a promising code, im not really familiar with vb.net at all, this is my first program.
This is the code i found that i think would work...
Imports System
Imports System.IO
Imports System.Xml
Imports iTextSharp
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.xml
Imports System.Security
Private Sub xmltopdf()
Dim pdfTemp As String = "C:\ExampleTemplate.pdf" ' ---> It's the original pdf form you want to fill
Dim newFile As String = "C:\NewFile.Pdf" ' ---> It will generate new pdf that you have filled from your program
' ------ READING -------
Dim pdfReader As New PdfReader(pdfTemp)
' ------ WRITING -------
' If you don’t specify version and append flag (last 2 params) in below line then you may receive “Extended Features” error when you open generated PDF
Dim pdfStamper As New PdfStamper(pdfReader, New FileStream(newFile, FileMode.Create), "\6c", True)
Dim pdfFormFields As AcroFields = pdfStamper.AcroFields
' ------ SET YOUR FORM FIELDS ------
pdfFormFields.SetField("Company", "Parth Dave & Co.")
pdfFormFields.SetField("SalesOrder", "1234456")
pdfFormFields.SetField("InstallAddress", "Lorimer Street")
pdfFormFields.SetField("Suburb", "Port Melbourne")
pdfFormFields.SetField("Post_Code", "3207")
pdfFormFields.SetField("ClientContact", "Parth")
pdfFormFields.SetField("ClientPhone", "0402020202")
pdfStamper.FormFlattening = False
' close the pdf
pdfStamper.Close()
' pdfReader.close() ---> DON"T EVER CLOSE READER IF YOU'RE GENERATING LOTS OF PDF FILES IN LOOP
End Sub
I get errors on the Imports and dont know what the import are..
I have added the itextsharp.dll so that should be ok..
If anyone could help me out or send me in the right direction it would be much appreciated.
Put the Imports outside of the class.
I'm working on a vb.net app to fill preexisting pdf forms and I've run in to a frustrating problem. The code below puts the values into the given fields on the pdf form, but in order to see those values in Adobe Reader, the fields themselves have to be selected. I can't share the pdf itself, but from opening it in Acrobat, it seems like security/protection isn't the issue, though I do get a permissions error when I set FormFlattening to True.
Is there a step in the code below which I'm missing?
Imports System
Imports System.IO
Imports System.Xml
Imports iTextSharp
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.xml
Imports iTextSharp.pdfa
Imports System.Security
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pdfTemp As String = "C:\ExampleTemplate.pdf"
Dim newFile As String = "C:\NewFile.Pdf"
Dim pdfReader As New PdfReader(pdfTemp)
Dim pdfStamper As New PdfStamper(pdfReader, New FileStream(newFile,_ FileMode.Create), "\6c", True)
Dim pdfFormFields As AcroFields = pdfStamper.AcroFields
pdfFormFields.SetField("Date", "03092014", "03092014")
pdfFormFields.SetField("Contract_No", "1234456", "1234456")
pdfFormFields.SetField("Buyer", "bar, foo", "bar, foo")
pdfFormFields.GenerateAppearances = True
pdfStamper.FormFlattening = True
pdfStamper.Close()
pdfReader.Close()
End Sub
End Class
So it's not 100% clear to me why this worked and my previous efforts didn't, but copying and pasting this code to initialize the PdfStamper
Dim pdfTemplate As String = "Path to fillable pdf"
Dim strFolder As String = "Path to destination Folder"
Dim newFile As String = strFolder & "Name of Completed Form"
Dim pdfReader As New PdfReader(pdfTemplate)
Dim pdfStamper As New PdfStamper(pdfReader, New FileStream(newFile, FileMode.Create))
Dim pdfFormFields As AcroFields = pdfStamper.AcroFields
`fields and values as in original question'
pdfStamper.FormFlattening = True
pdfStamper.Close()
from the tutorial here made the project work.
On a side note, it became clear to me that not all .pdf files with fillable fields are "forms", and that itextsharp requires that the file be a form. I realized this when, after applying the above code to two files successfully, the third failed, despite me knowing the names of the fields. To make it into a form, and thus recognizable to itextsharp, I opened it in acrobat and created a form. All the fields and their names were preserved so I saved it and it worked like a charm.
I am a beginner programmer and I am currently looking for a way to load texts from an ANSI encrypted file to a RichTextBox in my form. Also, I am looking for a way to save it back to the file with ANSI encryption.
This is what am I am currently trying:
Imports System
Imports System.IO
Imports System.Text
Dim strResult As String
Using SR As StreamReader = New StreamReader("startup_config.cfg", Encoding.Default)
strResult = SR.ReadToEnd
End Using
I believe you mean ANSI encoding instead of encrypted.
You can use Encoding.GetEncoding with one of the code pages from the list here instead of using Encoding.Default. Here's the code sample from the GetEncoding page I linked above:
Imports System
Imports System.Text
Imports Microsoft.VisualBasic
Namespace Convert_Example
Class MyConvertExampleClass
Shared Sub Main()
Dim unicodeString As String = "This string contains the unicode character Pi(" & ChrW(&H03A0) & ")"
' Create two different encodings.
Dim ascii As Encoding = Encoding.ASCII
Dim [unicode] As Encoding = Encoding.Unicode
' Convert the string into a byte[].
Dim unicodeBytes As Byte() = [unicode].GetBytes(unicodeString)
' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = Encoding.Convert([unicode], ascii, unicodeBytes)
' Convert the new byte[] into a char[] and then into a string.
' This is a slightly different approach to converting to illustrate
' the use of GetCharCount/GetChars.
Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)) As Char
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)
' Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString)
Console.WriteLine("Ascii converted string: {0}", asciiString)
End Sub
End Class
End Namespace
To see if the code page you want is available, you can use GetEncodings to get a list of them.
ANSI encoding may mean any encoding.
Encoding.Default will return your default Code Page in Windows (it depends on regional settings in Windows, where program runs).
If file is encoded with other Code Page you have to set your StreamReader encoding to that Code Page, like this:
Encoding.GetEncoding(1250);
1250 is Central and East European Latin code page.
Added later:
You asked about saving back in that ANSI format - just use StreamWriter with same encoding as StreamReader.
Few code page examples and more about Code Pages at Wikipedia: link
I don't know whether it is simple or not because i am new to programming.
my requirement is : In my vb.net winform application, the filenames of the files present in "D:\Project" willbe displayed in DataGridView1 control. Now I want to load these files one after another into memory stream buffer and add the headers("ID","Name","Class") to the content in the file. Then I want to save these files in "C:\" with "_de" as suufix to the filename i.e.,sample_de.csv.
Can anyone please help me? If you need more clarity i can post it in more clear way
Many Thanks for your help in advance.
Try adapting this example to your situation:
Imports System.Text
Imports System.IO
Module Module1
Sub Main()
' Read input
Dim inputBuffer As Byte() = File.ReadAllBytes(".\input.txt")
' Manipulate the input
Dim outputBuffer As Byte() = DoSomethingWithMyBuffer(inputBuffer)
' Add headers
' There are several ecodings to choose from, make sure you are using
' the appropriate encoder for your file.
Dim outputTextFromBuffer As String = Encoding.UTF8.GetString(outputBuffer)
Dim finalOutputBuilder As StringBuilder = New StringBuilder()
finalOutputBuilder.AppendLine("""ID"",""Name"",""Class""")
finalOutputBuilder.Append(outputTextFromBuffer)
' Write output
File.WriteAllText(".\output.txt", finalOutputBuilder.ToString(), Encoding.UTF8)
End Sub
Private Function DoSomethingWithMyBuffer(inputBuffer As Byte()) As Byte()
'' Do nothing because this is just an example
Return inputBuffer
End Function
End Module
I'm wondering how can I put a header into my PDF file, cause I've tried the tutorials from here:
http://itextsharp.sourceforge.net/tutorial/ch04.html
And it has not worked.
I've done this:
Dim head As New HeaderFooter(New Phrase("This is page: "), False)
head.Border = Rectangle.NO_BORDER
document.Header = head
But VS2008 says that HeaderFooter is not defined (line 1), and Footer it's not a member of "iTextSharp.text.document" (line 3).
I've already included the imports at the beginning of my code and iIdon't have any other problems with the iTextsharps (I mean that it is working apart of the header problem):
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.Data.SQLite
Imports System.IO
So please, can anyone explain to me how can i set a header for my pages?
Regards
The answer to this question depends on which version of the iTextSharp dll you are using.
If you are using a version lower than 5, this should work
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Module Module1
Sub Main()
Dim pdfWrite As PdfWriter
Dim pdfDoc As New Document()
Dim pdfMemoryStream As New IO.FileStream("tryme.pdf", IO.FileMode.Create)
pdfWrite = PdfWriter.GetInstance(pdfDoc, pdfMemoryStream)
Dim pdfHeader As New HeaderFooter(New Phrase("Im at the head: "), False)
pdfHeader.Border = Rectangle.NO_BORDER
pdfDoc.Header = pdfHeader
pdfDoc.Open()
pdfDoc.Add(New Paragraph("Hello World"))
pdfDoc.NewPage()
pdfDoc.Add(New Paragraph("Hello World Again"))
pdfDoc.Close()
End Sub
End Module
Update
For version 5+ of iTextSharp the HeaderFooter property has been removed from iTextSharp. To add Headers/Footers now you must use PageEvents. The following code demonstrates how to do this (very simply!)
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.IO
Module Module1
Sub Main()
Dim pdfDoc As New Document()
Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream("tryme2.pdf", FileMode.Create))
Dim ev As New itsEvents
pdfWrite.PageEvent = ev
pdfDoc.Open()
pdfDoc.Add(New Paragraph("Hello World"))
pdfDoc.NewPage()
pdfDoc.Add(New Paragraph("Hello World Again"))
pdfDoc.Close()
End Sub
End Module
Public Class itsEvents
Inherits PdfPageEventHelper
Public Overrides Sub OnStartPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document)
Dim ch As New Chunk("This is my Stack Overflow Header on page " & writer.PageNumber)
document.Add(ch)
End Sub
End Class