This line is wrong:

output.Write(ms2.GetBuffer(), 0, ms2.GetBuffer.Length)

it should be (in C#):

byte[] b = ms2.ToArray();
output.Write(b, 0, b.Length)

Paulo


________________________________________
From: Teun Ham [[email protected]]
Sent: Tuesday, July 06, 2010 1:10 PM
To: [email protected]
Subject: [iText-questions] SetFullCompression results in unreadable PDF

To all,

This is my first-time to post on this mailing list:

We are using Visual Studio.Net 2008 and iTextSharp (version 4.1.6.0) to add
a Barcode39 to our existing PDFs. The following routine will create a new
PDF, based upon an existing PDF, with a Barcode.

The only thing which is strange: when I enable 'FullCompression', the new
PDF is not readable with Adobe Reader 9.3.3. It states: "An error occured
when opening this document. The file is damaged and could not be repared"
(translated from dutch to english).

We have no clue why the FullCompression results in an unreadable PDF.

Any clues?

[code]
    Sub AddBarcode39ToPDF(ByVal sInputFileName As String, ByVal
sOutputFileName As String, ByVal sBarcodeText As String)
        Dim Reader As New iTextSharp.text.pdf.PdfReader(sInputFileName)
        Dim ms2 As New MemoryStream
        Dim stamper As iTextSharp.text.pdf.PdfStamper = New
iTextSharp.text.pdf.PdfStamper(Reader, ms2)
        Dim pageCount As Integer = 0
        Dim overContent As iTextSharp.text.pdf.PdfContentByte = Nothing
        Dim rect As iTextSharp.text.Rectangle =
Reader.GetPageSizeWithRotation(1)

        pageCount = Reader.NumberOfPages()

        Dim Barcode39 As New iTextSharp.text.pdf.Barcode39
        With Barcode39
            .Code = UCase(sBarcodeText)
            .StartStopText = True
            .GenerateChecksum = False
            .Extended = True
            .X = 1
            .Size = 10
            .BarHeight = 15 * .X
        End With

        Dim iBarColor As iTextSharp.text.Color = iTextSharp.text.Color.BLACK
        Dim iTextColor As iTextSharp.text.Color =
iTextSharp.text.Color.BLACK

        For i As Integer = 1 To pageCount
            overContent = stamper.GetOverContent(i)

            Dim BarcodeImage As iTextSharp.text.Image =
Barcode39.CreateImageWithBarcode(overContent, iBarColor, iTextColor)

            Dim iSpacing As Integer = 5
            Dim BarcodePosX As Single = rect.Width - BarcodeImage.Width -
iSpacing - 30 'rect.Width / 2 - ImageBC39.Width / 2
            Dim BarcodePosY As Single = 0 'ImageBC39.Height / 2 'rect.Height
/ 2 - ImageBC39.Height / 2

            BarcodeImage.SetAbsolutePosition(BarcodePosX, BarcodePosY)

            Dim BarcodeRect As New iTextSharp.text.Rectangle(BarcodePosX -
iSpacing, BarcodePosY - iSpacing, BarcodePosX + BarcodeImage.Width +
iSpacing, BarcodePosY + BarcodeImage.Height + iSpacing)
            BarcodeRect.BackgroundColor = iTextSharp.text.Color.WHITE
            BarcodeRect.BorderColor = iTextSharp.text.Color.RED
            BarcodeRect.BorderWidth = 1
            BarcodeRect.Border = 0 '1=Top, 2=Bottom, 4=Left, 8=Right, 15=All

            With overContent
                .SaveState()
                .Rectangle(BarcodeRect)
                .AddImage(BarcodeImage)
                .RestoreState()
            End With
        Next
        stamper.FormFlattening = True
        ''stamper.SetFullCompression() ' <----- UNCOMMENT THIS LINE
        stamper.Close()
        Reader.Close()

        Dim output As New System.IO.FileStream(sOutputFileName,
IO.FileMode.Create)
        output.Write(ms2.GetBuffer(), 0, ms2.GetBuffer.Length)
        output.Flush()
        output.Close()
        output.Dispose()

        ms2.Close()
        ms2.Dispose()
    End Sub
[/code]
--
View this message in context: 
http://itext-general.2136553.n4.nabble.com/SetFullCompression-results-in-unreadable-PDF-tp2279435p2279435.html
Sent from the iText - General mailing list archive at Nabble.com.

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

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.


------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to