Results 1 to 7 of 7

Thread: iTextSharp help

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    iTextSharp help

    I'm looking for a code snippet that will add graphics (lines, circles, etc.) to an existing pdf file & write it out as a new file. Thanks...

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: iTextSharp help

    Here is some demo code to draw various shapes on a pdf page
    vb.net Code:
    1. Public Shared Sub DrawShapesDemo(ByVal sourcePdf As String, ByVal outputPdf As String)
    2.         Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
    3.         Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing
    4.         Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing
    5.         Dim rect As iTextSharp.text.Rectangle = Nothing
    6.         Dim pageCount As Integer = 0
    7.         Dim borderColor, fillColor As iTextSharp.text.Color
    8.         Try
    9.             reader = New iTextSharp.text.pdf.PdfReader(sourcePdf)
    10.             rect = reader.GetPageSizeWithRotation(1)
    11.             stamper = New iTextSharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(outputPdf, IO.FileMode.Create))
    12.             ' Set up the border and fill color for the shapes to be drawn
    13.             borderColor = iTextSharp.text.Color.BLUE
    14.             fillColor = iTextSharp.text.Color.PINK
    15.             'Loop thru the pages and draw various shapes to their overcontent layer
    16.             pageCount = reader.NumberOfPages()
    17.             For i As Integer = 1 To pageCount
    18.                 'Get the undercontent layer of this page
    19.                 cb = stamper.GetUnderContent(i)
    20.                 '<<< Note: if you want the drawings to appear on top of the contents (covering it)
    21.                 ' then you need to get the overcontent layer.
    22.                 'cb = stamper.GetOverContent(i)
    23.  
    24.                 'Set the boder color of the shapes
    25.                 cb.SetColorStroke(borderColor)
    26.                 'Set the fill color of the shapes
    27.                 cb.SetColorFill(fillColor)
    28.                 'Start drawing shapes.
    29.  
    30.                 ' >>>> Remember, the cordinate of the LOWER-LEFT corner of a page is (0, 0). Think of it
    31.                 ' as the 1st quadrant in a Cartesean coordinates system.
    32.                 ' 1 in = 72 units, so a 8.5 x 11 page will have a width of 612 units and a height of 792 units.
    33.                 ' Figuring out where to draw your shapes will be much easier if you use a piece of paper to
    34.                 ' plot out the cordinates first.
    35.  
    36.                 'Draw a circle centered at (135, 500) with a radius of 50
    37.                 cb.Circle(135, 500, 50)
    38.                 'Draw an ellipse that fits in a ractangle with (190, 450) as the lower-left corner
    39.                 'and (400, 550) as the upper-right corner
    40.                 cb.Ellipse(190, 450, 400, 550)
    41.                 'Draw a square with the lower-left corner is (410, 450) and the width (and height) = 100
    42.                 cb.Rectangle(410, 450, 100, 100)
    43.                 'Draw a rounded rectangle
    44.                 cb.RoundRectangle(150, 330, 200, 100, 20)
    45.                 'Color fill the shapes above
    46.                 cb.FillStroke()
    47.                 'Draw a line starting from (150, 310) to (450, 310)
    48.                 cb.MoveTo(150, 310)
    49.                 cb.LineTo(450, 310)
    50.                 cb.Stroke()
    51.                 'Draw a triangle with vertices (290, 300), (150, 150) and (450, 150) without filling
    52.                 cb.MoveTo(290, 300)
    53.                 cb.LineTo(150, 150)
    54.                 cb.LineTo(450, 150)
    55.                 cb.ClosePathStroke()
    56.             Next
    57.             stamper.Close()
    58.             reader.Close()
    59.         Catch ex As Exception
    60.             Throw ex
    61.         End Try
    62.     End Sub
    There are 7 methods in the PdfContentByte class that you can use to practically draw any shapes you want. They are:
    Code:
    MoveTo(x,y): Move the current point to cordinates(x,y)
    
    LineTo(x,y): Draw a line from the current point to the point (x,y) and update the current pint to (x,y)
    
    CurveTo(x1, y1, x2, y2, x3, y3): Move the current point to cordinates(x3, y3), appending a Bezier curve from the previous to the current point using (x1, y1) and (x2, y2) as control points for the curve.
    
    CurveTo(x1, y1, x2, y2): Move the current point to (x2, y2) appending a Bezier curve from the previous point to the new current point using the previous point and (x1, y1) as control points for the curve.
    
    CurveFromTo(x1, y1, x2, y2): Move cuurent point to (x2, y2) appending a Bezier curve using (x1, y1) and (x2, y2) as control points for the curve
    
    ClosePath(): close the current path by appending a straight line from the current point to the starting point of the path
    
    Rectangle(x, y, width, height): appends a rectangle to the current path as a complete subpath.
    Last edited by stanav; Dec 30th, 2009 at 09:21 AM.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: iTextSharp help

    Thanks stanav, this is a big help. Also, one more quick question - how can I add text to a pdf? I tried doc.Add(New Phrase("Hello World")) & doc.Add(New Paragraph("Hello World")) which does add text, but only in the upper left of the document. I need to be able to specify the location x/y as well.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: iTextSharp help

    You can't... A pdf page has 4 layers. The The text and background (called direct content layers) are in 2 middle layers that are private and cannot be modified. We only have access to the undercontent and overcontent layers. The only time when we have access to the direct content layers is when we create a pdf from scratch.
    You maybe able to workaround by adding your text on the overcontent layer, but since it's not on the direct content layer, the newly added text will not be part of the content and thus it can't be indexed, searched or extracted...
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: iTextSharp help

    Very good information stanav, thanks. So if I create a document from scratch can I specify text placement? This code:

    Code:
    Dim doc As Document = New Document(PageSize.LETTER) 'A portrait
    PdfWriter.GetInstance(doc, New FileStream(outputFile, FileMode.Create))
    
    doc.Open()
    doc.Add(New Phrase("Hello World"))
    doc.Close()
    will add the text, but only in the upper left corner, as I mentioned. Is there a way to specify position if creating a new document?

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: iTextSharp help

    Quote Originally Posted by nbrege View Post
    Very good information stanav, thanks. So if I create a document from scratch can I specify text placement? This code:

    Code:
    Dim doc As Document = New Document(PageSize.LETTER) 'A portrait
    PdfWriter.GetInstance(doc, New FileStream(outputFile, FileMode.Create))
    
    doc.Open()
    doc.Add(New Phrase("Hello World"))
    doc.Close()
    will add the text, but only in the upper left corner, as I mentioned. Is there a way to specify position if creating a new document?
    Yes. You have to get the direct content and perform low-level operations using MoveText and ShowText... Something like this:
    Code:
    'Write the string "Hello World!" starting at (200, 400)
    
    Dim doc As Document = New Document(PageSize.LETTER) 'A portrait
    Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(outputFile, FileMode.Create))
    doc.Open()
    Dim cb As PdfContentByte = writer.DirectContent
    Dim fnt As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
    cb.SaveState()
    cb.SetFontAndSize(fnt, 14)
    cb.BeginText()
    cb.MoveText(200, 400) 'Move the current point to this position
    cb.ShowText("Hello World!") 'Then write the text
    cb.EndText()
    cb.RestoreState()
    doc.Close()
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: iTextSharp help

    That worked. Thanks again for all your iText help stanav. Have a great new year...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width