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!

Debenu Quick PDF Library - PDF SDK Community Forum Homepage
Forum Home Forum Home > For Users of the Library > I need help - I can help
  New Posts New Posts RSS Feed - Merge several PDF documents into one document
  FAQ FAQ  Forum Search   Register Register  Login Login

Merge several PDF documents into one document

 Post Reply Post Reply
Author
Message
JuanS View Drop Down
Beginner
Beginner


Joined: 01 Apr 16
Location: Texas
Status: Offline
Points: 8
Post Options Post Options   Thanks (0) Thanks(0)   Quote JuanS Quote  Post ReplyReply Direct Link To This Post Topic: Merge several PDF documents into one document
    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
Back to Top
Ingo View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 29 Oct 05
Status: Offline
Points: 3524
Post Options Post Options   Thanks (0) Thanks(0)   Quote Ingo Quote  Post ReplyReply Direct Link To This Post Posted: 01 Apr 16 at 10:31PM
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

Back to Top
Ingo View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 29 Oct 05
Status: Offline
Points: 3524
Post Options Post Options   Thanks (0) Thanks(0)   Quote Ingo Quote  Post ReplyReply Direct Link To This Post Posted: 01 Apr 16 at 10:32PM
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

Back to Top
JuanS View Drop Down
Beginner
Beginner


Joined: 01 Apr 16
Location: Texas
Status: Offline
Points: 8
Post Options Post Options   Thanks (0) Thanks(0)   Quote JuanS Quote  Post ReplyReply Direct Link To This Post Posted: 04 Apr 16 at 2:21PM
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);

}

Back to Top
JuanS View Drop Down
Beginner
Beginner


Joined: 01 Apr 16
Location: Texas
Status: Offline
Points: 8
Post Options Post Options   Thanks (0) Thanks(0)   Quote JuanS Quote  Post ReplyReply Direct Link To This Post Posted: 04 Apr 16 at 2:35PM
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);

}

Back to Top
Ingo View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 29 Oct 05
Status: Offline
Points: 3524
Post Options Post Options   Thanks (0) Thanks(0)   Quote Ingo Quote  Post ReplyReply Direct Link To This Post Posted: 04 Apr 16 at 8:00PM
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

Back to Top
JuanS View Drop Down
Beginner
Beginner


Joined: 01 Apr 16
Location: Texas
Status: Offline
Points: 8
Post Options Post Options   Thanks (0) Thanks(0)   Quote JuanS Quote  Post ReplyReply Direct Link To This Post Posted: 04 Apr 16 at 8:19PM
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.
Back to Top
Ingo View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 29 Oct 05
Status: Offline
Points: 3524
Post Options Post Options   Thanks (0) Thanks(0)   Quote Ingo Quote  Post ReplyReply Direct Link To This Post Posted: 04 Apr 16 at 8:41PM
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

Back to Top
JuanS View Drop Down
Beginner
Beginner


Joined: 01 Apr 16
Location: Texas
Status: Offline
Points: 8
Post Options Post Options   Thanks (0) Thanks(0)   Quote JuanS Quote  Post ReplyReply Direct Link To This Post Posted: 04 Apr 16 at 9:52PM
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);

}

Back to Top
Ingo View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 29 Oct 05
Status: Offline
Points: 3524
Post Options Post Options   Thanks (0) Thanks(0)   Quote Ingo Quote  Post ReplyReply Direct Link To This Post Posted: 05 Apr 16 at 8:15PM
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

Back to Top
JuanS View Drop Down
Beginner
Beginner


Joined: 01 Apr 16
Location: Texas
Status: Offline
Points: 8
Post Options Post Options   Thanks (0) Thanks(0)   Quote JuanS Quote  Post ReplyReply Direct Link To This Post Posted: 05 Apr 16 at 10:23PM
I appreciate your help.
I am going to try it.
 
Thanks
Back to Top
JuanS View Drop Down
Beginner
Beginner


Joined: 01 Apr 16
Location: Texas
Status: Offline
Points: 8
Post Options Post Options   Thanks (0) Thanks(0)   Quote JuanS Quote  Post ReplyReply Direct Link To This Post Posted: 06 Apr 16 at 3:56PM
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();

}

Back to Top
Ingo View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 29 Oct 05
Status: Offline
Points: 3524
Post Options Post Options   Thanks (0) Thanks(0)   Quote Ingo Quote  Post ReplyReply Direct Link To This Post Posted: 06 Apr 16 at 6:53PM
I like good news ... Thanks for the info ;-)

Cheers,
Ingo

Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 11.01
Copyright ©2001-2014 Web Wiz Ltd.

Copyright © 2017 Debenu. Debenu Quick PDF Library is a PDF SDK. All rights reserved. AboutContactBlogSupportOnline Store