Insert a PDF

I’m creating a PDF using Aspose.Pdf.Generator.Pdf. I want to insert another PDF that I have in a MemoryStream. How can I do this?

Hi,


Thanks for using our products.

Please visit the following link for required information on Concatenate PDF Files

As per your requirement, you can initialize Document object using MemoryStream object. In case you still face any problem or you have any further query, please feel free to contact.

I do not think the link you gave helps me. The example is for two existing PDF files. That’s not my scenario. I have a PDF that I am creating. During it’s creation, I need to append/insert another PDF that is in a MemoryStream. I cannot figure out how to do this.

Hi,


Please take a look over the following code snippet which shows the steps on how to create a sample PDF document from scratch and save it into MemoryStream object and then instantiate the Document object using this MemoryStream instance. Then you can load the existing PDF document to other Document object and then append the pages of first Document to second one.

In case of any further query, please feel free to contact.

[C#]
// instantiate MemoryStream object
MemoryStream stream = new MemoryStream();
//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”));
//Save the document
pdf1.Save(stream);

//Load first document
Aspose.Pdf.Document doc1 = new Aspose.Pdf.Document(stream);
//open second document
Aspose.Pdf.Document doc2 = new Aspose.Pdf.Document(@“D:\pdftest\existing.pdf”);
//add pages of second document to the first
doc2.Pages.Add(doc1.Pages);
//save concatenated output file
doc2.Save(@“D:\pdftest\resultant.pdf”);

Thanks! I will take a look. Question: I need to do this process repeatedly as I have numerous PDFs to merge into the PDF that I am creating. Also, in the main PDF that I am creating, I am creating Bookmarks. I still need access to the Bookmarks that I have created, and I will be adding new bookmarks that link to each PDF that I am inserting. That being said, will you method describe here still work?

OK. Maybe a modified version of what you have posted will work. But it still is not me scenario. Let me try to articulate it again:

I have a main PDF. I am creating it using Apose.PDF.Generator.PDF. So this is not a stream.

I have a second PDF - actually, numerous other PDFs, that are in a SQL database as varbinary(MAX). During the creation of the main PDF, I read these secondary PDF’s from the database and put them into a stream. No file is created.

During the creation of the main PDF, I create Bookmarks. One of the Bookmarks I need to create is a bookmark to where the secondary PDFs are placed. So, while the main PDF is still being created and not yet saved, I need to append/concatenate in the secondary PDF. I still need access to the Bookmarks, because a new bookmark is added for each secondary PDF.

Make sense? In the code you attached, you have me save my main PDF to a stream. Does saving impact my Bookmarks?

hlrosenberger:
I have a main PDF. I am creating it using Apose.PDF.Generator.PDF. So this is not a stream.

Hi,

Thanks for sharing the details.

I have saved the PDF file being generated from Aspose.Pdf.Generator namespace into Stream object so that I do not have to save it over system and then read it back. However you may consider saving it over system. Please note that Aspose.Pdf.Generator name space only supports the feature to create PDF documents from scracth and it does not support the feature to manipulate any existing PDF file.

hlrosenberger:
I have a second PDF - actually, numerous other PDFs, that are in a SQL database as varbinary(MAX). During the creation of the main PDF, I read these secondary PDF's from the database and put them into a stream. No file is created.
I believe you can read individual documents from database and append it to the main PDF generated with Aspose.Pdf.Generator. Please note that in order to fulfill this requirement, you need to try using Aspose.Pdf namespace or Aspose.Pdf.Facades namespace.
You may use Aspose.Pdf.Document object to read the PDF file or you can load the existing PDF file using Aspose.Pdf.Facades.PdfFileEditor object.

hlrosenberger:
During the creation of the main PDF, I create Bookmarks. One of the Bookmarks I need to create is a bookmark to where the secondary PDFs are placed. So, while the main PDF is still being created and not yet saved, I need to append/concatenate in the secondary PDF. I still need access to the Bookmarks, because a new bookmark is added for each secondary PDF.
Please note that Aspose.Pdf namespace provides the capability to add Bookmarks inside the PDF document. So as per your requirement, you can add Bookmarks inside the main PDF file targeting towards the secondary files attached to main PDF file. However you need to programatically calculate the reference page number where the secondary files are being attached. For this requirement, first you need to load the main PDF file into Document object, get the page count information using Document.Pages.Count property and save this value in some variable i..e. PageCount. Then load individual secondary PDF file into Document object and get the total page count for this document. Append secondary file to main PDF and add Bookmark inside the concatenated PDF file. Now you can use the PageCount variable to direct the target page for Bookmark link. And then get the page count for resultant document and update PageCount variable information, so that when you need to add another secondary PDF file, you get the appropriate information on where the document will be added and the location where Bookmark will point. For more information, please visit
  1. Get Number of Pages of a PDF File
  2. Add a Bookmark in the PDF Document

hlrosenberger:
Make sense? In the code you attached, you have me save my main PDF to a stream. Does saving impact my Bookmarks?
I believe that the saving of resultant file in Stream should not disturb the Bookmarks inside the PDF document, however if you still face any related issue or you have any further query, please feel free to contact. We apologize for your inconvenience.

PS, My apologies for the lengthy explanation of scenario but I utmost intention have been to explain the scenario in extended details.

Thanks for the details. That helps clear things up. However, I still have questions.

As I said I am using Aspose.Pdf.Generator.Pdf to create the PDF. I create the main PDF first, and save it as a file. This PDF has bookmarks.

Now, using SQL I read in the secondary PDFs from the database into a Stream and then use Facades.PdfFileEditor to create a PDF file. After all secondary PDF files are created, I use PdfFileEditor.Insert to insert the secondary PDFs into the main PDF.

Will this approach still work? What I do not have using this method are the bookmarks to the secondary PDF’s - and this is what I need.

I have this working, by keeping track of page numbers - almost.

Here’s what I am doing.

1) Create a main PDF with bookmarks using Generator.PDF. I do a Save and create a file.
2) Create secondary PDFs.
3) Using PdfFileEditor, I Insert the secondary PDFs at the correct position within the main PDF. I keep track of page number and page count. So, now I add additional bookmarks into the main PDF - I still have the object available, even though it’s been saved to a file. The problem is I do not see these additional bookmarks, and that’s because of course it’s previously been saved. So I did a second save. Now I see my entire bookmark hierarchy, but my data is missing. And I get an error message - see attached. The ‘EF000000001’ bookmarks are for the secondary PDFs.

How do I do this correctly?



Hi,


Thanks for sharing the details.

I have tried replicating the issue while using following code snippet and I am unable to notice any problem with Bookmarks. Can you please share the source PDF documents so that we can test the scenario at our end.We are sorry for your inconvenience. The following code can be optimized by using Stream objects but as per your requirement, I have saved the documents over system.

We are sorry for your inconvenience.

[C#]
// instantiate MemoryStream object
MemoryStream stream = new MemoryStream();
//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”));
//Save the document
pdf1.Save(@“D:\pdftest\Main.pdf”);

// create File Info object for first main file
PdfFileInfo FirstFileInfo = new PdfFileInfo(@“D:\pdftest\Main.pdf”);
int FirstFilePageCount = FirstFileInfo.NumberOfPages;
//create PdfFileEditor object
PdfFileEditor pdfEditor = new PdfFileEditor();
//concatenate files
pdfEditor.Concatenate(@“D:\pdftest\Main.pdf”, @“D:\pdftest\test.pdf”, @“D:\pdftest\Resultant.pdf”);
//open document
PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor();
bookmarkEditor.BindPdf(@“D:\pdftest\Resultant.pdf”);
//create bookmark of a particular page
bookmarkEditor.CreateBookmarkOfPage(“Bookmark Name”, FirstFilePageCount + 1);
//save updated PDF file
bookmarkEditor.Save(@“D:\pdftest\Resultant.pdf”);

PdfFileInfo SecondFileInfo = new PdfFileInfo(@“D:\pdftest\Resultant.pdf”);
int ResultantFilePageCount = SecondFileInfo.NumberOfPages;

//create PdfFileEditor object
PdfFileEditor pdfEditor2 = new PdfFileEditor();
//concatenate files
pdfEditor2.Concatenate(@“D:\pdftest\PDF_A.pdf”, @“D:\pdftest\Resultant.pdf”, @“D:\pdftest\Resultant2.pdf”);
//open document
PdfBookmarkEditor bookmarkEditor2 = new PdfBookmarkEditor();
bookmarkEditor2.BindPdf(@“D:\pdftest\Resultant2.pdf”);
//create bookmark of a particular page
bookmarkEditor2.CreateBookmarkOfPage(“Bookmark Name 2”, ResultantFilePageCount + 1);
//save updated PDF file
bookmarkEditor2.Save(@“D:\pdftest\Resultant2.pdf”);

At this point, I have used a different approach to the Bookmarks by using the PdfFileEditor library.

Can I ask - is it valid to save a PDF, then add more data and/or Bookmark, adn then save it again?

Hi,


As per my approach in above code snippet, PdfFileEditor object requires the document to be saved once the files are concatenated and same is the scenario with
PdfBookmarkEditor class. Once you have made some changes, you need to re-save the PDF file. In fact the objects of Aspose.Pdf.Facades cannot be directly passed to initialize other class instance. However you may consider using the approach where you can merge/concatenate PDF files using Aspose.Pdf.Document object and pass the same object as an argument to PdfBookmarkEditor constructor.

In the event of any other query, please feel free to contact.