PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 3:59 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Tue Jul 12, 2022 11:37 pm 
Offline

Joined: Tue Jul 12, 2022 11:30 pm
Posts: 3
Hello,
so currently right now i am converting a text file to pdf file but i do not want to save it on my desktop
i want to store the pdf file in a variable and use it somewhere else in my program.

how would I do this?
i am following this guide
http://csharp.net-informations.com/file/txttopdf.htm

The part i am questioning on
string pdfFilename = "txttopdf.pdf";
pdf.Save(pdfFilename);

thank you in advance


Top
 Profile  
Reply with quote  
PostPosted: Wed Jul 13, 2022 6:24 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 905
Location: CCAA
Hi!
You can invoke "pdf.Save()" with a stream. Use a MemoryStream and you will have the PDF file in it. Use GetBytes for the stream to get a byte array.

See lines 21 through 26 in the first code block here:
http://pdfsharp.net/wiki/Clock-sample.ashx

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Jul 13, 2022 5:26 pm 
Offline

Joined: Tue Jul 12, 2022 11:30 pm
Posts: 3
TH-Soft wrote:
Hi!
You can invoke "pdf.Save()" with a stream. Use a MemoryStream and you will have the PDF file in it. Use GetBytes for the stream to get a byte array.

See lines 21 through 26 in the first code block here:
http://pdfsharp.net/wiki/Clock-sample.ashx


Thank you for the reply,
I am trying to incorporate this into my code, just to clarify. If I just want to store the pdf file in a variable how would I proceed from here?

this is my code right now

MemoryStream stream = new MemoryStream();
document.Save(stream, false);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "output/pdf";
HttpContext.Current.Response.AddHeader("content-length", stream.Length.ToString());
HttpContext.Current.Response.BinaryWrite(stream.ToArray());

i am not sure how to store the pdf file to use in another method


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 14, 2022 7:44 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 905
Location: CCAA
"stream.ToArray()" is your PDF file.
"var pdf = stream.ToArray();" gives you a variable with the PDF file. You can pass that to another method.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 14, 2022 3:26 pm 
Offline

Joined: Tue Jul 12, 2022 11:30 pm
Posts: 3
TH-Soft wrote:
"stream.ToArray()" is your PDF file.
"var pdf = stream.ToArray();" gives you a variable with the PDF file. You can pass that to another method.


Thank you again for your reply and your support

I have solved that issue but now i have come across another issue

How should my code look if im converting a byte[] to pdf
and not txt file to pdf?

many things are breaking in my code now

thank you


Top
 Profile  
Reply with quote  
PostPosted: Fri Jul 15, 2022 8:17 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 905
Location: CCAA
noodlesincup wrote:
How should my code look if im converting a byte[] to pdf
and not txt file to pdf?
Use the byte[] to initialize a MemoryStream and then use PDFsharp to read the PdfDocument from the stream.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


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

All times are UTC


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 48 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