Multiple page PDF

RIAS
RIAS used Ask the Experts™
on
Hi,
How to create multiple PDF pages saved as one PDF file by PDFsharp or MergeDOC.

I have an working code creating only 1 page but not multi page.
Any suggestions are appreciated.

 Dim dtSQLImage As New DataTable
        Dim imagedt
        Dim imageData() As Byte

        Dim unicode = PdfFontEncoding.Unicode
        Dim embedding = PdfFontEncoding.Automatic

        Dim clsFrmain As New ClsFrmMainLoad

        dtSQLImage = DgFinanceImg.DataSource
        Dim pdfRenderer As PdfDocumentRenderer = New PdfDocumentRenderer(unicode, embedding)
        For j = 0 To dtSQLImage.Rows.Count - 1
            If j = DirectCast(sender, Telerik.WinControls.UI.RadGridView).CurrentRow.Index Then
                imageData = (dtSQLImage.Rows(j)(0))

                For i = 0 To 2

                    imageFilename = MigraDocFilenameFromByteArray(imageData)
                    document = New MigraDoc.DocumentObjectModel.Document
                    Dim pageSetup As MigraDoc.DocumentObjectModel.PageSetup = document.DefaultPageSetup.Clone
                    ' set orientation
                    pageSetup.Orientation = MigraDoc.DocumentObjectModel.Orientation.Landscape
                    Dim section As MigraDoc.DocumentObjectModel.Section = document.AddSection

                    Dim image = section.AddImage(imageFilename)

                    '  Image.Top = ShapePosition.Top
                    ' Image.Left = ShapePosition.Left
                    ' Image.WrapFormat.Style = WrapStyle.TopBottom
                    With section
                        .PageSetup.PageHeight = image.Height  ' ImageHeight
                        .PageSetup.PageWidth = image.Width
                        .PageSetup.TopMargin = 0
                        .PageSetup.LeftMargin = 0
                        .PageSetup.BottomMargin = 0
                        .PageSetup.RightMargin = 0
                        ' .AddImage(imageFilename)
                    End With
                    ' Associate the MigraDoc document with a renderer

                    pdfRenderer.Document = document
                    ' Layout and render document to PDF
                    pdfRenderer.RenderDocument()
                Next
            End If
        Next

        Dim filename As String = "C:\HelloWorld.pdf"
        Me.PdfView.UnloadDocument()
        pdfRenderer.PdfDocument.Save(filename)
        ' ...and start a viewer.
        Process.Start(filename)

        Me.PdfView.LoadDocument(filename)

Open in new window


Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Experts please ..its really urgent.

Thanks in advance
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Rias,

I wrote this code for a product I built in 2011(using PDFSharp and couple of other third party controls) so the code might be bit outdated - I have given this to many others but haven't heard any negative feedback - I am hoping it will help you.

Public Shared Function Merge(ByVal fileNames As String(), ByVal targetFileName As String) As Boolean
    Dim outputDocument As PdfDocument = New PdfDocument()

    For i As Integer = fileNames.Length To 0 + 1
        Dim inputDocument As PdfDocument = PdfReader.Open(fileNames(i - 1), PdfDocumentOpenMode.Import)
        outputDocument.Info.Creator = "Oblak Consulting"
        Dim count As Integer = inputDocument.PageCount

        For idx As Integer = 0 To count - 1
            Dim page As PdfPage = inputDocument.Pages(idx)
            outputDocument.AddPage(page)
        Next
    Next

    outputDocument.Save(targetFileName)
    Return True
End Function

Open in new window


This is a quick fix so you can generate the files one at a time and then call this merge method, if you can explain what you are trying to do I can try to come up with a better solution.

Regards,
Chinmay.

Author

Commented:
Hi ,
Thanks. Here I am not clubing Pdf already existing but generating pdf from system.byte .
Any suggestion
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Of course I understood that but as a quick way out I suggested that you generate multiple files with a single page using your existing logic and then on top of that using the merge method provided, you merge all the files.

Basically after pdfRenderer.PdfDocument.Save(filename), you keep track of each file that you have rendered and then call the merge method.

Is that something you can do?

Author

Commented:
Hi,
The problem with my code is only one file is generated. So dont understand how can I loop with number if files.
Can you elaborate please.
.
where in your loops do you want the page breaks ?
from my use, u have to manually create the new page and then restart the output onto that new page.
i use a line count to do this but that wont work for your images i expect

Author

Commented:
Thanks experts ...but still looking for options like PDFSharp is the suitable solution for creating PDF from memory stream in system.bytes.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi RIAS,

As you can see robr has asked a very straight forward question - where is the logic in which you are getting multiple pages from SQL?
For i = 0 to 2? is that the loop?

Also we get it that you want to create a pdf from memory stream but there is always a method of doing things. If it is not supported by PdfSharp then we have to dump it on disk and then run a merge.

If you can point out where in your code you are getting multiple images from SQL, we can suggest something.

Regards,
Chinmay.

Author

Commented:
Chinmay , Thanks ! That loop For i = 0 to 2? is that the loop? is just for testing in order to generate multiple document .But really did not do the job .
Thanks
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Ok.. but then you are generating the document outside the loop. Your Save document method is having a hardcoded file name as well so every time it will overwrite the same file. if I wanted to code this - I do not see the entire picture just yet so I have to guess - I would take the approach below:
1. Get pages from SQL -
2. For each page I will create a pdf file, save the file to the disk and keep its name in a list or an array
3. Run a merge operation using the array.

Now, if you don't want to do it by creating each file seperately

There is document.Pages.Add method that you can use, but then still you will need to know the number of pages and their content upfront to use that method.

Author

Commented:
Chinmay,
Thanks.But byte does not specify where the page end. System.Byte is in array format.
Any suggestion on code?
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi RIAS,

I think that is what robr was asking. We can get as much data as we want but if you don't know where there are logical breaks in the data are - to create separate pages then I don't think anyone can help.

Are you storing individual images in SQL in each row? If so I can try to come up with a PoC.

Regards,
Chinmay.

Author

Commented:
Hi,
Yes There are individual images with multipages saved in sql.  
I thought renderpages did that also I have found this link where it explains what to do but can't put that in code.


https://forum.pdfsharp.net/viewtopic.php?f=2&t=781

Please check this bit there:
Well, i managed to solve this. For those who might face similar issues, here's what helped me:
- used MigraDoc entirely, the Section and Renderer objects
- created table using Tables.Table and adding rows to it (as they come from db)
- adding the table to section > Section.Add(table)
- finally adding the section to the PDF document, using the Renderer

Now the table stretches across multiple pages.. Thanks indeed to PDFsharp and MigraDoc :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial