How to convert images to PDF in memory?

Question:
I need to convert PNG, JPEG and TIFF images to PDF all in memory. No reading or writing to disk.

Answer:

The following C# code shows how to convert an image to a PDF all in memory, using this class.

https://www.pdftron.com/api/PDFTronSDK/dotnet/pdftron.PDF.Convert.html#pdftron_PDF_Convert_StreamingPDFConversion_pdftron_Filters_Filter_pdftron_PDF_ConversionOptions_

PDFNet.Initialize(yourLicenseOrDemoKey);
// At the beginning of your process
PDFNet.SetDefaultDiskCachingEnabled(false); // disables writing temporary streams to disk
string imageExt = "png";
byte[] imageData = System.IO.File.ReadAllBytes(input_path + "butterfly.png"); // for demo purpose read from disk
using (pdftron.Filters.MemoryFilter memoryFilter = new pdftron.Filters.MemoryFilter((int)imageData.Length, false)) // false = sink
{
	pdftron.Filters.FilterWriter writer = new pdftron.Filters.FilterWriter(memoryFilter); // helper filter to allow us to write to buffer
	writer.WriteBuffer(imageData);
	writer.Flush();
	memoryFilter.SetAsInputFilter(); // switch from sink to source

	using (var newDoc = new PDFDoc())
	{
		var options = new ConversionOptions();
		options.SetFileExtension(imageExt);
		DocumentConversion documentConversion = pdftron.PDF.Convert.StreamingPDFConversion(newDoc, memoryFilter, options);
		documentConversion.Convert();
		byte[] pdfData = newDoc.Save(SDFDoc.SaveOptions.e_linearized);
		// System.IO.File.WriteAllBytes(outPath, pdfData); // if you want to test/verify the output
	}
}

Hi Ryan,

I used this code myself for multi page tiffs and found that it only copied over the first page of the tiff…
I then tried to adapt the code with documentConversion.ConvertNextPage(); and documentConversion.GetConversionStatus(), but documentConversion.GetConversionStatus() fires complete after a single page, which does not work…

Do you have any advice for getting multi page tiffs to work?

I actually got this to work for multi-page tiff with a different approach. I wrote the file temporarily to file then used: pdftron.PDF.Convert.ToPdf(newDoc, tempFileName); to write to the pdfDoc to then get a memory stream.

Going forward it might be nice to not have to write a file to disk to use pdftron.PDF.Convert.ToPdf… And use a memory stream/byte[] instead.

Thanks

Hi @Ryan,
Without specifying “png” format, how to generalize this conversion for more different types of formats ?