I am using the code blow to draw text on a image which i will be adding to PDF which is working fine when i am adding English text. I would like to know how i can do this and add Arabic text. when i step through the code i can clearly see that the sting 'txtModule' is holding the text in Arabic. currenlt it is changeing the text to ????
Dim page As PdfPage = document.AddPage
page.Orientation = PageOrientation.Landscape
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
Dim XImage As XImage = XImage.FromFile("C:\Projects\CISIPR\currentPr\images\Certificate\prCertificate.jpg")
gfx.DrawImage(XImage, 20, 20, 800, 564)
Dim fontModule As New XFont("arial", 20, XFontStyle.Bold)
' Draw the Module text box
gfx.DrawString(txtModule, fontModule, New PdfSharp.Drawing.XSolidBrush(PdfSharp.Drawing.XColor.FromArgb(103, 154, 165)), _
New XRect(0, 10, page.Width.Point, page.Height.Point), XStringFormats.Center)
this code here will work in converting the font so;
Dim options = New XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always)
Dim fontModule As New XFont("arial", 20, XFontStyle.Bold, options)
You should get more than "????", but you have to enable Unicode. See this sample:
http://pdfsharp.net/wiki/Unicode-sample.ashx
But there is a show stopper: PDFsharp does not (yet) support LTR and Arabic glyphs.
I don't know what this means. Maybe it is enough to reverse the string and select initial, middle, and final glyphs in your code, maybe you cannot get correct Arabic at all.
Related
I'm trying to convert some itextsharp code to use itext7 which stamps text on each page of a pdf at rotate 90 degrees. Unfortunately all the examples I can find are in c# and while I can use an online translator I'm having difficulties with this one.
The below code stamps my text on at the specified coords on each page of a given pdf:
Shared Sub itext7_stamp_text_on_pdf(mypdfname As String, myfoldername As String)
Dim src As String = myfoldername & "\" & mypdfname
Dim dest As String = myfoldername & "\Stamped " & mypdfname
Dim pdfDoc As PdfDocument = New PdfDocument(New PdfReader(src), New PdfWriter(dest))
Dim document As Document = New Document(pdfDoc)
Dim canvas As PdfCanvas
Dim n As Integer = pdfDoc.GetNumberOfPages()
For i = 1 To n
Dim page As PdfPage = pdfDoc.GetPage(i)
canvas = New PdfCanvas(page)
With canvas
.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 12)
.BeginText()
.MoveText(100, 100)
.ShowText("SAMPLE TEXT 100,100")
.EndText()
End With
Next
pdfDoc.Close()
End Sub
... but I can't see a way of rotating it to 90 degrees.
There's an example here if you use a paragraph:
https://kb.itextpdf.com/home/it7kb/examples/itext-7-building-blocks-chapter-2-rootelement-examples#iText7BuildingBlocksChapter2:RootElementexamples-c02e14_showtextaligned
... but I can't seem to translate this to vb.net. I can specify where the errors I get are, but I thought I'd be better asking this general question first in case there's a way to do this without using a paragraph.
Can anyone help please?
Thanks!
Well, after some more digging this code seems to work OK on the rotation part:
Dim pdf As New PdfDocument(New PdfReader(inpdf), New PdfWriter(outpdf))
Dim document As New Document(pdf)
document.ShowTextAligned("This is some test text", 400, 750, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0.5F * CSng(Math.PI))
document.Close()
End Sub
.... but it gets hidden behind existing content, so I need a way to make sure it's set to over content.
I have added an image to my iTextSharp PDF document like this:
Public Sub CreatePDFFromBitmap(ByVal uPath As String, ByVal uBitmap As Bitmap)
Dim nFs As System.IO.FileStream = New FileStream(uPath, FileMode.Create)
Dim nDocument As iTextSharp.text.Document
Dim nWriter As iTextSharp.text.pdf.PdfWriter
Dim nCb As iTextSharp.text.pdf.PdfContentByte
Dim nImgFromBitmap As System.Drawing.Image = DirectCast(uBitmap, System.Drawing.Image)
Dim nImg As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(nImgFromBitmap, Imaging.ImageFormat.Png)
Dim bLandscape As Boolean = (nImg.Width > nImg.Height)
'rotation needs to be set before document is being opened
If bLandscape Then
nDocument = New iTextSharp.text.Document(PageSize.A4.Rotate, 0, 0, 0, 0)
Else
nDocument = New iTextSharp.text.Document(PageSize.A4, 0, 0, 0, 0)
End If
'if an exception is raised here, the following will help: https://stackoverflow.com/questions/15833285/pdfwriter-getinstance-throws-system-nullreferenceexception
nWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(nDocument, nFs)
nDocument.Open()
nCb = nWriter.DirectContent
nImg.ScaleToFit(nDocument.PageSize.Width, nDocument.PageSize.Height) 'raises dpi size :-)))
'X-Y-Koordinatensystem 0,0 startet also unten links, nicht oben-links
nImg.SetAbsolutePosition(0, nDocument.PageSize.Height - nImg.ScaledHeight)
nCb.AddImage(nImg)
nDocument.Close()
nWriter.Close()
nFs.Close()
End Sub
It works fine.
However, when I click the image in the PDF, it gets selected.
This is not what I want.
If I click the image in the PDF, it should not be selected.
This is what it looks like: The image becomes blue:
I want to add editable fields to the PDF, so I need to make the image not selectable, else it would confuse the user.
As Abdel-Rahman Al-Qawasmi mentions in his answer, it is completely up to the PDF viewer which entities it makes selectable and which not. Thus, there is no guaranteed way to get what you want.
Nonetheless, there are ways to put an image into a PDF which dissuade current versions of most PDF viewers from making it selectable. These ways either transform the bitmap image into a non-bitmap entity (e.g. by iterating over the pixels of the bitmap and drawing a little rectangle per pixel using vector graphics) or wrap the bitmap image into something that usually is not selectable.
Let's take the latter approach and wrap the image into a page-size PDF pattern with which we then fill the actual page. You can do that by replacing your
nCb.AddImage(nImg)
by
Dim painter As iTextSharp.text.pdf.PdfPatternPainter = nCb.CreatePattern(nDocument.PageSize.Width, nDocument.PageSize.Height)
painter.AddImage(nImg)
nCb.SetColorFill(New iTextSharp.text.pdf.PatternColor(painter))
nCb.Rectangle(0, 0, nDocument.PageSize.Width, nDocument.PageSize.Height)
nCb.Fill()
(This essentially is the VB/iTextSharp pendant of the Java/iText code from this answer.)
This is a pdf program specifications and not related to asp.net or vb.net programming. you need to have control of the pdf reader settings. Or try to use another format.
I am trying to get data from a table in a pdf file using iTextSharp. The table contains Russian symbols that are not displayed correctly after cutting (parsing).
I'm doing this:
Dim rect_for_page_one As New
iTextSharp.text.Rectangle(MillimetersToPoints(39), MillimetersToPoints(33), MillimetersToPoints(163), MillimetersToPoints(297 - 70))
Dim filter As New RegionTextRenderFilter(rect_for_page_one)
Dim strategyOne As ITextExtractionStrategy
strategyOne = New FilteredTextRenderListener(New LocationTextExtractionStrategy(), filter)
Dim output As New String(PdfTextExtractor.GetTextFromPage(PDFReader, 1, strategyOne))
Code screen shot
What do I need to do to get correctly displayed Russian symbols?
I've edited a code I found to gets all the outline data of a text so I can manually draw it as lines in some other program.
My code works great in English, but when I try to send Hebrew text the outlines turn to be in gibberish font.
The main steps of the code are:
Typeface -> GlyphTypeface -> GlyphRun
And the main code is
Private m_gtf As System.Windows.Media.GlyphTypeface
Private m_glypText As GlyphRun
Private m_textFont As System.Drawing.Font
textFont = New Font("Aharoni", 12, FontStyle.Regular, GraphicsUnit.World, 177, False)
m_typeface = New Typeface(New System.Windows.Media.FontFamily(m_textFont.Name), m_fontStyle, _
m_fontWeight, New System.Windows.FontStretch())
m_typeface.TryGetGlyphTypeface(m_gtf)
'then use m_gtf to crate the m_glyphIndices and advanceWidths vectors
m_glypText = New GlyphRun(m_gtf, bidiLevel, False, m_height, m_glyphIndices, origin, advanceWidths, _
Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
I think that my problem is with the "m_typeface = New Typeface(…"
In this command there is no way to send the font gdiCharSet value.
Is there a way to get the typeface straight from the m_textFont?
Or is there another way to do this?
Zohar
Found the problem.
I was using:
Dim glyphIndex As UShort = m_gtf.CharacterToGlyphMap(Asc(m_textString(n)))
but I had to use AscW to get the correct ascii code:
Dim glyphIndex As UShort = m_gtf.CharacterToGlyphMap(AscW(m_textString(n)))
now it works!
Zohar
I need to write some data which is string to tiff file. I am doing in the following way
Dim OFile As System.IO.File
Dim OWrite As system.IO.TextWriter
OWrite = OFile.CreateText("Signature.tiff")
OWrite.Write(ControlData)
MessageBox.Show("Signature is recieved and it is saved in Signature.tiff")
ControlData is the string which is to be written to the file.
I am capturing the signature from the user. This function gets the data in string format and i need to create a tiff file using the string data.
When i did in this way, signature.tiff is created but when i opened the image it is giving no preview available.
Can you tell me what is the problem or correct way of doing this?
Thanks a lot.
Dim format As StringFormat = New StringFormat()
Dim MyRect As Rectangle = New Rectangle(0, 0, 400, 400)
Dim MyGraphics As Graphics = Me.CreateGraphics()
Dim MyImg As Image = New Bitmap(MyRect.Width, MyRect.Height, MyGraphics)
Dim imageGraphics As Graphics = Graphics.FromImage(MyImg)
imageGraphics.FillRectangle(Brushes.White, MyRect)
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
imageGraphics.DrawString("Hello Everyone", objFont, Brushes.Black, RectangleF.op_Implicit(MyRect))
MyGraphics.DrawImage(MyImg, MyRect)
MyImg.Save(filename)
Just see this may help you all for converting text string to image.
Thanks.
TIFF files are binary image files. You are writing the string to a text file. Try opening the file in Notepad to check.
You need a way to create an image in memory and save it to TIFF format.
You could use a PictureBox control. Write the string onto the PictureBox then save as a TIFF.