|
-
Dec 30th, 2009, 08:12 AM
#1
Thread Starter
Frenzied Member
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...
-
Dec 30th, 2009, 09:18 AM
#2
Re: iTextSharp help
Here is some demo code to draw various shapes on a pdf page
vb.net Code:
Public Shared Sub DrawShapesDemo(ByVal sourcePdf As String, ByVal outputPdf As String) Dim reader As iTextSharp.text.pdf.PdfReader = Nothing Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing Dim rect As iTextSharp.text.Rectangle = Nothing Dim pageCount As Integer = 0 Dim borderColor, fillColor As iTextSharp.text.Color Try reader = New iTextSharp.text.pdf.PdfReader(sourcePdf) rect = reader.GetPageSizeWithRotation(1) stamper = New iTextSharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(outputPdf, IO.FileMode.Create)) ' Set up the border and fill color for the shapes to be drawn borderColor = iTextSharp.text.Color.BLUE fillColor = iTextSharp.text.Color.PINK 'Loop thru the pages and draw various shapes to their overcontent layer pageCount = reader.NumberOfPages() For i As Integer = 1 To pageCount 'Get the undercontent layer of this page cb = stamper.GetUnderContent(i) '<<< Note: if you want the drawings to appear on top of the contents (covering it) ' then you need to get the overcontent layer. 'cb = stamper.GetOverContent(i) 'Set the boder color of the shapes cb.SetColorStroke(borderColor) 'Set the fill color of the shapes cb.SetColorFill(fillColor) 'Start drawing shapes. ' >>>> Remember, the cordinate of the LOWER-LEFT corner of a page is (0, 0). Think of it ' as the 1st quadrant in a Cartesean coordinates system. ' 1 in = 72 units, so a 8.5 x 11 page will have a width of 612 units and a height of 792 units. ' Figuring out where to draw your shapes will be much easier if you use a piece of paper to ' plot out the cordinates first. 'Draw a circle centered at (135, 500) with a radius of 50 cb.Circle(135, 500, 50) 'Draw an ellipse that fits in a ractangle with (190, 450) as the lower-left corner 'and (400, 550) as the upper-right corner cb.Ellipse(190, 450, 400, 550) 'Draw a square with the lower-left corner is (410, 450) and the width (and height) = 100 cb.Rectangle(410, 450, 100, 100) 'Draw a rounded rectangle cb.RoundRectangle(150, 330, 200, 100, 20) 'Color fill the shapes above cb.FillStroke() 'Draw a line starting from (150, 310) to (450, 310) cb.MoveTo(150, 310) cb.LineTo(450, 310) cb.Stroke() 'Draw a triangle with vertices (290, 300), (150, 150) and (450, 150) without filling cb.MoveTo(290, 300) cb.LineTo(150, 150) cb.LineTo(450, 150) cb.ClosePathStroke() Next stamper.Close() reader.Close() Catch ex As Exception Throw ex End Try 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 -
-
Dec 30th, 2009, 10:02 AM
#3
Thread Starter
Frenzied Member
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.
-
Dec 30th, 2009, 10:52 AM
#4
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 -
-
Dec 30th, 2009, 11:29 AM
#5
Thread Starter
Frenzied Member
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?
-
Dec 30th, 2009, 12:39 PM
#6
Re: iTextSharp help
 Originally Posted by nbrege
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 -
-
Dec 30th, 2009, 12:58 PM
#7
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|