Add a pdf to a pdf object

I have a Pdf object and want to add a pdf file to it.How do i do it?

Thanks,


Vlado

Hello Vlado,

Thanks for your interest in our products.

Do you need to add/Merge/Concatenate two or more PDF documents ? If so is the case, please visit the following link for required information.

Aspose.Pdf (DOM Approach)

Aspose.Pdf.Facades ApproachOr, in case you need to add a PDF document as a watermark to an existing PDF document, please visit the following link for required information. Adding PDF Page Stamp in the PDF File or Add PDF Page Stamp on All Pages in a PDF File (Facades).

In case I have not properly understood your requirements, please share some more details. We apologize for your inconvenience.

Thank you for you quick response.
I have seen the links you mentioned but cannot find example for that what I need there:

I have a new pdf object:


Pdf pdf1 = new Pdf();


And I did insert in to it some images. Now I need to append to the end of this object an existing pdf file:


Document pdfDocument1 = new Document("input.pdf");


Something similar to this:

pdf1.Pages.Add(pdfDocument1);

of course the code above does not work.

Thanks for the advice

Vlado

Hello Vlado,

Thanks for sharing the details.

In order to accomplish your requirement, first you need to create an object of Pdf object and place image files inside it. Save the PDF document to Stream object/or save it to system. Then use PdfFileEditor to append this file at the end of an existing PDF document. Finally save the resultant file over your system. Please take a look over following code snippet which I have tested with Aspose.Pdf for .NET 6.0.0 and it seems to be working properly. In case you still face any problem or you have any further query, please feel free to contact. We apologize for your inconvenience.

[C#]

//Instantiate a Pdf object by calling its empty constructor
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
//Create a section in the Pdf object
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
//Create an image object in the section
Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(sec1);
//Add image object into the Paragraphs collection of the section
sec1.Paragraphs.Add(image1);
//Set the path of image file
image1.ImageInfo.File = "d:/pdftest/aspose-logo.png";
//Set the type of image using ImageFileType enumeration
image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Png;
// create memory Stream object to hold resultant PDF
MemoryStream MyStream = new MemoryStream();
//Save the Pdf
pdf1.Save(MyStream);

//create PdfFileEditor object
PdfFileEditor pdfEditor = new PdfFileEditor();
//load the contents of existing PDF file
FileStream ExistingFile = new FileStream("d:/pdftest/TestHTMLFileSample_1.pdf", FileMode.Open);
// create an instance of Final PDF document
FileStream FinalMerged = new FileStream("d:/pdftest/MergedPDF.pdf", FileMode.Create);
//Append file
pdfEditor.Append(ExistingFile, MyStream, 1, 1, FinalMerged);
// close all Stream Objects
MyStream.Close();
ExistingFile.Close();
FinalMerged.Close();

Thank you so much for you patience but this is not exactly what I am looking for. Let me give you the background:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I need to create a pdf and append different file types: image, pdf, MS Word, MS Excel to it in different order. This means the output cannot be a Stream but a Pdf object to be able to insert more files in next steps down the road. So this is not the solution:

pdfEditor.Append(ExistingFile, MyStream, 1, 1, FinalMerged);

Here a snippet of my code which works with images:

Pdf pdf1 = new Pdf();

Section sec1 = pdf1.Sections.Add();

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(sec1);

sec1.Paragraphs.Add(image1);

image1.ImageInfo.ImageFileType = ImageFileType.Png;

image1.ImageInfo.File = "d:/pdftest/aspose-logo.png";;

now I need to add pdf file to pdf1 and not to save, not to turn it into stream, but I do not know how.

Thanks a lot.

Vlado

Hello Vlado,

Thanks for sharing the details.

In case you need to append various file formats into existing PDF documents, all these files should be first converted into PDF format and then they can be concatenated with existing files. May be you can try using the code shared over following links into order to concatenate the PDF documents.

In code snippet that I have shared earlier, I inserted the image into PDF file and finally saved the resultant PDF into Stream Object instead of saving it over system. Then I used the same Stream object containing PDF file and Appended to an existing PDF document.
  • In order to convert Word file into PDF format, please try using Aspose.Words for .NET. For more information, please visit How-to: Convert a Document to PDF
  • In order to convert Excel worksheet into PDF format, please try using Aspose.Cells for .NET. For further information, please visit Converting to PDF Files
Or , in case you need to attach different file formats as an attachment to PDF document, please try using the code snippet shared over Add Attachment from a File in an Existing PDF (Facades)<span style=“color: rgb(31, 73, 125); font-family: “Calibri”,“sans-serif”; font-size: 11pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: “Times New Roman”; mso-bidi-theme-font: minor-bidi; mso-themecolor: dark2;”>

In case I have still not understood your requirements, please share some more details. We apologize for the confusion and inconvenience.

Hello Nayyer,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I am afraid that i still not have the answer I am looking for. Let’s write a pseudo code:

1. Create new Aspose.Pdf pdf1 object

2. Place and image from file to a section on pdf1

3. Add a ABC.pdf file to the end of pdf1

4. Add new section to a page after pdf file in pdf1

5. Add new CDE.pdf file to the end of pdf1

6. Add a new image to a new section of pdf1….

As you can see from the steps there is no stream used, only Aspose.Pdf object. There is no saving of any pdf file. All what I need is manipulate Aspose.Pdf pdf1.

I know how to create pdf from MS Word or Excel, I know how to place an image on Aspose.Pdf object, what I do not know is how to append a pdf file to Aspose.Pdf object without loosing the object.

Thank you again.

Vlado

Please let me know if you need further clarification.

Regards,


Vlado

Hello Vlado,

Thanks for sharing the detailed information.

When creating a PDF document from scratch (converting an Image file to PDF) you can not add/append any existing PDF document. As per your requirement, first you need to convert all the image files into PDF format and then you can use Concatenation method of Aspose.Pdf for .NET to generate a single combined PDF document.

In case I am still missing something, please feel free to contact. We apologize for your inconvenience.

Thank you. This is not a good news for me. I need to redo lots of existing code.

Hello Vlado,

I am afraid the previously explained scenario is one that you need to follow in case you need to accomplish your requirements. However, you may try using the following workarounds to fulfill your requirements.


We are sorry for your inconvenience.