How to concatenate multiple MigraDoc documents into one










0















There is sample code on how to add multiple Pdf documents into one in the docs. But I want to combine multiple MigraDoc documents into one.



The code I have so far is:



 private void GeneratePdfDocument(IEnumerable<Document> parts, string fileName)

using (var outputDocument = new PdfDocument())

foreach (var part in parts)

var renderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);

renderer.Document = part;

renderer.RenderDocument();

var pdfPart = renderer.PdfDocument;

for (var pageIndex = 0; pageIndex < pdfPart.PageCount; pageIndex++)

outputDocument.AddPage(pdfPart.Pages[pageIndex]);



// create the PDF
outputDocument.Save(fileName);




But on AddPage, I get an System.InvalidOperationException:



A PDF document must be opened with PdfDocumentOpenMode.Import to import pages from it.


A solution would be to create each Document part as separate PDF, then combine all of them into a single PDF file, but not all parts will require a whole page.



EDIT:
I tried also the following approach:



 var combineDocument = new Document();
foreach (var part in parts)

//for(var styleIndex = 0; styleIndex < part.Styles.Count; styleIndex++)
//
// combineDocument.Add(part.Styles[styleIndex]);
//

for(var sectionIndex = 0; sectionIndex < part.Sections.Count; sectionIndex++)

var section = part.Sections[sectionIndex].Clone();
combineDocument.Add(section);




The idea was to copy the sections of each Document into the combineDocument instance, but I was not able to retrieve the Style instances and the result was not as expected.



Question:
Is it possible to combine MigraDoc Document instances into one document?










share|improve this question




























    0















    There is sample code on how to add multiple Pdf documents into one in the docs. But I want to combine multiple MigraDoc documents into one.



    The code I have so far is:



     private void GeneratePdfDocument(IEnumerable<Document> parts, string fileName)

    using (var outputDocument = new PdfDocument())

    foreach (var part in parts)

    var renderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);

    renderer.Document = part;

    renderer.RenderDocument();

    var pdfPart = renderer.PdfDocument;

    for (var pageIndex = 0; pageIndex < pdfPart.PageCount; pageIndex++)

    outputDocument.AddPage(pdfPart.Pages[pageIndex]);



    // create the PDF
    outputDocument.Save(fileName);




    But on AddPage, I get an System.InvalidOperationException:



    A PDF document must be opened with PdfDocumentOpenMode.Import to import pages from it.


    A solution would be to create each Document part as separate PDF, then combine all of them into a single PDF file, but not all parts will require a whole page.



    EDIT:
    I tried also the following approach:



     var combineDocument = new Document();
    foreach (var part in parts)

    //for(var styleIndex = 0; styleIndex < part.Styles.Count; styleIndex++)
    //
    // combineDocument.Add(part.Styles[styleIndex]);
    //

    for(var sectionIndex = 0; sectionIndex < part.Sections.Count; sectionIndex++)

    var section = part.Sections[sectionIndex].Clone();
    combineDocument.Add(section);




    The idea was to copy the sections of each Document into the combineDocument instance, but I was not able to retrieve the Style instances and the result was not as expected.



    Question:
    Is it possible to combine MigraDoc Document instances into one document?










    share|improve this question


























      0












      0








      0








      There is sample code on how to add multiple Pdf documents into one in the docs. But I want to combine multiple MigraDoc documents into one.



      The code I have so far is:



       private void GeneratePdfDocument(IEnumerable<Document> parts, string fileName)

      using (var outputDocument = new PdfDocument())

      foreach (var part in parts)

      var renderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);

      renderer.Document = part;

      renderer.RenderDocument();

      var pdfPart = renderer.PdfDocument;

      for (var pageIndex = 0; pageIndex < pdfPart.PageCount; pageIndex++)

      outputDocument.AddPage(pdfPart.Pages[pageIndex]);



      // create the PDF
      outputDocument.Save(fileName);




      But on AddPage, I get an System.InvalidOperationException:



      A PDF document must be opened with PdfDocumentOpenMode.Import to import pages from it.


      A solution would be to create each Document part as separate PDF, then combine all of them into a single PDF file, but not all parts will require a whole page.



      EDIT:
      I tried also the following approach:



       var combineDocument = new Document();
      foreach (var part in parts)

      //for(var styleIndex = 0; styleIndex < part.Styles.Count; styleIndex++)
      //
      // combineDocument.Add(part.Styles[styleIndex]);
      //

      for(var sectionIndex = 0; sectionIndex < part.Sections.Count; sectionIndex++)

      var section = part.Sections[sectionIndex].Clone();
      combineDocument.Add(section);




      The idea was to copy the sections of each Document into the combineDocument instance, but I was not able to retrieve the Style instances and the result was not as expected.



      Question:
      Is it possible to combine MigraDoc Document instances into one document?










      share|improve this question
















      There is sample code on how to add multiple Pdf documents into one in the docs. But I want to combine multiple MigraDoc documents into one.



      The code I have so far is:



       private void GeneratePdfDocument(IEnumerable<Document> parts, string fileName)

      using (var outputDocument = new PdfDocument())

      foreach (var part in parts)

      var renderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);

      renderer.Document = part;

      renderer.RenderDocument();

      var pdfPart = renderer.PdfDocument;

      for (var pageIndex = 0; pageIndex < pdfPart.PageCount; pageIndex++)

      outputDocument.AddPage(pdfPart.Pages[pageIndex]);



      // create the PDF
      outputDocument.Save(fileName);




      But on AddPage, I get an System.InvalidOperationException:



      A PDF document must be opened with PdfDocumentOpenMode.Import to import pages from it.


      A solution would be to create each Document part as separate PDF, then combine all of them into a single PDF file, but not all parts will require a whole page.



      EDIT:
      I tried also the following approach:



       var combineDocument = new Document();
      foreach (var part in parts)

      //for(var styleIndex = 0; styleIndex < part.Styles.Count; styleIndex++)
      //
      // combineDocument.Add(part.Styles[styleIndex]);
      //

      for(var sectionIndex = 0; sectionIndex < part.Sections.Count; sectionIndex++)

      var section = part.Sections[sectionIndex].Clone();
      combineDocument.Add(section);




      The idea was to copy the sections of each Document into the combineDocument instance, but I was not able to retrieve the Style instances and the result was not as expected.



      Question:
      Is it possible to combine MigraDoc Document instances into one document?







      c# pdfsharp migradoc






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 8:12







      gpinkas

















      asked Nov 15 '18 at 16:44









      gpinkasgpinkas

      1,68921841




      1,68921841






















          1 Answer
          1






          active

          oldest

          votes


















          1














          To transfer elements of one MigraDoc Document into another, try the Clone() method of the element you want to transfer. This can also be used to re-use elements within the same document.

          To do this, you have to iterate through all the document elements you want to copy and call Clone() for each and call Add for the receiving Document passing the cloned object as a parameter.



          To work around the PDFsharp exception shown in the original question you could save the PdfDocument into a MemoryStream and open it again using PdfDocumentOpenMode.Import.



          The solution I would prefer: Write methods that create MigraDoc Documents or their parts. Call those methods twice - once to create one big document, once to create several small documents. Thus you avoid cloning and saving/reading.






          share|improve this answer

























          • I'm already preparing the preferred solution, so each part is created as a MigraDoc Document separately, but the question is: How can I create a single ´PDF´ out of them?

            – gpinkas
            Nov 16 '18 at 8:06











          • Do not create each part as a separate Document, instead write routines that get a Document as a parameter and add their content. Then you can easily get separate documents with one aspect per document, or one big document that contains all aspects (maybe with a table of contents). - The other option is iterating through the small documents you have and call Clone() to add each element to another document.

            – Vive la déraison
            Nov 16 '18 at 13:43










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324157%2fhow-to-concatenate-multiple-migradoc-documents-into-one%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          To transfer elements of one MigraDoc Document into another, try the Clone() method of the element you want to transfer. This can also be used to re-use elements within the same document.

          To do this, you have to iterate through all the document elements you want to copy and call Clone() for each and call Add for the receiving Document passing the cloned object as a parameter.



          To work around the PDFsharp exception shown in the original question you could save the PdfDocument into a MemoryStream and open it again using PdfDocumentOpenMode.Import.



          The solution I would prefer: Write methods that create MigraDoc Documents or their parts. Call those methods twice - once to create one big document, once to create several small documents. Thus you avoid cloning and saving/reading.






          share|improve this answer

























          • I'm already preparing the preferred solution, so each part is created as a MigraDoc Document separately, but the question is: How can I create a single ´PDF´ out of them?

            – gpinkas
            Nov 16 '18 at 8:06











          • Do not create each part as a separate Document, instead write routines that get a Document as a parameter and add their content. Then you can easily get separate documents with one aspect per document, or one big document that contains all aspects (maybe with a table of contents). - The other option is iterating through the small documents you have and call Clone() to add each element to another document.

            – Vive la déraison
            Nov 16 '18 at 13:43















          1














          To transfer elements of one MigraDoc Document into another, try the Clone() method of the element you want to transfer. This can also be used to re-use elements within the same document.

          To do this, you have to iterate through all the document elements you want to copy and call Clone() for each and call Add for the receiving Document passing the cloned object as a parameter.



          To work around the PDFsharp exception shown in the original question you could save the PdfDocument into a MemoryStream and open it again using PdfDocumentOpenMode.Import.



          The solution I would prefer: Write methods that create MigraDoc Documents or their parts. Call those methods twice - once to create one big document, once to create several small documents. Thus you avoid cloning and saving/reading.






          share|improve this answer

























          • I'm already preparing the preferred solution, so each part is created as a MigraDoc Document separately, but the question is: How can I create a single ´PDF´ out of them?

            – gpinkas
            Nov 16 '18 at 8:06











          • Do not create each part as a separate Document, instead write routines that get a Document as a parameter and add their content. Then you can easily get separate documents with one aspect per document, or one big document that contains all aspects (maybe with a table of contents). - The other option is iterating through the small documents you have and call Clone() to add each element to another document.

            – Vive la déraison
            Nov 16 '18 at 13:43













          1












          1








          1







          To transfer elements of one MigraDoc Document into another, try the Clone() method of the element you want to transfer. This can also be used to re-use elements within the same document.

          To do this, you have to iterate through all the document elements you want to copy and call Clone() for each and call Add for the receiving Document passing the cloned object as a parameter.



          To work around the PDFsharp exception shown in the original question you could save the PdfDocument into a MemoryStream and open it again using PdfDocumentOpenMode.Import.



          The solution I would prefer: Write methods that create MigraDoc Documents or their parts. Call those methods twice - once to create one big document, once to create several small documents. Thus you avoid cloning and saving/reading.






          share|improve this answer















          To transfer elements of one MigraDoc Document into another, try the Clone() method of the element you want to transfer. This can also be used to re-use elements within the same document.

          To do this, you have to iterate through all the document elements you want to copy and call Clone() for each and call Add for the receiving Document passing the cloned object as a parameter.



          To work around the PDFsharp exception shown in the original question you could save the PdfDocument into a MemoryStream and open it again using PdfDocumentOpenMode.Import.



          The solution I would prefer: Write methods that create MigraDoc Documents or their parts. Call those methods twice - once to create one big document, once to create several small documents. Thus you avoid cloning and saving/reading.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 12:21









          gpinkas

          1,68921841




          1,68921841










          answered Nov 15 '18 at 22:27









          Vive la déraisonVive la déraison

          15.2k251115




          15.2k251115












          • I'm already preparing the preferred solution, so each part is created as a MigraDoc Document separately, but the question is: How can I create a single ´PDF´ out of them?

            – gpinkas
            Nov 16 '18 at 8:06











          • Do not create each part as a separate Document, instead write routines that get a Document as a parameter and add their content. Then you can easily get separate documents with one aspect per document, or one big document that contains all aspects (maybe with a table of contents). - The other option is iterating through the small documents you have and call Clone() to add each element to another document.

            – Vive la déraison
            Nov 16 '18 at 13:43

















          • I'm already preparing the preferred solution, so each part is created as a MigraDoc Document separately, but the question is: How can I create a single ´PDF´ out of them?

            – gpinkas
            Nov 16 '18 at 8:06











          • Do not create each part as a separate Document, instead write routines that get a Document as a parameter and add their content. Then you can easily get separate documents with one aspect per document, or one big document that contains all aspects (maybe with a table of contents). - The other option is iterating through the small documents you have and call Clone() to add each element to another document.

            – Vive la déraison
            Nov 16 '18 at 13:43
















          I'm already preparing the preferred solution, so each part is created as a MigraDoc Document separately, but the question is: How can I create a single ´PDF´ out of them?

          – gpinkas
          Nov 16 '18 at 8:06





          I'm already preparing the preferred solution, so each part is created as a MigraDoc Document separately, but the question is: How can I create a single ´PDF´ out of them?

          – gpinkas
          Nov 16 '18 at 8:06













          Do not create each part as a separate Document, instead write routines that get a Document as a parameter and add their content. Then you can easily get separate documents with one aspect per document, or one big document that contains all aspects (maybe with a table of contents). - The other option is iterating through the small documents you have and call Clone() to add each element to another document.

          – Vive la déraison
          Nov 16 '18 at 13:43





          Do not create each part as a separate Document, instead write routines that get a Document as a parameter and add their content. Then you can easily get separate documents with one aspect per document, or one big document that contains all aspects (maybe with a table of contents). - The other option is iterating through the small documents you have and call Clone() to add each element to another document.

          – Vive la déraison
          Nov 16 '18 at 13:43



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324157%2fhow-to-concatenate-multiple-migradoc-documents-into-one%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          27

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          Category:Rhetoric