Saving new merged PDF file to Network folder on Windows Server 2019

Hi,

We are facing a big problem with saving a newly created pdf file to a network folder. The matter has now reached critical level. The file is a new file into which we merge our converted Pdf documents. The convertions work and can save to the network location (same location and folder even) but as soon as we attempt to save the merged file to the location we get a Java access denied error. When we run the same command locally on that server there is no problem and it works perfect. This was also working under Windows Server 2012 without any problems but since the OS upgrade it has failed to work. We have exhausted all avenues with OS support to resolve the issue and both Java versions and the OS permissions had been reconfirmed as full admin rights.

Code below:

	File dir = new File(sourceFolder);

	File[] directoryListing = dir.listFiles();

	if (directoryListing != null) {

		Arrays.sort(directoryListing);
		
		File f = new File(outputFile);

		if (!f.exists() && !f.isDirectory()) {
			Document newMergedPDF = new Document();
			newMergedPDF.save(outputFile);
			newMergedPDF.close();
		}else {
			
			f.delete();
			
			Document newMergedPDF = new Document();
			newMergedPDF.save(outputFile);
			newMergedPDF.close();
		}
		
		Document mergedPDF = new Document(outputFile);
		
		for (File child : directoryListing) {

			if (child.toString().toLowerCase().endsWith(".pdf")) {

				System.out.println("Processing file " + child);

				try {

					OptimizationOptions opt = new OptimizationOptions();

					opt.setCompressImages(true);
					opt.setImageQuality(50);
					opt.setMaxResoultion(150);
					opt.setRemoveUnusedObjects(true);
					opt.setRemoveUnusedStreams(true);
					opt.setResizeImages(true);

					mergedPDF.optimize();
					
					mergedPDF.optimizeResources(opt);

					if (preview.equalsIgnoreCase("X")) {

						System.out.println(bundle.getString("TEXT11"));	

						DocumentPrivilege privilege = DocumentPrivilege.getForbidAll();

						mergedPDF.encrypt("", "", privilege, CryptoAlgorithm.AESx128, false);

					}

					Document existingPDF = new Document(child.getAbsolutePath());

					if (watermark.equalsIgnoreCase("X")) {

						System.out.println(bundle.getString("TEXT12"));								

						AddWaterMark waterMark = new AddWaterMark();

						waterMark.addWaterMark(existingPDF, waterMarkText, watermarkOption);

					}

					System.out.println(bundle.getString("TEXT13"));	

					mergedPDF.getPages().add(existingPDF.getPages());

				} catch (Exception e) {

					System.out.println(e.getCause() + "-->" + e.getMessage());

				}

			}

		}
		System.out.println(bundle.getString("TEXT14"));	
		mergedPDF.save(outputFile);
		mergedPDF.close();
		mergedPDF.dispose();
		

		if (pageNumbers.equalsIgnoreCase("X")) {

			AddPageNumbers addPages = new AddPageNumbers();

			addPages.addPages(outputFile, gv_lan, gv_cntry);
		}

                    // System.out.println("Merging complete!");
		System.out.println(bundle.getString("TEXT15"));	
	}
}}

@mappelgryn
If you get “access denied error” then you are probably trying to work with an already occupied file.

“The conversions work and can save to the network location (same location and folder even) but as soon as we attempt to save the merged file to the location we get a Java access denied error.”

if you save just a newly created empty file, does an error appear?

What values does the outputFile variable take?
Due to the presence of the f.isDirectory() condition, it can be assumed that it can be a directory name, but in this case there will be overlaps.

What line of code is the access denied for?

@sergei.shibanov
outputFile contains the network folder and the file name with extension required for output - typically could resemble //SERVER01/Folder1/FolderForMergedDocs/Filename001.pdf
When creating the file earlier in the code it is created during:
if (!f.exists() && !f.isDirectory()) {
Document newMergedPDF = new Document();
newMergedPDF.save(outputFile);
newMergedPDF.close();

we then look through the source folder and merge all the documents into the file:

	Document mergedPDF = new Document(outputFile);

	for (File child : directoryListing) {
		if (child.toString().toLowerCase().endsWith(".pdf")) {
			System.out.println("Processing file " + child);
			try {

…snip

				mergedPDF.getPages().add(existingPDF.getPages());

			} catch (Exception e) {
				System.out.println(e.getCause() + "-->" + e.getMessage());
			}
		}
	}

Failure happens at the end on the lines at the end on:

mergedPDF.save(outputFile);
With access denied. Previously we saved the file inside FOR loop but this also would then fail on the mergedPDF.save(outputFile); when it was inside the loop. I removed it from the loop to reduce the amount of time we override file so this only happens after all merging into the file is complete.

When saving to local folders it works fine but only when saving to network shared folder we have failure

@mappelgryn
Is parallel work with several documents used?

Not to the same folder. And even failure on the first set of documents that need merged.

@mappelgryn

Try to write even the merged files to the directory one by one, waiting for the end of work with each file. At least for checking.