Hi Guy I have a problem saving the converted image(.jpg or .png) to PDF to a path, I have the following code that works great displaying the converted image to PDF in browser but when I try to save it to a path I get blank PDF here it my code, I am using .NET Framework 4.6.1 and iTextSharp.
default.aspx.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.xmp.impl;
//- HOW TO:
var MyImage = Server.MapPath("~/media/sample.jpg");
Netstair.iTextShapHelper.ImageToPDF(MyImage,true,"~/export/");
public static void ImageToPDF(string uploadfilePath,bool saveToPath=false,string pathToSave="")
{
var _pdfiLename = "ConvertedIamge.pdf";
var _pdfilePath;
var _pdfileMedia;
if (saveToPath==true)
{
_pdfilePath = HttpContext.Current.Server.MapPath(pathToSave);
_pdfileMedia = _pdfilePath + _pdfiLename;
}
using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f))
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
//Add the Image file to the PDF document object.
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(uploadfilePath);
//bytes = ImageToBytes(img);
pdfDoc.Add(img);
pdfDoc.Close();
// var outpuStreamem = HttpContext.Current.Response.OutputStream;
if (saveToPath == true)
{
//- Save Converted Image to PDF to the passed path here
}
else
{
//- This works 100% fine, once the image converted to PDF it shows it on the browser as file downloaded
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + _pdfiLename);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.End();
}
}
}
TIA