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.
itext7
add a comment |
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.
itext7
First of all, which line of your code throws that error?
– mkl
Nov 11 at 19:24
add a comment |
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.
itext7
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
itext7
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
add a comment |
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
add a comment |
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.
Thank you so much. I should have caught that. I really appreciate the help.
– kmcnet
Nov 11 at 23:03
add a comment |
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.
Thank you so much. I should have caught that. I really appreciate the help.
– kmcnet
Nov 11 at 23:03
add a comment |
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.
Thank you so much. I should have caught that. I really appreciate the help.
– kmcnet
Nov 11 at 23:03
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
First of all, which line of your code throws that error?
– mkl
Nov 11 at 19:24