简体   繁体   English

使用itextsharp添加.eps图像到PDF

[英]Aadding .eps image to PDF using itextsharp

I am able to create a PDF from image using the code below. 我可以使用下面的代码从图像创建PDF。 But I received an error when the image format is .eps 但是当图像格式为.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. 但现在它说.eps不是公认的格式。

So my solution in mind is convert the eps to another format. 所以我的解决方案是将eps转换为另一种格式。

I found the following code from Microsoft. 我从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#. 您可以使用Ghostscript通过从C#中的命令行调用EPS将其转换为PDF。

You can use the below method once you have installed Ghostscript, and you need to supply the path for it 安装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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM