All
I had a task recently to create a master customer electronic billing
document
the first document was a multipage pdf invoice the subsequent documents
were scanned 1 page images in a variety of images formats.
Each master customer billing pdf document was later combine into a single
batch pdf document.
I choose to use the iTextSharp.text.pdf.PdfSmartCopy interface
to append the 1 pdf document to the master oPdfCopy
The use pattern in vb.net looks like this (w/o the declares)
& oFiles is an xml node structure with the files to combine
oPdfDoc=New iTextSharp.text.Document(PageSize.LETTER)
oFileStream = New
System.IO.FileStream(oFiles.GetAttribute("OutputFilePath"),
IO.FileMode.Create, IO.FileAccess.ReadWrite)
oPdfCopy = New iTextSharp.text.pdf.PdfSmartCopy(oPdfDoc, oFileStream)
For Each oFile In oFiles.ChildNodes
Select Case Path.GetExtension(oFile.GetAttribute("FilePath")).ToLower
Case ".jpg", ".png", ".bmp", ".gif", "tif", "wmf"
oPdfImgDoc = New iTextSharp.text.Document(PageSize.LETTER)
oStream = New System.IO.MemoryStream
oPdfImgWri =
iTextSharp.text.pdf.PdfWriter.GetInstance(oPdfImgDoc, oStream)
oPdfImgWri.CloseStream = False
oPdfImgDoc.Open() '//Open Document to write
'http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images
Dim oImg As iTextSharp.text.Image =
iTextSharp.text.Image.GetInstance(oFile.GetAttribute("FilePath"))
oImg.ScalePercent(24.0F) 'scale% a std image from 72dpi to
300dpi for pritning 72/300 * 100
'oImg.ScaleToFit(oPdfImgDoc.PageSize.Width,
oPdfImgDoc.PageSize.Height)
'oImg.SetAbsolutePosition(oPdfImgDoc.PageSize.Width,
oPdfImgDoc.PageSize.Height)
oPdfImgDoc.Add(oImg)
oPdfImgDoc.Close()
oStream.Seek(0, IO.SeekOrigin.Begin)
oPdfReader = New iTextSharp.text.pdf.PdfReader(oStream)
For pageLoop = 1 To oPdfReader.NumberOfPages
oPdfCopy.AddPage(oPdfCopy.GetImportedPage(oPdfReader,
pageLoop))
Next
oPdfCopy.FreeReader(oPdfReader)
oPdfCopy.Flush()
Case ".pdf"
oPdfReaderFile = New
iTextSharp.text.pdf.RandomAccessFileOrArray(oFiles.GetAttribute("OutputFilePath")",
True)
oPdfReader = New iTextSharp.text.pdf.PdfReader(oPdfReaderFile,
Nothing)
For pageLoop = 1 To oPdfReader.NumberOfPages
oPdfCopy.AddPage(oPdfCopy.GetImportedPage(oPdfReader,
pageLoop))
Next
Next
The case statement above is a bit of a hack for images but it does work
but its not very efficient
So I struggled a bit to find an interface that exposed a full pdfwriter via
iTextSharp.text.pdf.PdfSmartCopy interface so I could just
append images to the iTextSharp.text.pdf.PdfSmartCopy interface to
pdfwriter
This was a couple of attempts that failed :(
oPdfCopy.NewPage()
oPdfCopy.Add(oImg)
then I tried this
oPdfCopy.NewPage()
oIndirectPageref = oPdfCopy.GetPageReference(iPageCnt)
oPdfCopy.AddDirectImageSimple(sigImg, oIndirectPageref)
but I could never get the image into the pdf pages
Can anyone drop a hint as to how you can use the
iTextSharp.text.pdf.PdfSmartCopy to append images
& create a new page in the pdfcopy document
It seems like AddDirectImageSimple would be the route to go
if I could properly create a new blank page using the pdfcopy & get
back an the coorect page reference but oPdfCopy.NewPage()
doers not seems to be the magic bullet :(
------------------------------------------------------------------------------
Lotusphere 2011
Register now for Lotusphere 2011 and learn how
to connect the dots, take your collaborative environment
to the next level, and enter the era of Social Business.
http://p.sf.net/sfu/lotusphere-d2d
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php