Merging All Pdf Files in Directory Using iText











up vote
0
down vote

favorite












Hello everyone and thanks for your help in advance. I am trying to use iText to merge all Pdf files contained within a directory. Here is my code:



public class MergeFiles
{
public MergeFiles(string targetDirectory) {
string dest = targetDirectory + @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
PdfMerger merger = new PdfMerger(pdfDoc);

string fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries) {

//PdfMerger merger = new PdfMerger(pdfDoc);
PdfDocument newDoc = new PdfDocument(new PdfReader(fileName));
merger.Merge(newDoc, 1, newDoc.GetNumberOfPages());

newDoc.Close();

};
pdfDoc.Close();
}
}


This code is resulting in the error "System.IO.IOException: The process cannot access the file 'E:Merged.pdf' because it is being used by another process." however, I am not sure why. Any help would be appreciated.










share|improve this question
























  • First of all, which line of your code throws that error?
    – mkl
    Nov 11 at 19:24















up vote
0
down vote

favorite












Hello everyone and thanks for your help in advance. I am trying to use iText to merge all Pdf files contained within a directory. Here is my code:



public class MergeFiles
{
public MergeFiles(string targetDirectory) {
string dest = targetDirectory + @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
PdfMerger merger = new PdfMerger(pdfDoc);

string fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries) {

//PdfMerger merger = new PdfMerger(pdfDoc);
PdfDocument newDoc = new PdfDocument(new PdfReader(fileName));
merger.Merge(newDoc, 1, newDoc.GetNumberOfPages());

newDoc.Close();

};
pdfDoc.Close();
}
}


This code is resulting in the error "System.IO.IOException: The process cannot access the file 'E:Merged.pdf' because it is being used by another process." however, I am not sure why. Any help would be appreciated.










share|improve this question
























  • First of all, which line of your code throws that error?
    – mkl
    Nov 11 at 19:24













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Hello everyone and thanks for your help in advance. I am trying to use iText to merge all Pdf files contained within a directory. Here is my code:



public class MergeFiles
{
public MergeFiles(string targetDirectory) {
string dest = targetDirectory + @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
PdfMerger merger = new PdfMerger(pdfDoc);

string fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries) {

//PdfMerger merger = new PdfMerger(pdfDoc);
PdfDocument newDoc = new PdfDocument(new PdfReader(fileName));
merger.Merge(newDoc, 1, newDoc.GetNumberOfPages());

newDoc.Close();

};
pdfDoc.Close();
}
}


This code is resulting in the error "System.IO.IOException: The process cannot access the file 'E:Merged.pdf' because it is being used by another process." however, I am not sure why. Any help would be appreciated.










share|improve this question















Hello everyone and thanks for your help in advance. I am trying to use iText to merge all Pdf files contained within a directory. Here is my code:



public class MergeFiles
{
public MergeFiles(string targetDirectory) {
string dest = targetDirectory + @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
PdfMerger merger = new PdfMerger(pdfDoc);

string fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries) {

//PdfMerger merger = new PdfMerger(pdfDoc);
PdfDocument newDoc = new PdfDocument(new PdfReader(fileName));
merger.Merge(newDoc, 1, newDoc.GetNumberOfPages());

newDoc.Close();

};
pdfDoc.Close();
}
}


This code is resulting in the error "System.IO.IOException: The process cannot access the file 'E:Merged.pdf' because it is being used by another process." however, I am not sure why. Any help would be appreciated.







itext7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 20:56









Alexey Subach

4,59472042




4,59472042










asked Nov 11 at 16:49









kmcnet

34




34












  • First of all, which line of your code throws that error?
    – mkl
    Nov 11 at 19:24


















  • First of all, which line of your code throws that error?
    – mkl
    Nov 11 at 19:24
















First of all, which line of your code throws that error?
– mkl
Nov 11 at 19:24




First of all, which line of your code throws that error?
– mkl
Nov 11 at 19:24












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










After these two lines:



string dest = targetDirectory +  @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));


A new (empty) file with name "Merged.pdf" is created in your target directory, with file stream opened in writing mode to write the result of the merging process.



Then, you are getting the list of file in target directory with string fileEntries = Directory.GetFiles(targetDirectory);. This array already includes your newly created Merged.pdf file.



Eventually the code tries to merge the resultant file into itself, which obviously fails.



To avoid this error, either collect the files to merge before creating the target document (but make sure there is no existing "Merged.pdf" file in the target directory already):



string fileEntries = Directory.GetFiles(targetDirectory);

string dest = targetDirectory + @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

// The rest of the code


Or, simply remove the target file from fileEntries array manually before merging the files.






share|improve this answer





















  • Thank you so much. I should have caught that. I really appreciate the help.
    – kmcnet
    Nov 11 at 23:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250964%2fmerging-all-pdf-files-in-directory-using-itext%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










After these two lines:



string dest = targetDirectory +  @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));


A new (empty) file with name "Merged.pdf" is created in your target directory, with file stream opened in writing mode to write the result of the merging process.



Then, you are getting the list of file in target directory with string fileEntries = Directory.GetFiles(targetDirectory);. This array already includes your newly created Merged.pdf file.



Eventually the code tries to merge the resultant file into itself, which obviously fails.



To avoid this error, either collect the files to merge before creating the target document (but make sure there is no existing "Merged.pdf" file in the target directory already):



string fileEntries = Directory.GetFiles(targetDirectory);

string dest = targetDirectory + @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

// The rest of the code


Or, simply remove the target file from fileEntries array manually before merging the files.






share|improve this answer





















  • Thank you so much. I should have caught that. I really appreciate the help.
    – kmcnet
    Nov 11 at 23:03















up vote
3
down vote



accepted










After these two lines:



string dest = targetDirectory +  @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));


A new (empty) file with name "Merged.pdf" is created in your target directory, with file stream opened in writing mode to write the result of the merging process.



Then, you are getting the list of file in target directory with string fileEntries = Directory.GetFiles(targetDirectory);. This array already includes your newly created Merged.pdf file.



Eventually the code tries to merge the resultant file into itself, which obviously fails.



To avoid this error, either collect the files to merge before creating the target document (but make sure there is no existing "Merged.pdf" file in the target directory already):



string fileEntries = Directory.GetFiles(targetDirectory);

string dest = targetDirectory + @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

// The rest of the code


Or, simply remove the target file from fileEntries array manually before merging the files.






share|improve this answer





















  • Thank you so much. I should have caught that. I really appreciate the help.
    – kmcnet
    Nov 11 at 23:03













up vote
3
down vote



accepted







up vote
3
down vote



accepted






After these two lines:



string dest = targetDirectory +  @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));


A new (empty) file with name "Merged.pdf" is created in your target directory, with file stream opened in writing mode to write the result of the merging process.



Then, you are getting the list of file in target directory with string fileEntries = Directory.GetFiles(targetDirectory);. This array already includes your newly created Merged.pdf file.



Eventually the code tries to merge the resultant file into itself, which obviously fails.



To avoid this error, either collect the files to merge before creating the target document (but make sure there is no existing "Merged.pdf" file in the target directory already):



string fileEntries = Directory.GetFiles(targetDirectory);

string dest = targetDirectory + @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

// The rest of the code


Or, simply remove the target file from fileEntries array manually before merging the files.






share|improve this answer












After these two lines:



string dest = targetDirectory +  @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));


A new (empty) file with name "Merged.pdf" is created in your target directory, with file stream opened in writing mode to write the result of the merging process.



Then, you are getting the list of file in target directory with string fileEntries = Directory.GetFiles(targetDirectory);. This array already includes your newly created Merged.pdf file.



Eventually the code tries to merge the resultant file into itself, which obviously fails.



To avoid this error, either collect the files to merge before creating the target document (but make sure there is no existing "Merged.pdf" file in the target directory already):



string fileEntries = Directory.GetFiles(targetDirectory);

string dest = targetDirectory + @"Merged.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

// The rest of the code


Or, simply remove the target file from fileEntries array manually before merging the files.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 21:00









Alexey Subach

4,59472042




4,59472042












  • Thank you so much. I should have caught that. I really appreciate the help.
    – kmcnet
    Nov 11 at 23:03


















  • Thank you so much. I should have caught that. I really appreciate the help.
    – kmcnet
    Nov 11 at 23:03
















Thank you so much. I should have caught that. I really appreciate the help.
– kmcnet
Nov 11 at 23:03




Thank you so much. I should have caught that. I really appreciate the help.
– kmcnet
Nov 11 at 23:03


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250964%2fmerging-all-pdf-files-in-directory-using-itext%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine