PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Combine new PDF in memory with saved file
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3617
Page 1 of 1

Author:  RandyHJ57 [ Mon Jul 03, 2017 12:12 pm ]
Post subject:  Combine new PDF in memory with saved file

I am working with the Combine PDFs sample. The sample uses two saved PDF files. I am wanting to do something different.

I have one PDF file saved as a resource in my Project in Visual Studio (My.Resources.pslf_ecf). I access it with the following code:
Code:
Dim formSrc As Byte() = My.Resources.pslf_ecf
        Dim pdfStream As MemoryStream = New MemoryStream(formSrc)
        Dim newForm As PdfDocument = PdfReader.Open(pdfStream, PdfDocumentOpenMode.Import)


This opens successfully.

I am also creating a PDF document in memory (Dim document As PdfDocument) and I want to combine it with my resource file (My.Resources.plsf_ecf).

Do I need to save the new document I am creating in memory first before I combine it with my Resource file? Or can I combine the files without saving the new file first? If I can do that, how would I do it?

Author:  RandyHJ57 [ Mon Jul 03, 2017 12:42 pm ]
Post subject:  Re: Combine new PDF in memory with saved file

I did some more searching and found some code on this forum that seems to partially work. Here is the code I have now.
Code:
 ' Create a PDF document, not a MigraDoc document
        Dim document As PdfDocument = New PdfDocument()
        document.Info.Title = "ECF Manual Denial Letter"
        document.Info.Author = "Randy Johnson"
        document.Info.Subject = "Creating denial letter for expired form submission"

        'LetterPage1(document, Data)
        LetterPage1(document)
        LetterPage2(document)

        Dim newLetter As MemoryStream = New MemoryStream()
        document.Save(newLetter, False)
        'Dim ltrSrc As Byte() = newLetter.ToArray()
        Dim ltrForm As PdfDocument = PdfReader.Open(newLetter, PdfDocumentOpenMode.Import)

        Dim formSrc As Byte() = My.Resources.pslf_ecf
        Dim pdfStream As MemoryStream = New MemoryStream(formSrc)
        Dim newForm As PdfDocument = PdfReader.Open(pdfStream, PdfDocumentOpenMode.Import)

        Dim outputDocument As PdfDocument = New PdfDocument()
        outputDocument.PageLayout = PdfPageLayout.SinglePage

        Dim pages As Integer = Math.Max(document.PageCount, newForm.PageCount)

        For idx As Integer = 0 To pages - 1

            Dim page1 As PdfPage = If(ltrForm.PageCount > idx, document.Pages(idx), New PdfPage())
            Dim page2 As PdfPage = If(ltrForm.PageCount > idx, document.Pages(idx), New PdfPage())
            Dim page3 As PdfPage = If(newForm.PageCount > idx, newForm.Pages(idx), New PdfPage())
            Dim page4 As PdfPage = If(newForm.PageCount > idx, newForm.Pages(idx), New PdfPage())
            Dim page5 As PdfPage = If(newForm.PageCount > idx, newForm.Pages(idx), New PdfPage())
            Dim page6 As PdfPage = If(newForm.PageCount > idx, newForm.Pages(idx), New PdfPage())
            Dim page7 As PdfPage = If(newForm.PageCount > idx, newForm.Pages(idx), New PdfPage())
            Dim page8 As PdfPage = If(newForm.PageCount > idx, newForm.Pages(idx), New PdfPage())

           :evil:  :shock:  :mrgreen:   page1 = outputDocument.AddPage(page1)
            page2 = outputDocument.AddPage(page2)
            page3 = outputDocument.AddPage(page3)
            page4 = outputDocument.AddPage(page4)
            page5 = outputDocument.AddPage(page5)
            page6 = outputDocument.AddPage(page6)
            page7 = outputDocument.AddPage(page7)
            page8 = outputDocument.AddPage(page8)

        Next

        outputDocument.Save(outputFilename)
        Process.Start(outputFilename)


Everything works up until the line of code with the emoticons, when I try to add the newly created pages to the outputDocument. I get an Exception that a "A PDF document must be opened with PdfDocumentOpenMode.Import to import pages from it."

I based this code on the samples in the source code and it is reflected in these lines:
Code:
Dim newLetter As MemoryStream = New MemoryStream()
        document.Save(newLetter, False)
        'Dim ltrSrc As Byte() = newLetter.ToArray()
        Dim ltrForm As PdfDocument = PdfReader.Open(newLetter, PdfDocumentOpenMode.Import)

        Dim formSrc As Byte() = My.Resources.pslf_ecf
        Dim pdfStream As MemoryStream = New MemoryStream(formSrc)
        Dim newForm As PdfDocument = PdfReader.Open(pdfStream, PdfDocumentOpenMode.Import)


It appears to me that both documents are opened in Import mode. I do not understand the reason for the Exception.

Author:  RandyHJ57 [ Mon Jul 03, 2017 1:41 pm ]
Post subject:  Re: Combine new PDF in memory with saved file

I found my error. I failed to change the document names to match one another properly.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/