views:

19

answers:

1

Hi,

I am trying to generate a PDF using iTextSharp.
It will consists of a number of images, each with a heading preceding it. But when I generate the PDF, the order of the elements is not preserved - multiple headings are grouped together etc.

I am wrapping the header and image in a single paragraph as follows:

' Create paragraph and heading
Dim paragraph As New iTextSharp.text.Paragraph()
Dim heading As New iTextSharp.text.Chunk("Image title" & vbNewLine, pdfHeadingFont)

' Create image from Chart
Dim image = GetPdfImage(Me.chtMain)
Dim width = iTextSharp.text.PageSize.A4.Width - pdfDocument.LeftMargin - pdfDocument.RightMargin
Dim height = iTextSharp.text.PageSize.A4.Height - pdfDocument.TopMargin - pdfDocument.BottomMargin
image.Alignment = image.ALIGN_CENTER Or image.TEXTWRAP
image.ScaleToFit(width, height)

' Add heading and image to paragraph
paragraph.Add(heading)
paragraph.Add(image)

' Add paragraph to document
pdfDocument.Add(paragraph)

Why are the image and heading not placed together in the PDF? Could I be doing this in some other way?

Thank you,

Martin

A: 

Figured it out, thanks to this question.

Apparently, setting PdfWriter.StrictImageSequence = true resolves this issue.
iTextSharp "optimizes" your document by trying to fit as many paragraphs as possible on every single page - regardless of order.

Martin

Martin Wiboe