locked
iTextSharp not working on the server RRS feed

  • Question

  • User1210767569 posted

    The below code was working and create a corruption PDF that is 1kb, usually it would be around 250kb. I can not see anything wrong with the code though!

    I would appreciate a second pair of eyes to see if there is something 'obvious'!

    The URL created is correct, a PDF is created so I suspect it might not be getting the file or closing off correctly as I can not delete the files either without restarting IIS.

     

     Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GridView1.SelectedIndexChanged
    
            Session.Item("ID") = ""
            Session.Item("ID") = sender.SelectedValue.ToString
    
    
            If sender.SelectedValue.ToString = "" Or sender.SelectedValue.ToString = Nothing Then
    
                lblError.Text = "Sorry, unable to find, please try again"
    
            Else
                Try
                    PanelDetails.Visible = True
                    PanelMain.Visible = False
                    GridView1.Visible = False
    
                    Dim sConnString As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("GWmycompanySQL").ConnectionString
                    Dim dsNames As SqlDataSource
    
                    dsNames = New SqlDataSource
                    dsNames.ConnectionString = sConnString
                    Dim sSQL As String
                    Dim vID As String
                    Dim vName As String
                    vID = (sender.SelectedValue.ToString)
                    sSQL = "SELECT * FROM tbresults WHERE ID =" & Session.Item("ID")
                    dsNames.SelectCommand = sSQL
                    For Each datarow As Data.DataRowView In dsNames.Select(DataSourceSelectArguments.Empty)
    
                        ModuleNameDisplay.Text = datarow("ModuleName")
                        ScorePercentDisplay.Text = datarow("Score")
                        PassDisplay.Text = datarow("Pass")
    
                        Session.Item("moduleIDPDF") = datarow("ModuleID")
                        Session.Item("EmployeeIDPDF") = datarow("EmployeeID")
    
    
                        If datarow("Pass") = "Yes" Then
    
                            '############ Create PDF
    
                            Dim input As String
                            Dim output As String
                            Dim water As String
                            Dim vDate As Date
                            vDate = Now.Date
                            Dim vModuleName As String
                            Dim outputURL As String
    
                            input = "C:\sites\companygateway\www\gateway\apps\elearning\assessments\Certificates\blank.pdf"
                            water = "C:\notused.jpg"
                            output = "C:\sites\companygateway\www\gateway\apps\elearning\assessments\Certificates\Completed\" & datarow("moduleID") & "-" & datarow("EmployeeID") & ".pdf"
    
                            vName = datarow("fullname")
    
                            vModuleName = datarow("ModuleName")
    
                            outputURL = "http://www.ward-security.info/gateway/apps/elearning/assessments/certificates/completed/" & datarow("moduleID") & "-" & datarow("EmployeeID") & ".pdf"
    
                            AddWatermarkImage(input, output, water, vName, vDate, vModuleName)
    
                            '###########################
                            '#########################
    
                            Session.Item("EmailPath") = datarow("ModuleID") & "-" & datarow("EmployeeID") & ".pdf" ' need this for email string attachment!
                            Session.Item("PDFpath") = "http://www.ward-security.info/gateway/apps/elearning/assessments/certificates/completed/" & datarow("moduleID") & "-" & datarow("EmployeeID") & ".pdf"
                            PDFHyperLink.NavigateUrl = Session.Item("PDFpath")
    
                            PDFHyperLink.Visible = True
                            PDFText.Visible = True
                            btnEmail.visible = True
                            txtEmail.visible = True
                        Else
                            PDFHyperLink.Visible = False
                            PDFText.Visible = False
                            btnEmail.visible = False
                            txtEmail.visible = False
                        End If
                    Next
                Catch
    
                End Try
    
    
            End If
    
            With oAuditItem
                .EventDescription = "Details of eLearning looked at"
                .EventType = "LOG"
                .companyModule = "Admin - eLearning.aspx"
                .SiteID = 0
                .UserID = CInt(Session.Item("UserID"))
            End With
    
            oAudit.WriteAuditTrailItem(oAuditItem)
    
        End Sub
    
        '################### PDF code '##############################################################
        Public Shared Sub AddWatermarkImage(ByVal sourceFile As String, ByVal outputFile As String, ByVal watermarkImage As String, ByRef vName As String, ByRef vDate As String, ByRef vModuleName As String)
            ' ByVal vName As String, ByVal vModuleName As String, ByVal vDate As String
    
            Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
            Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing
            Dim img As iTextSharp.text.Image = Nothing
            Dim img1 As iTextSharp.text.Image = Nothing
            Dim underContent As iTextSharp.text.pdf.PdfContentByte = Nothing
            Dim overContent As iTextSharp.text.pdf.PdfContentByte = Nothing
            Dim rect As iTextSharp.text.Rectangle = Nothing
    
            'Dim X, Y As Single
            Dim pageCount As Integer = 0
            Try
    
                If System.IO.File.Exists(sourceFile) Then
                    reader = New iTextSharp.text.pdf.PdfReader(sourceFile)
                    rect = reader.GetPageSizeWithRotation(1)
                    stamper = New iTextSharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(outputFile, IO.FileMode.Create))
    
                    'img = iTextSharp.text.Image.GetInstance("C:\sites\PDF\Img\certificate_header.jpg")
    
                    'If img.Width > rect.Width OrElse img.Height > rect.Height Then
                    '    img.ScaleToFit(rect.Width, rect.Height)
                    '    X = (rect.Width - img.ScaledWidth) / 2
                    '    Y = (rect.Height - img.ScaledHeight) / 2
                    'Else
                    '    X = (rect.Width - img.Width) / 2
                    '    Y = (rect.Height - img.Height) / 2
                    'End If
                    '###
    
                    pageCount = reader.NumberOfPages()
                    For i As Integer = 1 To pageCount
    
                        '#############
                        overContent = stamper.GetOverContent(i) ' can be over or under the existing layers
                        'underContent = stamper.GetUnderContent(i) ' can be over or under the existing layers
    
                        '  img.SetAbsolutePosition(X, Y)
                        ' overContent.AddImage(img)
    
                        ' img1.SetAbsolutePosition(X, 500)
                        ' overContent.AddImage(img1)
                        ''//Add it directly to the raw pdfwriter instead of the document helper. DirectContent is above and DirectContentUnder is below
    
                        '#####
                        watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
    
                        'We can either use preset colours from iTextSharp or we can create our own colours using the HTML translator
    
                        '#####################
                        watermarkFontColor = iTextSharp.text.Color.BLACK
                        overContent.BeginText() ' black set text first 
                        overContent.SetFontAndSize(watermarkFont, 22)
                        overContent.SetColorFill(watermarkFontColor)
                        overContent.ShowTextAligned(Element.ALIGN_CENTER, "This is to certify that", 300, 625, 0)
                        overContent.ShowTextAligned(Element.ALIGN_CENTER, "Successfully completed", 300, 475, 0)
                        overContent.ShowTextAligned(Element.ALIGN_CENTER, "E-Learning Module", 300, 325, 0)
                        overContent.ShowTextAligned(Element.ALIGN_CENTER, "on", 300, 275, 0)
                        overContent.EndText()
    
                        '#####################
                        watermarkFontColor = New iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#021d62"))
                        overContent.BeginText() ' black set text first 
                        overContent.SetFontAndSize(watermarkFont, 26)
                        overContent.SetColorFill(watermarkFontColor)
                        overContent.ShowTextAligned(Element.ALIGN_CENTER, vName, 300, 550, 0)
                        overContent.ShowTextAligned(Element.ALIGN_CENTER, vModuleName, 300, 410, 0)
                        overContent.ShowTextAligned(Element.ALIGN_CENTER, vDate, 300, 195, 0)
                        overContent.EndText()
    
                    Next
                    stamper.Close()
                    reader.Close()
    
                Else
                    ' MsgBox("Unable to find certificate", "Unable to find certificate")
                End If
            Catch ex As Exception
                ' Throw ex
            End Try
    
        End Sub

    Wednesday, February 19, 2014 12:43 PM

Answers

  • User1508394307 posted

    Get rid of try..catch in both methods and see if you will get any error.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 19, 2014 5:11 PM

All replies

  • User1508394307 posted

    Get rid of try..catch in both methods and see if you will get any error.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 19, 2014 5:11 PM
  • User1210767569 posted

    Had a session not setting correctly!

    Thursday, February 20, 2014 5:06 AM