PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jul 14, 2024 5:44 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Combine Pdf documents
PostPosted: Thu Sep 29, 2011 12:17 pm 
Offline

Joined: Thu Sep 29, 2011 11:37 am
Posts: 8
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!


Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 29, 2011 12:38 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
Save to a memory stream, then read from that memory stream in Import mode.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 29, 2011 1:10 pm 
Offline

Joined: Thu Sep 29, 2011 11:37 am
Posts: 8
i dont read from file.
I create these pdf's!!!
so i have no streams.
i only have 2 object of class PdfDocument.


Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 29, 2011 1:18 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
Save the newly created PDF to a memory stream, then read from that memory stream in Import mode.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 29, 2011 5:08 pm 
Offline

Joined: Thu Sep 29, 2011 11:37 am
Posts: 8
Have you Sample Code for this?
Thanks a lot


Top
 Profile  
Reply with quote  
PostPosted: Fri Sep 30, 2011 4:28 am 
Offline

Joined: Thu Sep 29, 2011 11:37 am
Posts: 8
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.


Top
 Profile  
Reply with quote  
PostPosted: Fri Sep 30, 2011 5:12 pm 
Offline
User avatar

Joined: Fri Sep 30, 2011 4:48 pm
Posts: 1
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


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 37 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