使用itextsharp添加.eps图像到PDF
本文关键字:PDF 图像 eps itextsharp 添加 使用 | 更新日期: 2023-09-27 18:04:57
我可以使用下面的代码从图像创建一个PDF。但是当图像格式为。eps
时,我收到了一个错误。下面是我的代码: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();
}
但是现在它说。eps不是一个可识别的格式。
所以我的解决方案是把eps转换成另一种格式。
我从Microsoft找到了以下代码。
代码如下:
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);
但是它给了我这个错误:
内存不足
那么我该如何解决这个问题呢?谢谢你。
您可以使用Ghostscript通过在c#中的命令行调用它来将EPS转换为PDF。
你可以使用下面的方法一旦你安装了Ghostscript,你需要提供它的路径
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;
}