/// <summary> /// Attaches files to a PDF file. /// </summary> /// <param name="pdftk">The IPDFtk object.</param> /// <param name="pdfFile">A stream of the PDF file input.</param> /// <param name="attachments">Streams of files to attach to the PDF.</param> /// <param name="page">The page to attach the given files, if null then files are attached to the document level.</param> /// <returns>A result with the files attached to the PDF.</returns> public static async Task <IPDFtkResult <byte[]> > AttachFiles(this IPDFtk pdftk, Stream pdfFile, IEnumerable <KeyValuePair <string, Stream> > attachments, int?page = null) { using var inputFile = await TempPDFtkFile.FromAsync(pdfFile); using var attachmentFiles = await TempPDFtkFiles.FromAsync(attachments); return(await pdftk.AttachFiles(inputFile.TempFileName, attachmentFiles.FileNames, page)); }
public static async Task <TempPDFtkFiles> FromAsync(IEnumerable <KeyValuePair <string, byte[]> > attachments) { var tempPdFtkFiles = new TempPDFtkFiles(); foreach (var kvp in attachments) { var fileName = kvp.Key; var content = kvp.Value; #if NETSTANDARD2_0 File.WriteAllBytes(Path.Combine(tempPdFtkFiles._directory.TempDirectoryFullName, fileName), content); await Task.CompletedTask; #else await File.WriteAllBytesAsync(Path.Combine(tempPdFtkFiles._directory.TempDirectoryFullName, fileName), content); #endif } return(tempPdFtkFiles); }
public static async Task <TempPDFtkFiles> FromAsync(IEnumerable <KeyValuePair <string, Stream> > attachments) { var tempPdFtkFiles = new TempPDFtkFiles(); foreach (var kvp in attachments) { var fileName = kvp.Key; var stream = kvp.Value; #if NETSTANDARD2_0 using var openWrite = File.OpenWrite(Path.Combine(tempPdFtkFiles._directory.TempDirectoryFullName, fileName)); #else await using var openWrite = File.OpenWrite(Path.Combine(tempPdFtkFiles._directory.TempDirectoryFullName, fileName)); #endif await stream.CopyToAsync(openWrite); } return(tempPdFtkFiles); }