Can I convert an office file to an in-memory PDF document?


I want to convert an office document to a PDF. I understand that the source file needs to be on disk, but can the result file be a PDF in memory, such as a memory buffer or stream?


Yes, you can output the PDF results as a memory buffer or stream instead of a file on disk. In C#, it would look like:

// Instantiate an in-memory PDFDoc object
pdftron.PDF.PDFDoc pdfdoc = new PDFDoc();

// Convert the on-disk docx file to an in-memory PDFDoc
pdftron.PDF.Convert.ToPdf(pdfdoc, "input.docx");

// Save the document to a memory buffer
byte[] mem_buf = pdfdoc.Save(SDFDoc.SaveOptions.e_linearized);

// Save the document to a stream
using (FileStream ostm = new FileStream("converted_doc_mem.txt", FileMode.Create, FileAccess.Write)) {
pdfdoc.Save(ostm, SDFDoc.SaveOptions.e_linearized);


Note also that if you want to keep the source documents (such as the original docx file) in memory as well, you can set up a RAM drive and save the document there for the duration of conversion. (There are many ways to set up a RAM drive, see for one free example.) You could then “save” the Office file to the RAM drive and convert it to an in-memory PDFDoc, and the Office file would only ever be stored in memory.

[ For Office documents, PDFNet does require the document to be saved to disk. This is because of PDFNet’s use of Office Interop APIs and/or printer driver APIs, both of which require the document to be stored on disk (i.e., they require file paths). ]

The same holds for any temporary files created by PDFNet: