private void TransformPptxStream(Stream stream, OptimizeDiagnostic diagnostic, ImageOptimizerOptions options, CancellationToken token) { using var document = OpenXmlFactory.OpenPowerPoint(stream, true, options.OpenXmlUriAutoRecovery); var transformation = new OptimizerPowerPoint(_memoryStreamManager, options); transformation.Transform(document, diagnostic, token); foreach (var slide in document.PresentationPart.SlideParts) { foreach (var part in slide.EmbeddedPackageParts) { token.ThrowIfCancellationRequested(); TransformEmbeddedPart(part, diagnostic, options, token); } } }
private void TransformEmbeddedPart(EmbeddedPackagePart part, OptimizeDiagnostic diagnostic, ImageOptimizerOptions options, CancellationToken token) { // Read mode about office mime types: http://filext.com/faq/office_mime_types.php var fileType = part.ContentType switch { "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => ".docx", "application/vnd.openxmlformats-officedocument.presentationml.presentation" => ".pptx", _ => null }; if (fileType is null) { var message = $"Unsupported embedding type {part.ContentType}"; diagnostic.Errors.Add(new OptimizeError(part.Uri.OriginalString, message)); return; } using var result = Transform(fileType, part.GetStream(), out var partDiagnostic, options, token); result.Position = 0; part.FeedData(result); foreach (var error in partDiagnostic.Errors) { var pointer = part.Uri.OriginalString + '|' + error.Pointer; diagnostic.Errors.Add(new OptimizeError(pointer, error.Message)); } }
private void TransformDocxStream(Stream stream, OptimizeDiagnostic diagnostic, ImageOptimizerOptions options, CancellationToken token) { using var document = OpenXmlFactory.OpenWord(stream, true, options.OpenXmlUriAutoRecovery); var transformation = new OptimizerWord(_memoryStreamManager, options); transformation.Transform(document, diagnostic, token); foreach (var part in document.MainDocumentPart.EmbeddedPackageParts) { token.ThrowIfCancellationRequested(); TransformEmbeddedPart(part, diagnostic, options, token); } }