// Only call this function from a STA
        // Only call if PDFDocument Open fails
        private bool RepairXRef(string source)
        {
            try
            {
                using (Workshare.Utilities.TempFileController tfc = new Utilities.TempFileController())
                {
                    // Create a copy
                    var tmp = tfc.GetTempFilename("pdf", true);
                    File.Copy(source, tmp, true);
                    if (!File.Exists(tmp))
                    {
                        Logger.LogError("PDF Repair failed to create a temp copy:" + source);
                        return false;
                    }

                    // Repair copy and override original
                    SolidFramework.Pdf.Repair.RepairResult result = SolidFramework.Pdf.Repair.RepairXRef.Rebuild(tmp, source, true);
                    if (result != SolidFramework.Pdf.Repair.RepairResult.Repaired)
                    {
                        Logger.LogError("Failed to repair PDF. File='" + source + @"'. RepairResult='" + result.ToString() + @"'.");
                        return false;
                    }

                    return true;
                }
            }
            catch (Exception ex)
            {
                Logger.LogError("Failed to repair XRef");
                Logger.LogError(ex);
                return false;
            }
        }
Example #2
0
        internal static void PdfToPdfA(string source, Type.ValidationMode mode)
        {
            try
            {
                if (String.IsNullOrEmpty(source) ||
                    !File.Exists(source))                    
                {
                    throw new ArgumentException("Invalid parameter");
                }

                Security security = new Security();
                if (security.IsEncrypted(source))
                {
                    throw new Exception("PDF/A Conversion failed. Document encrypted");
                }

                using (Workshare.Utilities.TempFileController controller = new Utilities.TempFileController())
                {
                    var target = controller.GenerateTempFileWithSpecificName(Path.GetFileNameWithoutExtension(source), Path.GetExtension(source), true);

                    Solid.PdfToPdfAData data = new Solid.PdfToPdfAData()
                        {
                            Source = source,
                            Target = target,
                            Mode = mode
                        };

                    Solid.ConvertPdfToPdfA converter = new Solid.ConvertPdfToPdfA();
                    converter.Execute(data);

                    File.Copy(target, source, true);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                throw;
            }
        }