Skip to content

Commit

Permalink
Support linearized PDF files in DocumentsRevisionsValidator
Browse files Browse the repository at this point in the history
DEVSIX-8415

Autoported commit.
Original commit hash: [5a9f769c0]
  • Loading branch information
Eugene Bochilo committed Oct 3, 2024
1 parent f1f0596 commit efc2d56
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ public virtual void LinearizedDocTest(bool continueValidationAfterFail) {
using (PdfDocument document = new PdfDocument(new PdfReader(SOURCE_FOLDER + "linearizedDoc.pdf"))) {
DocumentRevisionsValidator validator = builder.BuildDocumentRevisionsValidator();
ValidationReport report = validator.ValidateAllDocumentRevisions(validationContext, document);
AssertValidationReport.AssertThat(report, (a) => a.HasStatus(ValidationReport.ValidationResult.INDETERMINATE
).HasNumberOfFailures(1).HasNumberOfLogs(1).HasLogItem((l) => l.WithCheckName(DocumentRevisionsValidator
.DOC_MDP_CHECK).WithMessage(DocumentRevisionsValidator.LINEARIZED_NOT_SUPPORTED).WithStatus(ReportItem.ReportItemStatus
.INDETERMINATE)));
AssertValidationReport.AssertThat(report, (a) => a.HasStatus(ValidationReport.ValidationResult.VALID).HasNumberOfFailures
(0).HasNumberOfLogs(0));
NUnit.Framework.Assert.AreEqual(AccessPermissions.ANNOTATION_MODIFICATION, validator.GetAccessPermissions(
));
}
Expand Down
4 changes: 4 additions & 0 deletions itext/itext.sign/itext/signatures/SignExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public static void AddAll<TKey, TValue>(this IDictionary<TKey, TValue> c, IDicti
c[pair.Key] = pair.Value;
}
}

public static void Add<T>(this IList<T> list, int index, T elem) {
list.Insert(index, elem);
}

public static T JRemoveAt<T>(this IList<T> list, int index) {
T value = list[index];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ public class DocumentRevisionsValidator {
internal const String FIELD_REMOVED = "Form field {0} was removed or unexpectedly modified.";
//\endcond

//\cond DO_NOT_DOCUMENT
internal const String LINEARIZED_NOT_SUPPORTED = "Linearized PDF documents are not supported by DocumentRevisionsValidator.";
//\endcond

//\cond DO_NOT_DOCUMENT
internal const String LOCKED_FIELD_KIDS_ADDED = "Kids were added to locked form field \"{0}\".";
//\endcond
Expand Down Expand Up @@ -390,6 +386,7 @@ internal virtual ValidationReport ValidateAllDocumentRevisions(ValidationContext
.INDETERMINATE));
return report;
}
MergeRevisionsInLinearizedDocument(document, documentRevisions);
SignatureUtil signatureUtil = new SignatureUtil(document);
IList<String> signatures = new List<String>(signatureUtil.GetSignatureNames());
if (signatures.IsEmpty()) {
Expand Down Expand Up @@ -473,6 +470,29 @@ internal virtual void ValidateRevision(DocumentRevision previousRevision, Docume
}
//\endcond

private void MergeRevisionsInLinearizedDocument(PdfDocument document, IList<DocumentRevision> documentRevisions
) {
if (documentRevisions.Count > 1) {
// We need to check if document is linearized in first revision
// We don't need to populate validation report in case of exceptions, it will happen later
CreateDocumentAndPerformOperation(documentRevisions[0], document, new ValidationReport(), (firstRevisionDocument
) => {
if (IsLinearizedPdf(document)) {
ICollection<PdfIndirectReference> mergedModifiedReferences = new HashSet<PdfIndirectReference>(documentRevisions
[0].GetModifiedObjects());
mergedModifiedReferences.AddAll(documentRevisions[1].GetModifiedObjects());
DocumentRevision mergedRevision = new DocumentRevision(documentRevisions[0].GetEofOffset(), mergedModifiedReferences
);
documentRevisions.Add(0, mergedRevision);
documentRevisions.JRemoveAt(1);
documentRevisions.JRemoveAt(1);
}
return true;
}
);
}
}

private bool ValidateRevision(ValidationReport validationReport, ValidationContext context, PdfDocument documentWithoutRevision
, PdfDocument documentWithRevision, DocumentRevision currentRevision) {
usuallyModifiedObjects = new Pair<ICollection<PdfIndirectReference>, ICollection<PdfIndirectReference>>(CreateUsuallyModifiedObjectsSet
Expand Down Expand Up @@ -737,25 +757,13 @@ private bool CreateDocumentAndPerformOperation(DocumentRevision revision, PdfDoc
}
}
catch (System.IO.IOException exception) {
if (IsLinearizedPdf(originalDocument)) {
report.AddReportItem(new ReportItem(DOC_MDP_CHECK, LINEARIZED_NOT_SUPPORTED, exception, ReportItem.ReportItemStatus
.INDETERMINATE));
}
else {
report.AddReportItem(new ReportItem(DOC_MDP_CHECK, REVISIONS_READING_EXCEPTION, exception, ReportItem.ReportItemStatus
.INDETERMINATE));
}
report.AddReportItem(new ReportItem(DOC_MDP_CHECK, REVISIONS_READING_EXCEPTION, exception, ReportItem.ReportItemStatus
.INDETERMINATE));
return false;
}
catch (Exception exception) {
if (IsLinearizedPdf(originalDocument)) {
report.AddReportItem(new ReportItem(DOC_MDP_CHECK, LINEARIZED_NOT_SUPPORTED, exception, ReportItem.ReportItemStatus
.INDETERMINATE));
}
else {
report.AddReportItem(new ReportItem(DOC_MDP_CHECK, REVISIONS_READING_EXCEPTION, exception, ReportItem.ReportItemStatus
.INDETERMINATE));
}
report.AddReportItem(new ReportItem(DOC_MDP_CHECK, REVISIONS_READING_EXCEPTION, exception, ReportItem.ReportItemStatus
.INDETERMINATE));
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion port-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6595c3a7c8890a521c1a217c82960c9fdc052420
1156fe6f2e4a2ea0794e650746efc2cf312edf2f

0 comments on commit efc2d56

Please sign in to comment.