Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Multiple page PDF

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
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Experts please ..its really urgent.

Thanks in advance
Avatar of Chinmay Patel
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.
Avatar of RIAS

ASKER

Hi ,
Thanks. Here I am not clubing Pdf already existing but generating pdf from system.byte .
Any suggestion
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?
Avatar of RIAS

ASKER

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
Avatar of RIAS

ASKER

Thanks experts ...but still looking for options like PDFSharp is the suitable solution for creating PDF from memory stream in system.bytes.
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.
Avatar of RIAS

ASKER

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
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.
Avatar of RIAS

ASKER

Chinmay,
Thanks.But byte does not specify where the page end. System.Byte is in array format.
Any suggestion on code?
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.
Avatar of RIAS

ASKER

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 :)
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.