Multipage PDF from several images.

Mar 25, 2011 at 6:38 PM

I have three images and I'm successfully able to write it to one page of the PDF document. I'm very new and I want the PDF document to be multipage. Is there any option? please help me.. *-*

I'm using Imagetools to convert my stackpanel to image:

Here's my code:

       Dim d As New SaveFileDialog()
        d.Filter = "PDF file format|*.pdf"

        ' Save the document...
        If d.ShowDialog() = True Then
            stkMain.Children.Clear()
            stkMain.Children.Add(BillPage1)

            Dim document As New PdfDocument()
            Dim page As PdfPage = document.AddPage
            Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
            Dim img As ImageTools.ExtendedImage = stkMain.ToImage
            Dim mstream As New MemoryStream()
            Dim encoder As New JpegEncoder()
            encoder.Encode(img, mstream)
            mstream.Seek(0, SeekOrigin.Begin)
            Dim pdfImg As XImage = XImage.FromStream(mstream)
            gfx.DrawImage(pdfImg, 0, 0)
            stkMain.Children.Clear()
            stkMain.Children.Add(BillPage2)
'////////////////////////////////////////////////////////////////

            Dim page1 As PdfPage = document.AddPage
            Dim gfx1 As XGraphics = XGraphics.FromPdfPage(page1)
            Dim img1 As ImageTools.ExtendedImage = stkMain.ToImage
            Dim mstream1 As New MemoryStream()
            Dim encoder1 As New JpegEncoder()
            encoder1.Encode(img1, mstream1)
            mstream1.Seek(0, SeekOrigin.Begin)
            Dim pdfImg1 As XImage = XImage.FromStream(mstream1)
            gfx1.DrawImage(pdfImg1, 0, 0)


            document.Save(d.OpenFile())
        End If

the portion above '/////////////// is working if used without the second part, but using it with the second part overlaps my PDF pages created.

I'm sorry if I'm not clear. Please help.

Jan 19, 2012 at 1:57 PM

Use PdfReader.Open to open the pdf file and returns a PdfDocument object, thus can use AddPage method

Note: you need to specify PdfDocument.Version before saving, otherwise PdfReader.Open will throw an excepction as per this line:

  if (document.version == 0)
          throw new InvalidOperationException(PSSR.InvalidPdf);