Aspose Version: 24.4.0
One thing I thought would be important to note is that this did not happen back in version 21. When I was forced to upgrade to the most recent version is when this became a problem.
Attempting to using PdfDocument.Save() is automatically redacting text that it should not be.
Here is the code that is being used
public async Task SaveEachPageOut(CompileAttachment compileAttachment, bool isScanned = false)
{
var fileExt = Path.GetExtension(compileAttachment.OriginalAzurePath).ToLower();
var filePath = Path.Combine(_attachmentPath, $"Temp{fileExt}");
//If the file is originally a Docx it processes much faster
//then a PDF so no need to save out each page.
try
{
if (fileExt.ToLower() == ".docx" || fileExt.ToLower() == ".doc")
{
await _azureProvider.DownloadToFileAsync(compileAttachment.OriginalAzurePath, filePath);
var document = new Aspose.Words.Document(filePath);
for (var i = 0; i < document.PageCount; i++)
{
var docxFilePath = Path.Combine(_attachmentPath, $"{i + 1}.docx");
var extractedPage = document.ExtractPages(i, 1);
extractedPage.Save(docxFilePath);
}
document = null;
}
else
{
filePath = fileExt != ".pdf" ? filePath.Replace(fileExt, ".pdf") : filePath;
await _azureProvider.DownloadToFileAsync(compileAttachment.AzurePdfPath, filePath);
if (isScanned)
{
ConvertScannedDocument(filePath);
}
var document = new Aspose.Pdf.Document(filePath);
try
{
document.Repair();
document.Flatten(new Aspose.Pdf.Forms.Form.FlattenSettings { ApplyRedactions = false, UpdateAppearances = true });
}
catch (Exception repairAndFlattenException)
{
var logManagerModel = new LogManagerModel
{
Exception = repairAndFlattenException,
ExceptionData = new Dictionary<string, string>()
{
{ "Message", $"Failed to repair and/or flatten PDF (this is usually because it is already flattened) this will not cause any problems. Continuing to process document." },
{ "ClassName", $"BaseCompiler" },
{ "MethodName", $"SaveEachPageOut" },
{ "StackTrace", $"{Environment.StackTrace}" }
}
};
_customerCallContext.LogManager.Error(logManagerModel);
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
//Save attachment pages into Attachment ID Folder
foreach (var page in document.Pages)
{
var pdfFilePath = Path.Combine(_attachmentPath, $"{page.Number}.pdf");
var newDocument = new Aspose.Pdf.Document();
newDocument.Pages.Add(page);
newDocument.Save(pdfFilePath);
newDocument.FreeMemory();
newDocument.Dispose();
}
document.FreeMemory();
document.Dispose();
document = null;
}
}
catch (Exception ex)
{
File.Delete(filePath);
GC.Collect();
GC.WaitForPendingFinalizers();
throw;
}
finally
{
File.Delete(filePath);
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
Here is the file before it becomes redacted
Blacked out OKC - DC-0310 WC-1018 Amendment No 4.pdf (420.6 KB)
Here is the file after it is saved out and you will notice on the last page it is being redacted.
Temp.pdf (413.4 KB)