PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 11:13 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Mon Jul 03, 2017 12:12 pm 
Offline

Joined: Tue Jan 07, 2014 7:35 pm
Posts: 12
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?


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 03, 2017 12:42 pm 
Offline

Joined: Tue Jan 07, 2014 7:35 pm
Posts: 12
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.


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 03, 2017 1:41 pm 
Offline

Joined: Tue Jan 07, 2014 7:35 pm
Posts: 12
I found my error. I failed to change the document names to match one another properly.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 147 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group