PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

saving pdfDocument to memory stream
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3927
Page 1 of 1

Author:  clancaster [ Mon Mar 11, 2019 5:22 pm ]
Post subject:  saving pdfDocument to memory stream

I have a project where I am creating an app to open and sign .pdf files. To accomplish this, I create a new document with pdfsharp, add the pages from the original file using the XPdfform and Xgraphics classes to draw the backgrounds from the original file on the pages of the new file. Once I have the new document populated, I convert the pages to png images using ghostscript to view in a picture box. I have attached the code that I am using to do so.


//converts the pdf page into a png that can be displayed
private void PdfToPng(PdfDocument inputFile, int page_Number)
{
//create an instance of the memory stream class to save the file into
MemoryStream stream = new MemoryStream();

//Resolution of the png to display in the picturebox
var xDpi = 150; //set the x DPI
var yDpi = 150; //set the y DPI

//save the pdf document to the created stream
inputFile.Save(stream, false);


//create an instance for GhostscriptRasterizer
using (var rasterizer = new GhostscriptRasterizer())
{

//opens the PDF file for rasterizing
rasterizer.Open(stream);

//converts the PDF pages to png's
PNG = rasterizer.GetPage(xDpi, yDpi, page_Number);
stream.Close();
}

}


Is there a way that I can only save individual pages into the memory stream instead of saving the entire document? It seems inefficient to save the whole document to a stream, open that stream with ghostscript, then pull out one page.

Author:  Thomas Hoevel [ Mon Mar 11, 2019 5:41 pm ]
Post subject:  Re: saving pdfDocument to memory stream

clancaster wrote:
It seems inefficient to save the whole document to a stream, open that stream with ghostscript, then pull out one page.
AIUI you need PNGs for all pages anyway.
Why don't you create a helper class that saves the PDF document to a stream once and that can be called to convert any page to a PNG.
Close the stream at the right time.

That'll be more efficient if you need more than one page for a document.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/