i in process of putting code merge pdf's based on file name prefix. have below code grabs filename , doesn't merge, overwrites. believe problem filestream placement, if move out of current location, can't filename. suggestions? thanks.
static void createmergedpdfs() { string srcdir = "c:/pdfin/"; string resultpdf = "c:/pdfout/"; { var files = system.io.directory.getfiles(srcdir); string prevfilename = null; int = 1; foreach (string file in files) { string filename = left(path.getfilename(file), 8); using (filestream stream = new filestream(resultpdf + filename + ".pdf", filemode.create)) { if (prevfilename == null || filename == prevfilename) { document pdfdoc = new document(pagesize.a4); pdfcopy pdf = new pdfcopy(pdfdoc, stream); pdfdoc.open(); { pdf.adddocument(new pdfreader(file)); i++; } if (pdfdoc != null) pdfdoc.close(); console.writeline("merges done!"); } } } } } }
}
the behavior describing consistent code. creating loop in incorrect way.
try this:
static void createmergedpdfs() { string srcdir = "c:/pdfin/"; string resultpdf = "c:/pdfout/merged.pdf"; filestream stream = new filestream(resultpdf, filemode.create); document pdfdoc = new document(pagesize.a4); pdfcopy pdf = new pdfcopy(pdfdoc, stream); pdfdoc.open(); var files = system.io.directory.getfiles(srcdir); foreach (string file in files) { pdf.adddocument(new pdfreader(file)); } pdfdoc.close(); console.writeline("merges done!"); } }
that makes more sense, doesn't it?
if want group files based on prefix, should read answer question group files in directory based on prefix
in answer question, assumed prefix , rest of filename separated -
character. instance 1-abc.pdf
, 1-xyz.pdf
have prefix 1
whereas 2-abc.pdf
, 2-xyz.pdf
have prefix 2
. in case, it's not clear how you'd determine prefix, it's easy list of files, sort them , make groups of files based on whatever algorithm want determine prefix.
Comments
Post a Comment