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

204 views
Skip to first unread message

Aaron

unread,
Dec 5, 2014, 7:41:47 PM12/5/14
to pdfne...@googlegroups.com
Q:

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?

A:

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);

}

Aaron

unread,
Dec 8, 2014, 8:47:30 PM12/8/14
to
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 http://www.tekrevue.com/tip/create-10-gbs-ram-disk-windows/ 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:

Reply all
Reply to author
Forward
0 new messages