Question:
What happens to email attachments when I convert my email file to PDF using the Apryse PDFNet SDK?
Answer:
Starting with version 11.12, the Apryse PDFNet SDK includes improved support for converting .eml and .msg files to PDF.
When converting these file types, any attachments contained in the original email are included in the output PDF as document‑level attachments only. These attachments are not automatically converted or merged into the PDF content.
Converting and Merging Email Attachments
If your workflow requires email attachments to be:
-
Converted to PDF, and
-
Merged into the main generated PDF
you must handle them explicitly:
-
Extract the attachments from the generated PDF.
-
Convert each attachment using the appropriate conversion options.
-
Merge the converted attachments into the main PDF document.
You can use the SDK’s attachment extraction APIs in combination with the relevant conversion and merge APIs to achieve this behavior.
Here is an example on extracting document-level attachments.
string html2pdf_module_path = “[YOUR MODULE PATH]”;
HTML2PDF.SetModulePath(html2pdf_module_path);
using (PDFDoc pdf_doc = new PDFDoc())
{
pdf_doc.InitSecurityHandler();
OfficeToPDFOptions options = new OfficeToPDFOptions();
pdftron.PDF.Convert.OfficeToPDF(pdf_doc, $"{str_path}{file}", options);
// Once the original email has been converted any attached files
// are included in the output PDF as document-level embedded files.
// They can be extracted and then converted and merged with the
// original output.
NameTree embedded_files =
NameTree.Find(pdf_doc.GetSDFDoc(), "EmbeddedFiles");
if (embedded_files.IsValid())
{
DictIterator it = embedded_files.GetIterator();
for (; it.HasCurrent(); it.Next())
{
FileSpec file_spec = new FileSpec(it.Value());
string file_path = file_spec.GetFilePath();
Filter filter = file_spec.GetFileData();
if (filter != null)
{
string embedded_output_file = $"{str_path}{file_path}";
filter.WriteToFile(embedded_output_file, false);
Console.WriteLine("Embeded file found in output, extracting to: " + embedded_output_file);
}
}
}
// Now the embedded files are available. You can convert them if
// they are non PDF files and merge them with the original, see
// https://docs.apryse.com/core/samples/pdfpagetest or samples on merging.
string output_file = $"{str_path}{file}.{version}.pdf";
pdf_doc.Save(output_file, SDFDoc.SaveOptions.e_linearized);
}