Hi,
This example retrieves a pdf document.
How can I save the data as a pdf file to disk?
Thanks
string jsonResponse ;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(string uri);
webRequest.Method = "GET";
webRequest.Accept = "application/pdf";
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
{
jsonResponse = reader.ReadToEnd();
}
}

Manoj BhoirRank #599 · 4K reputationPosted Jun 24, 2015, 9:04 PM EDT
webRequest.Method = "GET";
webRequest.Accept = "application/pdf";
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
Stream objStream = webResponse.GetResponseStream();
using (var fileStream = new FileStream("Path To Save File", FileMode.Create, FileAccess.Write))
{
objStream.CopyTo(fileStream);
}
}
Upendra Pratap ShahiRank #329 · 7.5K reputationPosted Jun 24, 2015, 9:24 PM EDT
//Get the data from database into datatable string strQuery = "select CustomerID, ContactName, City, PostalCode" + " from customers"; SqlCommand cmd = new SqlCommand(strQuery); DataTable dt = GetData(cmd); //Create a dummy GridView GridView GridView1 = new GridView(); GridView1.AllowPaging = false; GridView1.DataSource = dt; GridView1.DataBind(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=DataTable.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); }Marvin ReidRank #1,963 · 746 reputationPosted Feb 27, 2023, 1:11 PM EST
You can take a stream and convert it to PDF using the DocumentConverter class. Here is an example that you can build on:
static void Main(string[] args) { var outBytes = ConvertStreams(new MemoryStream(inBytes)); File.WriteAllBytes("C:\Output\file.pdf", outBytes.ToArray()); } static MemoryStream ConvertStreams(Stream inputStream) { var ms = new MemoryStream(); //output stream using var document = DocumentFactory.LoadFromStream(inputStream, new LoadDocumentOptions()); using var documentConverter = new DocumentConverter(); var jobData = new DocumentConverterJobData { Document = document, DocumentFormat = DocumentFormat.Pdf, OutputDocumentStream = ms }; var job = documentConverter.Jobs.CreateJob(jobData); documentConverter.Jobs.RunJob(job); if (job.Status == DocumentConverterJobStatus.Success) return ms; else return null; }https://www.leadtools.com/help/sdk/v22/tutorials/dotnet-console-convert-files-with-the-document-converter.html