I am able to create a PDF from image using the code below. But I received an error when the image format is .eps
Here is my code:
string imagelocation = @"C:\Users\Desktop\1.eps";
string outputpdflocation = @"C:\Users\Desktop\outputfromeps.pdf";
using (MemoryStream ms = new MemoryStream())
{
Document doc = new Document(PageSize.A4, 10, 10, 42, 35);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(outputpdflocation, FileMode.Create));
doc.AddTitle("Document Title");
doc.Open();
iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(imagelocation);
image1.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
image1.ScaleToFit(700, 900);
image1.SetAbsolutePosition((PageSize.A4.Width - image1.ScaledWidth) / 2, (PageSize.A4.Height - image1.ScaledHeight) / 2);
doc.Add(image1);
doc.Close();
}
But now it says .eps is not a recognized format.
So my solution in mind is convert the eps to another format.
I found the following code from Microsoft.
And here is the code:
System.Drawing.Image image1 = System.Drawing.Image.FromFile(@"C:\Users\Desktop\1.eps");
// Save the image in JPEG format.
image1.Save(@"C:\Users\Programmer\epsoutput.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
But it gives me this error:
Out of memory
So how can I solve this problem? Thank you.
You can use Ghostscript to convert an EPS to PDF by calling to it from the command line in C#.
You can use the below method once you have installed Ghostscript, and you need to supply the path for it
public bool ConvertEpsToPdfGSShell(string epsPath, string pdfPath,
string ghostScriptPath)
{
var success = true;
var epsQual= (char)34 + epsPath + (char)34;
var sComment = "-q -dNOPAUSE -sDEVICE=pdfwrite -o " +
(char)34 + pdfPath + (char)34 + " " + (char)34 + epsPath+ (char)34;
var p = new Process();
var psi = new ProcessStartInfo {FileName = ghostScriptPath};
if (File.Exists(psi.FileName) == false)
{
throw new Exception("Ghostscript does not exist in the path
given: " + ghostScriptPath);
}
psi.CreateNoWindow = true;
psi.UseShellExecute = true;
psi.Arguments = sComment;
p.StartInfo = psi;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.WaitForExit();
if (p.ExitCode == 0) return success;
success = false;
try
{
p.Kill();
}
catch
{
}
finally
{
p.Dispose();
}
return success;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.