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

Combine Pdf documents
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1798
Page 1 of 1

Author:  jomolungma [ Thu Sep 29, 2011 12:17 pm ]
Post subject:  Combine Pdf documents

Hello,
in my software , i create pdf documents with pdf sharp.
I want to combine pdf documents with out reading by file.

for example:

Code:
public void AddPagesToMainDoc()
        {
            foreach (PdfPage page in actdoc.Pages)
            {
                maindoc.AddPage(page);
            }
           
           
        }// AddPagesToMainDoc


actdoc is an document which is created in my software.
maindoc too.
The user has the possibility different views to export in an pdf.
so the actdoc pdf has one view.
I would merge the actdoc pages to the main document.

But i get an exception:A PDF document must be opened with PdfDocumentOpenMode.Import to import pages from it.

how can i solve the problem??

Thanks bevore!

Author:  Thomas Hoevel [ Thu Sep 29, 2011 12:38 pm ]
Post subject:  Re: Combine Pdf documents

Save to a memory stream, then read from that memory stream in Import mode.

Author:  jomolungma [ Thu Sep 29, 2011 1:10 pm ]
Post subject:  Re: Combine Pdf documents

i dont read from file.
I create these pdf's!!!
so i have no streams.
i only have 2 object of class PdfDocument.

Author:  Thomas Hoevel [ Thu Sep 29, 2011 1:18 pm ]
Post subject:  Re: Combine Pdf documents

Save the newly created PDF to a memory stream, then read from that memory stream in Import mode.

Author:  jomolungma [ Thu Sep 29, 2011 5:08 pm ]
Post subject:  Re: Combine Pdf documents

Have you Sample Code for this?
Thanks a lot

Author:  jomolungma [ Fri Sep 30, 2011 4:28 am ]
Post subject:  Re: Combine Pdf documents

I think i Must discribe Bit more Detail.
The maindoc Object is also a own created Document.
There some pages in there. The user has the possibillity to add pages from the actdoc to the maindoc.the two Objects are in the Same class. So Why Must i opend the actdoc object?
I hope the Problem is better discibed.

Author:  masodo [ Fri Sep 30, 2011 5:12 pm ]
Post subject:  Re: Combine Pdf documents

jomolungma,

Not sure how much help this will be but I did a lot of hunting before I finally figured out how to get my pdf combiner to work.

Now the way I do it is to start with a complete PDF that contains every possible page that could be included and have a check-box selector field to let users pick from a menu of pages to include. In this code behind I force all documents to use pages 1 through 3 as cover pages and page 4 (of "Main.pdf") is always the last page. After that, the pages are determined by which check-boxes are checked on the aspx page.

Not clear on how this relates to your project but I hope you might find something you can use.

Code:
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////                                         #//////////
////////////////////////////                 PDFmill By              #//////////
////////////////////////////                                         #//////////
////////////////////////////                 - masodo -              #//////////
////////////////////////////                                         #//////////
////////////////////////////             All Rights Reserved         #//////////
////////////////////////////                                         #//////////
//////////////////////////////########################################//////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PdfSharp;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Drawing;
using PdfSharp.Pdf.Advanced;
using PdfSharp.Pdf.Security;



namespace PDFmill
{
            public partial class _Default : System.Web.UI.Page
        {

                protected void Page_Load(object sender, System.EventArgs e)
                {
                    if (IsPostBack)
               
                {   
                Variant1();
                  }
                }
           
            string[] GetSets()
            {
                ArrayList list = new ArrayList();
                {
                    // cover pages set <$$$>
                    list.Add("1,3");

                    for (int i = 0; i < Check1.Items.Count; i++)
                    {
                        if (Check1.Items[i].Selected)
                        {
                            String s = Check1.Items[i].Value;

                            list.Add(s);
                        }
                    }

                    // back pages set <$$$>
                    list.Add("4,4");
                }

                return (string[])list.ToArray(typeof(string));

            }

            protected void Variant1()
            {


                string filename = Server.MapPath("Main.pdf");

                string[] files = GetSets();

                PdfDocument outputDocument = new PdfDocument();

                outputDocument.Info.Title = "My Custom Document";
                outputDocument.Info.Author = "WhoDunnit";


                foreach (string file in files)
                {
                    string[] rNumb = file.Split(',');
                    string x = rNumb[0];
                    string y = rNumb[1];
                    int s = Convert.ToInt16(x);
                    int e = Convert.ToInt16(y);


                    PdfDocument inputDocument = PdfReader.Open(filename, PdfDocumentOpenMode.Import);

                    for (int idx = s - 1; idx < e; idx++)
                    {

                        PdfPage page = inputDocument.Pages[idx];

                        outputDocument.AddPage(page);
                    }

                }

                string savefilename = "GenericFilename";

                MemoryStream stream = new MemoryStream();
                outputDocument.Save(stream, false);
                Response.Clear();
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Length", stream.Length.ToString());
                Response.AddHeader("Content-Disposition", "attachment;filename=" + savefilename + ".pdf");
                Response.BinaryWrite(stream.ToArray());
                Response.Flush();
                Response.End();
                stream.Close();
             
            }
               
}
    }


:D

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