Do you own a Debenu Quick PDF Library version 7, 8, 9, 10, 11, 12, 13 or iSEDQuickPDF license? Upgrade to Debenu Quick PDF Library 14 today!
![]() |
Merge several PDF documents into one document |
Post Reply ![]() |
Author | |
JuanS ![]() Beginner ![]() Joined: 01 Apr 16 Location: Texas Status: Offline Points: 8 |
![]() ![]() ![]() ![]() ![]() Posted: 01 Apr 16 at 9:00PM |
I have a PDF file that is 400 pages long. it consists of Shop Orders. Each shop order has a corresponding drawing. The drawing is stored in a separate PDF file. I have thousands of drawings.
I want to insert the corresponding drawing after each shop order. I tried different ways of doing it using the code samples in the Knowledge Base, but no success. I can insert the fist drawing, but that's it. Any ideas on how I can do this? I am using C#. Code samples will be greatly appreciated. Thanks a lot.
Edited by JuanS - 01 Apr 16 at 9:18PM |
|
![]() |
|
Ingo ![]() Moderator Group ![]() ![]() Joined: 29 Oct 05 Status: Offline Points: 3529 |
![]() ![]() ![]() ![]() ![]() |
Hi Juan,
you know the signs and strings for a shop order identification ... So you could extract the complete text to get the page numbers. Having the pagenumbers you can split the one big pdf into single "shop order pdf-files". The same way you can try with the big drawing file. At least you can combine it your way again. So my idea: Try it using the extract functionalities and do a full text search for relevant strings and get the pagenumbers. Cheers and welcome here, Ingo |
|
Cheers,
Ingo |
|
![]() |
|
Ingo ![]() Moderator Group ![]() ![]() Joined: 29 Oct 05 Status: Offline Points: 3529 |
![]() ![]() ![]() ![]() ![]() |
Mmmmh... if i read again i think you're always one step further ;-)
You should post your code here then help will be easier. |
|
Cheers,
Ingo |
|
![]() |
|
JuanS ![]() Beginner ![]() Joined: 01 Apr 16 Location: Texas Status: Offline Points: 8 |
![]() ![]() ![]() ![]() ![]() |
Thanks Ingo, I appreciate the help.
I found this code in the knowledge Base. I modified a bit. I have this method named "mergeFiles". It takes 4 arguments. The first one is Quick PDF, the second one is the path of the drawing, the third one is the path of the final document, and the fourth one is the page position. I hardcoded the page position, but I wanted to use a loop. Inside the method you can see a second call to the LoadFromFile method and it takes the document with the shop orders, you can see "txtFile.Text". Now the method helps, but I can only insert one drawing. I use a loop at one point to try to insert the drawings but I couldn't make it work. Here is the code: private void mergeFiles(PDFLibrary mergePDF, string drawingPath, string finalPDF, int pagePosition) { // Load the document that you will extract the pages from mergePDF.LoadFromFile(drawingPath, ""); // Get the ID for the mergePDF document int docID1 = mergePDF.SelectedDocument(); // Load the document that you will insert the extracted pages into mergePDF.LoadFromFile(txtFile.Text, ""); // Get the ID for the origPDF document int docID2 = mergePDF.SelectedDocument(); // Count the number of pages in the destination document int tPages2 = mergePDF.PageCount(); // Copy pages from source document to the destination document mergePDF.CopyPageRanges(docID1, "1"); // Again count the number of pages in the destination document int tPages1 = mergePDF.PageCount(); // Get the total number of pages added to the destination document int tPages3 = tPages1 - tPages2; // Specify the page number that the new pages should be inserted at int NewPagePos = pagePosition + 1; // Loop through each insert page and move it to the correct location while (tPages3 > 0) { // We always move the last page // in the document until all // insert pages have been moved. mergePDF.SelectPage(mergePDF.PageCount()); mergePDF.MovePage(NewPagePos); // Increase location of next page. So if // first page is inserted at page number 2, // then next page will be inserted at page number 3 NewPagePos++; // Decrease number of pages left to process tPages3--; } mergePDF.SaveToFile(finalPDF); } |
|
![]() |
|
JuanS ![]() Beginner ![]() Joined: 01 Apr 16 Location: Texas Status: Offline Points: 8 |
![]() ![]() ![]() ![]() ![]() |
Here is the other approach. I tried to append pages to a file. I would extract pages from the Shop Orders file, save them, and then append the drawing to that temporary file. This approach didn't work. It wouldn't append the drawing.
private static void mergeDocuments(PDFLibrary pdfDoc, string finalPDF, string tempJobFile) { // Load the first pdf pdfDoc.LoadFromFile(finalPDF, ""); int PrimaryDoc = pdfDoc.SelectedDocument(); // Load the second pdf pdfDoc.LoadFromFile(tempJobFile, ""); int SecondaryDoc = pdfDoc.SelectedDocument(); // Select the first document pdfDoc.SelectDocument(PrimaryDoc); // Merge the second document with the // previously selected document. pdfDoc.MergeDocument(SecondaryDoc); // Save the merged document to disk pdfDoc.AppendToFile(finalPDF); } |
|
![]() |
|
Ingo ![]() Moderator Group ![]() ![]() Joined: 29 Oct 05 Status: Offline Points: 3529 |
![]() ![]() ![]() ![]() ![]() |
Juan,
please have a look at the samples. You've written: // Load the first pdf pdfDoc.LoadFromFile(finalPDF, ""); int PrimaryDoc = pdfDoc.SelectedDocument(); // Load the second pdf pdfDoc.LoadFromFile(tempJobFile, ""); After the second LoadFromFile all things from the finalPDF are lost. Please ONE LoadFromFile for ONE instance. And again: Please have a look into the samples and the developer guide. |
|
Cheers,
Ingo |
|
![]() |
|
JuanS ![]() Beginner ![]() Joined: 01 Apr 16 Location: Texas Status: Offline Points: 8 |
![]() ![]() ![]() ![]() ![]() |
Ingo,
If I have the Shop Orders file and the Drawings file plus the final file that has the two combined, does this mean I need three instances of the PDF library, one for each file? Thank you.
|
|
![]() |
|
Ingo ![]() Moderator Group ![]() ![]() Joined: 29 Oct 05 Status: Offline Points: 3529 |
![]() ![]() ![]() ![]() ![]() |
if you're handling with two files then two instances.
Why you don't use the search function here in the forum? This i#ve found in the samples section: http://www.quickpdf.org/forum/vb6-code-for-using-copypageranges-function_topic1219_post5615.html?KW=copypageranges#5615 Edited by Ingo - 04 Apr 16 at 8:42PM |
|
Cheers,
Ingo |
|
![]() |
|
JuanS ![]() Beginner ![]() Joined: 01 Apr 16 Location: Texas Status: Offline Points: 8 |
![]() ![]() ![]() ![]() ![]() |
Thanks Ingo,
I did use the search but it didn't turn out something I could use. I've working on my code and now I can extract the correct pages of the different files using a loop. The extracted pages from one file are saved to a temporary file. Then I call a method to merge the pages. The problem is when I try to merge them. I have a file named "FinalPDF" that will hold all of the extracted pages from all the files, but when I run the code the "FinalPDF" is still a blank document at the end. I am not sure why. I did created two instances of the Quick PDF. I am passing the location of the temporary file to the method that will merge the two documents: the temporary file with the "FinalPDF". This is the modified code: private void mergeJobAndDrawings(PDFLibrary pdf, string tempJobFile) { PDFLibrary pdf2 = newPdf(); // Final document containing the Job and the Drawing var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); var finalPDF = Path.Combine(desktopFolder, "FinalPDF.pdf"); // Load the first pdf pdf2.LoadFromFile(finalPDF, ""); //pdf.SaveToFile(finalPDF); int PrimaryDoc = pdf2.SelectedDocument(); // Load the second pdf pdf.LoadFromFile(tempJobFile, ""); int SecondaryDoc = pdf.SelectedDocument(); // Select the first document pdf2.SelectDocument(PrimaryDoc); // Merge the second document with the // previously selected document. pdf2.MergeDocument(SecondaryDoc); // Save the merged document to disk pdf2.AppendToFile(finalPDF); } |
|
![]() |
|
Ingo ![]() Moderator Group ![]() ![]() Joined: 29 Oct 05 Status: Offline Points: 3529 |
![]() ![]() ![]() ![]() ![]() |
I'm missing
PDFLibrary pdf = newPdf(); Each function returns with a value. The value tells you if the calling/processing was okay or not - so check after each function. You should forget what you wanna do and starting new with the merge sample on page 28 of the developer guide 10. There you can see that both files can be load in ONE instance (Sorry ;-) BTW: You're doing a merge - so no need to use AppendToFile. Use SaveToFile...
|
|
Cheers,
Ingo |
|
![]() |
|
JuanS ![]() Beginner ![]() Joined: 01 Apr 16 Location: Texas Status: Offline Points: 8 |
![]() ![]() ![]() ![]() ![]() |
I appreciate your help.
I am going to try it. Thanks
|
|
![]() |
|
JuanS ![]() Beginner ![]() Joined: 01 Apr 16 Location: Texas Status: Offline Points: 8 |
![]() ![]() ![]() ![]() ![]() |
Good news!
It's working now! The code sample in the knowledge Base used the AppendToFile while the Developers Guide use SaveToFile. Once I change the line from AppendToFile to SaveToFile it started working. Thanks Ingo for pointing me in the right direction. Here is the code that works: private void mergeJobAndDrawings(string tempJobFile, int counter) { PDFLibrary pdf1 = newPdf(); // Final document containing the Job and the Drawing var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); var FinalPDF = Path.Combine(desktopFolder, "FinalPDF.pdf"); // Load the first pdf pdf1.LoadFromFile(FinalPDF, ""); //pdf.SaveToFile(finalPDF); int PrimaryDoc = pdf1.SelectedDocument(); // Load the second pdf pdf1.LoadFromFile(tempJobFile, ""); int SecondaryDoc = pdf1.SelectedDocument(); // Select the first document pdf1.SelectDocument(PrimaryDoc); // Merge the second document with the // previously selected document. pdf1.MergeDocument(SecondaryDoc); // Save the merged document to disk if (pdf1.SaveToFile(FinalPDF) == 1) { //MessageBox.Show("Merge Successful"); } else { MessageBox.Show("Merge Not Successful" + " " + partNumbers[counter] + " " + pagePosition[counter]); } pdf1.ReleaseLibrary(); } |
|
![]() |
|
Ingo ![]() Moderator Group ![]() ![]() Joined: 29 Oct 05 Status: Offline Points: 3529 |
![]() ![]() ![]() ![]() ![]() |
I like good news ... Thanks for the info ;-)
|
|
Cheers,
Ingo |
|
![]() |
Post Reply ![]() |
|
Tweet
|
Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |
Copyright © 2017 Debenu. Debenu Quick PDF Library is a PDF SDK. All rights reserved. About — Contact — Blog — Support — Online Store