How to copy pages from one PDF file to another PDF in the end

I have PDF file saved (say source.pdf). I am creating a new PDF(to be saved as destination.pdf) from scratch in which i have say added some content. Now i want to append the content of the file source.ppt to destination.ppt. but starting from a new page.


i did this in PPT as
private bool AddSlidesFromPPTFile(Presentation destPres, string sourcePPTFileName)
{
string fileLocation = ConfigurationSettings.AppSettings[“FilePath”];
string filePath = string.Format("{0}{1}", fileLocation,
sourcePPTFileName );

try
{
Presentation sourcePres = new Presentation(filePath);
for (int i = 0; i < sourcePres.Slides.Count; i++)
{
Slide slide = sourcePres.Slides[i];
SortedList sList = new SortedList();
sourcePres.CloneSlide(slide, destPres.Slides.LastSlidePosition + 1, destPres, sList);
}
return true;
}
catch
{
return false;
}
}


How can i do this in PDF.

Hi Vibhor,

Thank you for considering Aspose.Pdf.

Well, you can use Aspose.Pdf DOM to achieve your desired results. You can get the page(s) from a PDF file using Document.Pages collection and add the page to a PDF file using Page.Add method. Please see the following documentation link for details and sample code.

Get a Particular Page of the PDF File

In case you need any further assistance, please feel free to contact support.

Thank you & Best Regards,

Hi Aslam,

I couldnt find this class Document in Aspose.Pdf namespace.

Also i saw that the corresponding page for this class in your online documentation has been added recently (on May 18, 2012).

http://www.aspose.com/docs/display/pdfnet/Document+Class

So it might be possible that this class is present in some newer version.

I am currently using version 4.7.

If i am correct, which version onwards can i find this class in the API.

Hi Vibhor,

Well, you are using a very old version of Aspose.Pdf for .NET, the document class was introduced in version 6.0 after merging of Aspose.Pdf for.NET and Aspose.Pdf.Kit for .NET products. We would suggest you to download and try the latest version of Aspose.Pdf for .NET v7.4.

You can also check the following documentation link regarding how to upgrade your code to be used with Aspose.Pdf for .NET v6.0 and later versions. Migration from earlier versions of Aspose.Pdf for .NET

Please do let us know in case you need any further assistance.

Thank You & Best Regards,

Hi Aslam,


Thanx for the information Aslam. So can we do this is any way using any of the latest Aspose API.
I tried the evaluation version of the PDF 7.4. But i couldnt achieve what i wanted to.

I took a destination PDF (from scratch) and added some text into it.
I took another PDF, source PDF (already saved with 3 pages) and appended these three pages to the destination PDF.

Pdf destnPDF = new Pdf();
Document destnDoc = new Document(destnPDF);
//add some text to this pdf
//create a sourcePDF by reading a saved file,
//add its pages to the destination pdf.
//save destination pdf - either using Pdf or Document

Problem -
When i save the destination PDF using PDF class i got the text but not the three slides.
When i save the destination using the Document class, i got the three slides but not the text.

What i want to do is -
1. Theres a destination PDF which i am creating from scratch by adding some sections and text to it programatically using Aspose. It is not yet saved to disk as a file. The instance is in memory.
2. I pick up a source PDF, already created & saved to disk.
3. I put all the content of source to destination but it should start appending from destination’s next page onwards(like we have page break function in MS Word).
4.i continue adding stuff to the destination PDF.
5. lastly i save the destination PDF to disk.

Its like while creating a PDF(destination, from scratch) i am appending some content to it from a different saved PDF file.
How can i achieve this? I earlier thought that it is possible with Document.Pages.Add() method but i have already mentioned the problem in that case. Please help.

Hi Vibhor,

Thank you for the feedback.

Following is the same code I used to create a new file with some text and then append the pages of another PDF file to the new PDF file and save it. I did not face any issue using the below mentioned code as per your scenario. Please try the below mentioned code with your template PDF file and in case you still face any issue, please share your template PDF file and sample code with us, we will check it and get back to you soon.

//Create pdf document
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();

//Add a section into the pdf document
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();

//Add a text paragraph into the section
sec1.Paragraphs.Add(new Aspose.Pdf.Generator.Text("Hello World"));

//Open PDF file in Document Object
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(pdf1);

//Open PDF file in Document Object using file path
Aspose.Pdf.Document doc2 = new Aspose.Pdf.Document("D:\\Test.pdf");

//Add Page to new PDF file
doc.Pages.Add(doc2.Pages);

//Save PDF file
doc.Save("D:\\Test_hello.pdf");

Sorry for the inconvenience,

Hi Aslam,


I tried the code which you provided. It worked but still it has a problem.
I cudn’t get to work the 4th point continue adding stuff to the destination PDF.(refer prev. post)

I created a presentation added a text into into it, added 2 pages from another PDF, again added some text into it.

Here’s my code

Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();

//Add first text into the pdf document
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
sec1.Paragraphs.Add(new Aspose.Pdf.Generator.Text(“Hello World”));

//Open PDF file in Document Object
Aspose.Pdf.Document doc1 = new Aspose.Pdf.Document(pdf1);

//Open PDF file with 2 pages in Document Object using file path
Aspose.Pdf.Document doc2 = new Aspose.Pdf.Document(@“C:\Users\vibhor.agarwal\Desktop\pdf2.pdf”);

//Add Page to new PDF file
doc1.Pages.Add(doc2.Pages);

//Add second text to the PDF
Aspose.Pdf.Generator.Section sec2 = pdf1.Sections.Add();
sec2.Paragraphs.Add(new Aspose.Pdf.Generator.Text(“Hello World 2”));

//Save PDF file
doc1.Save(@“C:\Users\vibhor.agarwal\Desktop\pdf1_1.pdf”);
pdf1.Save(@“C:\Users\vibhor.agarwal\Desktop\pdf1_2.pdf”);

What i expected was a PDF with 4 pages, page 1 with first text, then the two imported pages, then the second text at the 4th page.

When i saved this using Document class i got only 3 pages, didnt get the second text.
When i saved it usind Pdf class i got only 2 pages, containing the two texts, but no copied pages from other pdf.

I am attaching the following files -
pdf2 - the file whose pages are to be appended to pdf1.pdf.
pdf1_1 - file saved with Document class.
pdf1_2 - file saved with Pdf class

Hi Vibhor,

Well, your code is not correct as per your scenario. In your first approach, the problem is because when you add the second section, which is not added back to the Document object, that is why you are unable to see the last page. Regarding the second approach, Pdf Object only has two sections added to it, so it will show only those in the output.

Now, the better approach as per your scenario will be to use only the Document object to insert a new page, add contents, add existing pages of another PDF, and then insert another page with contents. Please see the following sample code and let us know if it works as per your requirement.

//open document
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();

//create TextFragment object
TextFragment textFragment = new TextFragment("main text");

//set location for the fragment
textFragment.Position = new Position(100, 600);

//create TextBuilder object
TextBuilder textBuilder = new TextBuilder(page);

//append text fragment to the PDF page
textBuilder.AppendText(textFragment);

//open document
Aspose.Pdf.Document doc2 = new Aspose.Pdf.Document(@"D:\AP Data\October2012\Test.pdf");

//add pages of doc2 to pdfDocument
pdfDocument.Pages.Add(doc2.Pages);

//insert a new page at the end
page = pdfDocument.Pages.Insert(pdfDocument.Pages.Count + 1);

//create text fragment
textFragment = new TextFragment("main text 2");

//set location for the fragment
textFragment.Position = new Position(100, 400);

//create TextBuilder object
textBuilder = new TextBuilder(page);

//append text fragment to the PDF page
textBuilder.AppendText(textFragment);

//save document
pdfDocument.Save(@"D:\AP Data\October2012\Test_Pdf.pdf");

Or another approach can be to open the existing PDF document from which you want to copy the pages, insert the pages to it (in start and end), and save it as a new PDF file. Please see the following code in this regard.

//open document
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(@"D:\AP Data\October2012\Test.pdf");

//create and insert a new page at the start
Page page = pdfDocument.Pages.Insert(1);

//create text fragment
TextFragment textFragment = new TextFragment("main text");

//set location for the fragment
textFragment.Position = new Position(100, 600);

//create TextBuilder object
TextBuilder textBuilder = new TextBuilder(page);

//append text fragment to the PDF page
textBuilder.AppendText(textFragment);

//create and insert a new page at the end
page = pdfDocument.Pages.Insert(pdfDocument.Pages.Count + 1);

//create text fragment
textFragment = new TextFragment("main text 2");

//set location for the fragment
textFragment.Position = new Position(100, 400);

//create TextBuilder object
textBuilder = new TextBuilder(page);

//append text fragment to the PDF page
textBuilder.AppendText(textFragment);

//save document
pdfDocument.Save(@"D:\AP Data\October2012\Test_Pdf.pdf");

Please feel free to contact support in case you need any further assistance.

Sorry for the inconvenience,