Cant Delete or Merge Converted Files After Conversion

Product:
WebViewer
Product Version:
WebViewer Api
Please give a brief summary of your issue:
Cant delete or merge the converted files after converting using PDFDoc in C#.

Please describe your issue and provide steps to reproduce it:
Calling the PDFDocApi to convert
Please provide a link to a minimal sample where the issue is reproducible:
public ConversionResult ConvertAnyToPDF(OutputFileNameAndPathExtractor OFOP, string? stamp)
{
ConversionResult cr = new ConversionResult();
try
{

      //this method throws exceptions which are to be caught by whichever code uses it. 
      using (PDFDoc pdfdoc = new PDFDoc())
      {
          pdfdoc.InitSecurityHandler();
          string extensionOfDocument = OFOP.input_fileExtension.ToLower();
          if (convertibles.Contains(extensionOfDocument))
          {
              DocumentConversion documentConversion = pdftron.PDF.Convert.StreamingPDFConversion(pdfdoc,
                                                                                                  OFOP.input_path + OFOP.input_filenameWithExtension,
                                                                                                  null
                                                                                                  );

              documentConversion.Convert();

              //if the incoming document has a stamp request 
              if (!String.IsNullOrEmpty(stamp))
              {

                  addStamp(pdfdoc, stamp, null);
                 
              }

              // save the result
              pdfdoc.Save(OFOP.output_path + OFOP.output_filename,  SDFDoc.SaveOptions.e_linearized);
             
              
              cr.conversionSuccessStatus = true;
              cr.convertedFilePath = OFOP.output_path + OFOP.output_filename;
             


          }
          else if (html_convertibles.Contains(OFOP.input_fileExtension))
          {
              // Running on IIS express the current working directory was inside C:/ProgramFiles/IISExpress, had to jump out to directory to get to tfs
              HTML2PDF.SetModulePath("../../tfs12/ImageRetrievalService/Application/CR73830bwm1-pdftron/DocConverterPdfTron/Lib/");

              HTML2PDF converter = new HTML2PDF();

              string htmlContent = File.ReadAllText(OFOP.input_path + OFOP.input_filenameWithExtension);

              converter.InsertFromHtmlString(htmlContent);

              if (converter.Convert(pdfdoc))
              {
                  pdfdoc.Save(OFOP.output_path + OFOP.output_filename, SDFDoc.SaveOptions.e_linearized);

                  cr.conversionSuccessStatus = true;
                  cr.convertedFilePath = OFOP.output_path + OFOP.output_filename;
                  //return cr;

              }
              else
              {

                  throw new ConvertingException();

              }

          }
          else
          {

              //throw exception for document type not supported instead
              throw new ConvertingException();

          }
          pdfdoc.Close();
          pdfdoc.Dispose();
          return cr;

      }

  }
  catch (Exception ex)
  {
      throw new ConvertingException(ex.Message);
  }

}

public void addStamp(PDFDoc pdfdoc, string stamp, int? pageNumber)
{
int defaultPageSetValue = 1;
if(pageNumber is not null)
{
defaultPageSetValue = (int)pageNumber;
}
try
{
using (Stamper s = new Stamper(Stamper.SizeType.e_absolute_size, 0.10, 0.10))
{
//get alignment from appsettings later
pdfdoc.InitSecurityHandler();

         s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_center, Stamper.VerticalAlignment.e_vertical_bottom);
         //get color from appsettings later
         s.SetFontColor(new ColorPt(0, 0, 0)); // set text color to black                
         
         s.StampText(pdfdoc, stamp, new PageSet(defaultPageSetValue));
     }
     pdfdoc.Unlock();

 }
 catch(Exception ex)
 {
     throw new StampingException("Error occured during stamping. ", ex);
 }

}

public PdftronServiceImplementation(IConfiguration configuration)
{
_configuration = configuration;
PDFNet.Initialize(_configuration.GetValue(“Secrets:PDFTRON_KEY”) ?? throw new NullReferenceException(“Appsettings Secrets:PDFTRON_KEY could not be found”));

}

1 Like

Hello, I’m AiRon, an automated tech support bot :robot:
While you wait for one of our customer support representatives, I can help you with your question.


It seems like you’re having trouble deleting or merging converted files after using the PDFDoc class in C#. This issue could be related to file locking or improper disposal of resources.

Some things you can try:

  • Ensure that PDFDoc and any other resources are properly disposed of using using statements or explicitly calling Dispose().

  • After saving the document, make sure that there are no open handles to the file that could prevent deletion or merging.

  • Check if the file is being accessed by another process or thread, which could lead to file locking issues.

  • Verify that you have the necessary permissions to delete or merge files in the specified directory.

  • Use pdfdoc.Unlock() only after you are done with all operations, as unlocking prematurely can sometimes cause issues.




Here are some resources that might help you:



Hopefully, this helps you solve your problem while you wait for a human to get back to you.

1 Like

Hi spoudel,

  1. Could you provide the file you tried to convert?
  2. When you say cannot “delete”, what are you trying to delete?
  3. When you say cannot “merge”, are you trying to merge 2 documents? Were both of them converted using this code?
  4. What is the version of Webviewer you are using?

Best Regards,
Mickaël

1 Like