You must call pdfStamper.Close() even if you set CloseStream to false. There 
are a lot of streams floating around, I advise you to read 
HttpContext.Current.Request.InputStream to a byte[] and see what you got and do 
a pdfFlat.ToArray() to check for the flattened pdf integrity before further 
processing.

Paulo

________________________________
From: Brian English [mailto:[email protected]]
Sent: Friday, February 18, 2011 5:34 PM
To: [email protected]
Subject: [iText-questions] ITextSharp, FDF problem.

Hi,

I posted to the itextsharp mailing list and got a response that it had been 
closed, and to use the itext mailing list.  Maybe someone can help me with this?

We currently have a site which will have a pdf which will post the FDF to a 
url.  The idea is, once the FDF posts to the url, we have some asp.net (vb.net) 
code that will grab the data, flatten it and send an email to a user (basically 
send them the pdf filled in).  As of yet, I have been unable to get this to 
work.  Below is what I am currently doing (this is a variation of an example 
that we found online at:  http://asp-net-whidbey.blogspot.com/

Please note the below example is from a dotnetnuke module, however, for the 
purpose of this, you can ignore the dnn parts:

When the fdf is posted to our page, the page load will fire, and this part 
appears to be functioning correctly:

        Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load
            Me.lblContent.Visible = False
                    If String.Compare(HTTPContext.Current.Request.ContentType, 
"application/vnd.fdf", true) = 0 Then
                ProcessPDF()
                    End If
        End Sub

I may have some incorrect assumptions here.  This is my assumptions:  
pdfformfilename is the pdf file, that we flatten the data to.  I need to read 
the fdf from HTTPContext.Current.Request.InputStream.  Basically, we load the 
pdf into pdfreader.  We create pdfstamper passing in pdfreader and pdfflat.  We 
set the data by using setFields.  After this, we save the file to the server, 
create an email and set the attachment.  A corrupt pdf is being sent, so it is 
working, almost, however, I think I may have some misunderstandings on how some 
of this works.

        Private Sub ProcessPDF()
            Dim pdfFormFileName As String = 
HttpContext.Current.Server.MapPath("NHCS_Employment_Application_20110126_Fillable.pdf")
            Dim pdfFlat As MemoryStream = New MemoryStream()
            Dim pdfreader As PdfReader = New PdfReader(pdfFormFileName)
            Dim pdfStamper As PdfStamper = New PdfStamper(pdfreader, pdfFlat)
            Dim fdfReader As FdfReader = New 
FdfReader(HttpContext.Current.Request.InputStream)
            Dim pdfForm As AcroFields = pdfStamper.AcroFields

            pdfForm.SetFields(fdfReader)
            pdfStamper.FormFlattening = True
            pdfStamper.Writer.CloseStream = False
                    'pdfStamper.Close()

            Dim saveTo As String = 
HttpContext.Current.Server.MapPath(String.Format("/Portals/0/{0}.pdf", 
Guid.NewGuid.ToString().Replace("-", "").Replace(" ", "")))
            Dim pdfStream As MemoryStream = New MemoryStream()
            Dim saveStream As FileStream = New FileStream(saveTo, 
FileMode.Create, FileAccess.Write)
            pdfFlat.WriteTo(saveStream)

            pdfFlat.Flush()
            'ReadWriteStream(pdfStream, saveStream)
            'saveStream.Flush()
            saveStream.Close()
            pdfFlat.Close()

            Send(saveTo)
            Me.lblContent.Visible = True
        End Sub

        Private Function LoadStream(ByVal s As Stream) As Stream
            Dim target As Stream = New MemoryStream()
            Dim length As Integer = 256
            Dim buffer(length) As Byte
            Dim bytesRead As Integer = s.Read(buffer, 0, length)
            While bytesRead > 0
                target.Write(buffer, 0, length)
                bytesRead = s.Read(buffer, 0, length)
            End While
            s.Close()
            Return target
        End Function


        Private Sub ReadWriteStream(ByRef s1 As Stream, ByRef s2 As Stream)
            Dim length As Integer = 256
            Dim buffer(length) As Byte
            Dim bytesRead As Integer = s1.Read(buffer, 0, length)
                    Dim totalbytes As Integer =  0
            While bytesRead > 0
                s2.Write(buffer, 0, length)
                totalbytes = bytesRead + totalbytes
                bytesRead = s1.Read(buffer, 0, length)
            End While
            s1.Close()
        End Sub

        Private Sub Send(ByVal attachement As String)
            Dim fromaddress As String = 
"[email protected]<mailto:[email protected]>"
            Dim toaddress As String = 
"[email protected]<mailto:[email protected]>"
            Dim bcc As String = ""
            Dim subject As String = "New employment application"
            Dim body As String = "Please see the attached application"
            Dim bodytype As String = "HTML"

            Dim smtpUser As String = 
Entities.Host.HostSettings.GetHostSetting("SMTPUser")
            Dim smtpPassword As String = 
Entities.Host.HostSettings.GetHostSetting("SMTPPassword")
            Dim smtpServer As String = 
Entities.Host.HostSettings.GetHostSetting("SMTPServer")
            Dim smtpAuthentication As String = 
Entities.Host.HostSettings.GetHostSetting("SMTPAuthentication")



            DotNetNuke.Services.Mail.Mail.SendMail(fromaddress, toaddress, bcc, 
subject, body, attachement, bodytype, smtpServer, smtpAuthentication, smtpUser, 
smtpPassword)



        End Sub


We are using itextsharp.dll version 5.0.5.  I have a client who is currently 
waiting on some feedback to when I can have this functionality to him.  Any 
help AT ALL would be appreciated.

Brian


Aviso Legal:
Esta mensagem é destinada exclusivamente ao destinatário. Pode conter 
informação confidencial ou legalmente protegida. A incorrecta transmissão desta 
mensagem não significa a perca de confidencialidade. Se esta mensagem for 
recebida por engano, por favor envie-a de volta para o remetente e apague-a do 
seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de 
usar, revelar ou distribuir qualquer parte desta mensagem. 

Disclaimer:
This message is destined exclusively to the intended receiver. It may contain 
confidential or legally protected information. The incorrect transmission of 
this message does not mean the loss of its confidentiality. If this message is 
received by mistake, please send it back to the sender and delete it from your 
system immediately. It is forbidden to any person who is not the intended 
receiver to use, distribute or copy any part of this message.

------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
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

Reply via email to