ITextSharp PDF library and VB Controls. Need some help ...

Hi there everybody.
Mainly for those who are into pdf and in particular iTextSharp library.
In this example I have a very simple Label control on Form, with text in it.
I want to build a very simple pdf test file with just one A4 page, and draw Label's Rectangle and Text exactly as I see on Form. Should be a simple task, as I've always read iTextSharp to be quite powerful library. In fact it's not as simple as it seems.
Here is the code I use for my test :
Dim fullPdfFileName As String = Application.StartupPath & "\" & "test.pdf"
'The FileStream
Dim FS As New System.IO.FileStream(fullPdfFileName, IO.FileMode.Create)
'The Document
Dim DOC As New iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0)
'The PdfWriter
Dim PdfW As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(DOC, FS)
DOC.Open()
'The PdfContentByte
Dim PdfCB As iTextSharp.text.pdf.PdfContentByte = PdfW.DirectContent
'Set Stroke properties
With PdfCB
.SetRGBColorStroke(255, 0, 0) 'RED
.SetLineWidth(1)
End With
'Draw the Label Rectangle
Dim X As Integer = GetDotsFromPixels(100)
Dim Y As Integer = GetDotsFromPixels(100)
Dim W As Integer = GetDotsFromPixels(lbl_test.Width)
Dim H As Integer = GetDotsFromPixels(lbl_test.Height)
PdfCB.Rectangle(X, Y, W, H)
PdfCB.Stroke()
'Draw the Label's Text
Dim BS As iTextSharp.text.pdf.BaseFont = _
iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, _
iTextSharp.text.pdf.BaseFont.CP1252, False)
With PdfCB
.BeginText()
.SetFontAndSize(BS, lbl_test.Font.Size)
.SetTextMatrix(X, Y)
.ShowText(lbl_test.Text)
.EndText()
End With
DOC.Close()
FS.Close()
And this is the very simple GetDotsFromPixels Function that makes 100 Pixels of the Label as one Inch on pdf file and paper, and it works for me :
Private Function GetDotsFromPixels(ByVal pPixels As Integer) As Integer
Return Convert.ToInt32(72 / 100 * pPixels)
End Function
The output is a pdf file with a red rectangle that is exactly 3 Inches in width and 1 Inch in height, while the Label
lbl_test is 300x100.
The problem is the way iTextSharp draws strings and uses Fonts.
The string is printed in black and in the lower-left corner of the rectangle.
The Font of the Label is Arial - Bold and top-left aligned, but there's no Arial or many else windows fonts named in BaseFont standard Fonts...
I need to be able to print out the exact string shown in the label, with its original Font, Size, Color and Position.
I know X Y coordinates are different with pdf page, and Y = 0 is at the bottom of the page, but if I add rectangle's height to string Y coordinate, then the string would be printed above the rectangle and not inside of it, and I would have to "calculate
string's height" too...
Another thing about the Font. iTextSharp's BaseFont seems to lack in properties : where are styles, colors, etc... ?
Maybe there are others Objects and Methods in the library allowing to do what I want...
Thanks to everyone able to help.

Ok, no problem.
Anyway, I've found myself a possible solution, without any use of the BaseFont.
It's based on the following Classes/Methods :
iTextSharp.text.Font
iTextSharp.text.FontFactory.GetFont()
iTextSharp.text.pdf.ColumnText
iTextSharp.text.pdf.ColumnText.SetSimpleColumn()
I post here the complete code :
Dim fullPdfFileName As String = Application.StartupPath & "\" & "test.pdf"
'The FileStream
Dim FS As New System.IO.FileStream(fullPdfFileName, IO.FileMode.Create)
'The Document
Dim DOC As New iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0)
'The PdfWriter
Dim PdfW As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(DOC, FS)
DOC.Open()
'The PdfContentByte
Dim PdfCB As iTextSharp.text.pdf.PdfContentByte = PdfW.DirectContent
'Set Stroke properties
With PdfCB
.SetRGBColorStroke(255, 0, 0) 'RED
.SetLineWidth(1)
End With
'Draw the Label Rectangle
Dim X As Integer = GetDotsFromPixels(200)
Dim Y As Integer = GetDotsFromPixels(100)
Dim W As Integer = GetDotsFromPixels(lbl_test.Width)
Dim H As Integer = GetDotsFromPixels(lbl_test.Height)
PdfCB.Rectangle(X, Y, W, H)
PdfCB.Stroke()
'Draw the Label's Text
Dim F As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont(lbl_test.Font.Name, _
lbl_test.Font.Size)
Dim CT As New iTextSharp.text.pdf.ColumnText(PdfCB)
CT.SetSimpleColumn(New iTextSharp.text.Phrase(lbl_test.Text, F), _
GetDotsFromPixels(200), _
GetDotsFromPixels(100), _
GetDotsFromPixels(200 + lbl_test.Width), _
GetDotsFromPixels(100 + lbl_test.Height), _
GetDotsFromPixels(Convert.ToInt32(F.Size) + 5), _
iTextSharp.text.Element.ALIGN_LEFT)
CT.Go()
DOC.Close()
FS.Close()
I think this is one of the best ways to put some text with desired font and position.
Thanks for attention.

Similar Messages

  • I am the biggest "Newb" on this site,  and I really need some help please..

    Ok, the short of it.... I started training on Database programming about 2 weeks ago. I still don't really know a thing, or even understand the concepts. Now, my boss wants me to fix a bug in Oracle, and I haven't a clue. The ONLY help I've gotten was a link to this forum, and a speech about how I need to help myself. I told him I have the same chance of fixing anything, as monkeys typing hamlet. Anyone up for a challenge?
    When I go to Application System, then Application Entry and enter the password a dialog box appears.... "FRM-40039: Cannot attatch library Empower while opening from APPUSRAPP."
    That's what I have to fix. I have no clue. I don't even really know how to click around in this program. Please help, someone.
    UPDATE: I've been told that everyone's pretty much going to be a jerk to me for asking this, and I'm currently searching old threads and can't find a way to delete this one I posted. So, you could just ignore the post, or jump in and be the internet tough-guy and tell me what I already know.... I'm a newb.
    Edited by: user11033020 on Apr 14, 2009 11:41 AM

    As this appears to be a Forms error (the error begins with FRM) you might get a better response by posting this in the Forms forum:
    Forms
    The Community Feedback forum isn't meant for product-related questions (as it says in the title).
    Still, you should consider searching Metalink as well. If you've paid for support, you have every right to use it.

  • Widget creation on Flex builder and related problems - need some help

    Hi all.
    I am trying to create a widget like application and it seems
    I have made a mistake. As you know, desktop widgets are like small
    stickers, they do not appear on taskbar and can be hidden or
    docked.
    The problem is that I created an AIR application using Flex
    Builder 3 Beta and now I cannot find how to make that application
    behave like custom-shaped window. My app appears on Windows taskbar
    and I don't want it. When I drag the window (which is a custom
    shape) it shows that nasty dragging rectangle around it.
    As far as I found out (and tried it) I need to use cod like:
    var initOptions:NativeWindowInitOptions = new
    NativeWindowInitOptions();
    initOptions.systemChrome = NativeWindowSystemChrome.NONE;
    initOptions.type = NativeWindowType.UTILITY;
    to hide my window from the taskbar. But it seems I cannot use
    such a code for class mx:Application. Or can I (would be grateful
    for some idea)?
    The main question is:
    if I want to migrate my app to that NativeWindow based
    solution - then NativeWindow is not an mx class and I cannot find
    how I will use Flex Builder Design mode to arrange my GUI elements.
    Is it somehow possible to wrap my mx:Application inside
    NativeWindow and do not have to migrate the design from mxml files
    to pure ActionScript?
    The second issue - if I want to use some kind of subforms -
    like widget settings window - when I move that window with the
    mouse, it is being clipped to the region of my Application. Does
    that again mean that I need to use NativeWindow for creating such a
    "Widget settings" dialog window?
    Thanks for any ideas (and especially for useful links and
    examples :-) )

    Hi all.
    I am trying to create a widget like application and it seems
    I have made a mistake. As you know, desktop widgets are like small
    stickers, they do not appear on taskbar and can be hidden or
    docked.
    The problem is that I created an AIR application using Flex
    Builder 3 Beta and now I cannot find how to make that application
    behave like custom-shaped window. My app appears on Windows taskbar
    and I don't want it. When I drag the window (which is a custom
    shape) it shows that nasty dragging rectangle around it.
    As far as I found out (and tried it) I need to use cod like:
    var initOptions:NativeWindowInitOptions = new
    NativeWindowInitOptions();
    initOptions.systemChrome = NativeWindowSystemChrome.NONE;
    initOptions.type = NativeWindowType.UTILITY;
    to hide my window from the taskbar. But it seems I cannot use
    such a code for class mx:Application. Or can I (would be grateful
    for some idea)?
    The main question is:
    if I want to migrate my app to that NativeWindow based
    solution - then NativeWindow is not an mx class and I cannot find
    how I will use Flex Builder Design mode to arrange my GUI elements.
    Is it somehow possible to wrap my mx:Application inside
    NativeWindow and do not have to migrate the design from mxml files
    to pure ActionScript?
    The second issue - if I want to use some kind of subforms -
    like widget settings window - when I move that window with the
    mouse, it is being clipped to the region of my Application. Does
    that again mean that I need to use NativeWindow for creating such a
    "Widget settings" dialog window?
    Thanks for any ideas (and especially for useful links and
    examples :-) )

  • I have 2 iphoto libraries but one is lost, missing, cannot seem to find it... tried an apple care person - they were no help - we use Time Machine for external backup... but cannot find my older iPhoto library! I desperately need some help!

    I have recently had something really odd happen within iphoto.  My photos from May-Oct 2013 appeared to be missing from my primary Iphoto library.
    last night I was trying to find any additional Iphoto libraries on my Imac... I could not find them... but then somehow the May-Oct 2013 library showed up and now I cannot find the "current" primary Iphoto library.   I sought help from apple care today but no solutoin from them after 2 hours trying.  I have timemachine that backs up my Imac each day to an external hard drive.   When I open Iphoto while holding down the "option" key - it lets me choose which Iphoto library... but only (1) library comes up...  I can't figure out what happened - but it seems that I must have more than one Iphoto library because yesterday I was able to see all my photos except the "gap in dates" from May-Oct 2013.  Today - I can only open a Library called Iphoto2014... and it contains the photos from the gap in dates.  I can't "see" any other options to open photos... but just yesterday there appeared to be a different library available.
    Any advice or steps i should take to locate my missing photos - would be soooooo much appreciated.

    I have recently had something really odd happen within iphoto.  My photos from May-Oct 2013 appeared to be missing from my primary Iphoto library.
    last night I was trying to find any additional Iphoto libraries on my Imac... I could not find them... but then somehow the May-Oct 2013 library showed up and now I cannot find the "current" primary Iphoto library.   I sought help from apple care today but no solutoin from them after 2 hours trying.  I have timemachine that backs up my Imac each day to an external hard drive.   When I open Iphoto while holding down the "option" key - it lets me choose which Iphoto library... but only (1) library comes up...  I can't figure out what happened - but it seems that I must have more than one Iphoto library because yesterday I was able to see all my photos except the "gap in dates" from May-Oct 2013.  Today - I can only open a Library called Iphoto2014... and it contains the photos from the gap in dates.  I can't "see" any other options to open photos... but just yesterday there appeared to be a different library available.
    Any advice or steps i should take to locate my missing photos - would be soooooo much appreciated.

  • Multiple same artists and albums missing, need some help pleas

    Hi,
    I just bought a Creative Zen mp3 player and for some reason, when I edit the name and info of my songs (artist, genre, album...ect...) , my player won't pick it up properly. I can have 2 or 3 different "Beatles" under artist, yet, they are spelled EXACTLY the same way. (capital letter, space, eveything is the same)
    Now I used to have the same problem with my Creative Zen V Plus but I was always able to make it work after doing some copy & paste while editing the tracks info. This time, it's not working.
    My second problem is the one that really bugs me the most. When I select certain artists on my player, some of the albums are missing. Ex: I select "Beatles" in my artists list and then I'm expecting to find all the albums but there are only 2 showing. Yet, if I go in the albums list,there're all gonna be there. And?as I said, the names & different info are all spelled properly. EVERYTHING IS THE SAME ! I spend alot of time editing my tracks info because I want everything to be organised perfectly on my player and it's getting really frustrating.
    Anyone has a solution? thank you so much !

    I am having a very similar problem. I had the problem, was told about Media Monkey, organized everything through Media Monkey, deleted all my songs off my Zen X-Fi 6GB and reloaded my songs. Again songs are missing. Here is the interesting thing. Media Monkey shows 273 items in the playlist and my Zen shows 273 songs So where are they?
    I too am getting VERY FRUSTRATED! Please let me know if you find a solution!!
    Thanx!

  • Robot arm control::: need some help

    hi
    i try to bulid this system using labview it simpaly pich and place an object on the table, the location of object found by camera relative to frame on the edage of table
    i have D_H parametrs of the robot
    does any one have information about this type of system
    what is the genral steps to do that/??
    or what is the next step
    best regardd
    hi ?Q>

    Dear member
    thank for replay
    to simplify the problem I draw this figure as below
    I have the physical robot which is take from here
    http://www.robotmatrix.org/lynxmotion-robots.htm
    I have build the vision system using labview to find the object  center position relative to base of of robot as shown below
    I have the D_H parameters of it
    I have the forward transformation matrices from gripper  frame axis (x6,x6) to base frame Axis (x1,y1) of the robot
    my goal is to  to convert the object position (X,Y) to angle OF ARM so that the gripper become at  object center???
    also did I put the link length in centimeter or meter in transformation matrices???
    also to to simplify now the object all ways lock like  acircle
    best REGARDS
    hi ?Q>

  • Need some help with downloading PDF's from the net.

    need some help with downloading PDF's from the net.  Each time I try to click on a link from a website, if it takes me to a new screen to view a pdf, it just comes up as a blank black screen?  any suggestions?

    Back up all data.
    Triple-click the line of text below to select it, the copy the selected text to the Clipboard (command-C):
    /Library/Internet Plug-ins
    In the Finder, select
    Go ▹ Go to Folder
    from the menu bar, or press the key combination shift-command-G. Paste into the text box that opens (command-V), then press return.
    From the folder that opens, remove any items that have the letters “PDF” in the name. You may be prompted for your login password. Then quit and relaunch Safari, and test.
    The "Silverlight" web plugin distributed by Microsoft can also interfere with PDF display in Safari, so you may need to remove it as well, if it's present.
    If you still have the issue, repeat with this line:
    ~/Library/Internet Plug-ins
    If you don’t like the results of this procedure, restore the items from the backup you made before you started. Relaunch Safari again.

  • Need some help: my itunes won't sync with my iphone anymore. Both softwares are up to date and I've reinstalled itunes. After I reinstall, it syncs. But then when I reboot my computer, it no longer syncs anymore and I have to keep reinstalling itunes. Tho

    Need some help: my itunes won't sync with my iphone anymore. Both softwares are up to date and I've reinstalled itunes. After I reinstall, it syncs. But then when I reboot my computer, it no longer syncs anymore and I have to keep reinstalling itunes. Thoughts???

    thought that it was possible to have the same iTunes library on any machine that was authorised, a kind of cloud-iTunes I guess. 
    That would convenient, but sadly it is not the case. Sorry!
    Either way your welcome and best of luck!
    B-rock

  • Need some help in saving video message from viber to my Iphone. I disabled the thing that would save photos and videos automatically then, there comes a video I want to save. After loading and watching it, I press the "save to gallery"

    Need some help in saving video message from viber to my Iphone 5S with new ios 8's program . I disabled the thing that would save photos and videos automatically then, there comes a video I want to save. After loading and watching it, I press the "save to gallery" thing but it doesn't save in gallery. I tried all, restarting my phone, rebooting then turning on the save automatically thing and when I watch it again, it still wouldn't save.

    Probably a good question to ask Viber or look at their support site.

  • Hi! I got movies on my external hard drive that are AVI kind and won't play on my macbook pro? need some help please!!

    Hi! I got Movies on my external hard drive that are AVI kind and won't play on my macbook pro? When I start playing the movie a message pops up and says "a required codec is not available". I tried flip4mac, xvid, divx already and still not playing my video. need some help please!! thanks.

    Although vlc mentioned above is a much more powerful and better player you could try installing Perian if you insist on using the quicktime player.  It may supply the codec it needs.
    Not sure why you wouldn't be able to play straight avi files though in quicktime.

  • I need some help resizing my images on PS6. I am using a mac and have been trying to resize with same resolution and constaining proportions but for some reaseon the smaller resized image appears pizelated.

    I need some help resizing my images on PS6. I am using a mac and have been trying to resize with same resolution and constaining proportions but for some reaseon the smaller resized image appears pizelated. Heres an image of before and after. The first image I use is a JPG 72dpi 1500px x1500px and I want to downsize it to 600x600px same res, but it keeps pixelating, this has never happened before. Any suggestions, thoughts?
    thanks!

    I wouldn't say pixelated; more like blurry.
    Like ConnectedCreative said, what steps are you using? Are you using "bicubic sharper" when resizing down?

  • My iphone 4 has switch of itself, and cant switch on its blankout , i need some help to solve this matter, My iphone 4 has switch of itself, and cant switch on its blankout , i need some help to solve this matter

    i need some help! my i phone has switch off totally blank as if there is no baterry, i cant wsitch on anymore, i live in Africa whereby there is no any  apple offises arroung, i want to find a solution thats why i am asking for the help

    Plug in iPhone with Wall Charger for 10 minutes, it may turn on itself. If not, keep on Wall Charger and Reset, hold both Home and Power buttons until the iPhone begins to restart. This usually takes less than 20 seconds of holding both buttons.

  • I'm new to itunes and need some help. Can anyone tell me if it is possible to download an album which has explicit content without actually downloading the explicit songs?

    I'm new to itunes and need some help.  Can anyone tell me if it is possible to download an album which has explicit songs without actually downloading the songs that are explicit?

    See if there's a non-explicit version and download that?  Just download the individual songs you want, and not the others?
    Honestly, your phrasing doesn't make sense.  You're asking how to download an album without downloading its tracks? 

  • Ok i have indesign CS6 and all files are stored on a 10.7.5 MAC server.  This just started happening.  When I open a file indesigns is creating a textfile in the same folder?  I need some help on this.

    Ok i have indesign CS6 and all files are stored on a 10.7.5 MAC server.  This just started happening.  When I open a file indesigns is creating a textfile in the same folder?  I need some help on this.

    Ask in the ID forum and be much more specific about your configuration. there could be any number of reasons why manifests, temp files or restore files are created.
    Mylenium

  • HT1222 I need some help.... I Have the iPhone 4S and downloaded the new software (6.0) and everything seems great so far except when I try to open a link in safari from a different app (messages, Facebook, ext..) it glitches and won't load. Any advice?

    I need some help.... I Have the iPhone 4S and downloaded the new software (6.0) and everything seems great so far except when I try to open a link in safari from a different app (ie..messages, Facebook, ect..) it glitches and won't load. The blue loading bar will just jump back and forth and the page will not load properly if at all....

    It sounds like you may have multiple problems, but none of them are likely to be caused by malware.
    First, the internet-related issues may be related to adware or a network compromise. I tend to lean more towards the latter, based on your description of the problem. See:
    http://www.adwaremedic.com/kb/baddns.php
    http://www.adwaremedic.com/kb/hackedrouter.php
    If investigation shows that this is not a network-specific issue, then it's probably adware. See my Adware Removal Guide for help finding and removing it. Note that you mention AdBlock as if it should have prevented this, but it's important to understand that ad blockers do not protect you against adware in any way. Neither would any kind of anti-virus software, which often doesn't detect adware.
    As for the other issues, it sounds like you've got some serious corruption. I would be inclined to say it sounds like a failing drive, except it sounds like you just got it replaced. How did you get all your files back after the new drive was installed?
    (Fair disclosure: I may receive compensation from links to my sites, TheSafeMac.com and AdwareMedic.com, in the form of buttons allowing for donations. Donations are not required to use my site or software.)

Maybe you are looking for

  • MBP on a big screen

    Got my 15" MBP hooked up to my 55" sony and man it's great! I've heard that you can't use a big TV for normal computing needs like reading e-mail etc. because the image isn't clear enough. Hogwash!! its as clear as my monitor only a LOT BIGGER. My qu

  • No accounting document for 561E via MB1C and MIGO

    Dear All We are in MTO Scenario. we are trying to post initial stock against sales order through MB1C VIA 561E movement. the material document is not having an accounting document. Please help out. I've checked all setting as follows: 1.) [OMJJ] for 

  • Latest Reader 11.0.03 & 10.1.7 Update Causes PDF from Quicken to be Unreadable

    There are display issues with the latest Reader 11.0.03 & 10.1.7 Update with PDFs produced by Quicken PDF Printer from Quicken 2013 (Amyuni PDF Converter 4.0.0.7 producing a PDF version 1.3).  The latest update has made the text (not images) unreadab

  • OS X WI-FI problem

    I just updated to OS X 10.9 and I tried connecting to the internet and it said that the passwords wrong and it dident have a IP address I tried the diagnostics for wi-FI and restarting it but nothing's working please help me with this problem

  • Crash on save/export

    We have recently installed a new editorial system (Newspaper) and started using InDesign 5.5 (7.5.2.318) extensively.  However the editors keep having crashes happen to them when they save or export a document.  The error message I'm seeing in event