簡體   English   中英

使用itextsharp添加.eps圖像到PDF

[英]Aadding .eps image to PDF using itextsharp

我可以使用下面的代碼從圖像創建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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM