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,
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,
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.
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.
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.
hlrosenberger:Make sense? In the code you attached, you have me save my main PDF to a stream. Does saving impact my Bookmarks?
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,
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”);
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,
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.