PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 4:59 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Mon Mar 11, 2019 5:22 pm 
Offline

Joined: Mon Mar 11, 2019 5:11 pm
Posts: 1
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.


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 11, 2019 5:41 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 191 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group