【问题标题】:From a PDF that was created using LaTeX, how can I retrieve images that are drawn using LaTeX?从使用 LaTeX 创建的 PDF 中,如何检索使用 LaTeX 绘制的图像?
【发布时间】:2015-10-04 02:11:16
【问题描述】:

如何从使用 LaTeX 创建的 PDF 中检索使用 LaTeX 绘制的图像?

我有从 pdf 中检索图像的 c# 代码。不幸的是,它只检索以 JPEG、PNG 等格式插入 PDF 的图像。

这是我用于从 PDF 中提取图像的代码。

class Image_Retriever
{
    public static void retrieve_image(String PDFSourcePath, String pdf_image_extraction_path)
    {
        PdfReader reader = new PdfReader(PDFSourcePath);
        PRStream pst;
        PdfImageObject pio;
        PdfObject po;
        int n = reader.XrefSize; //number of objects in pdf document
        FileStream fs = null;
        try
        {
            for (int i = 0; i < n; i++)
            {
                po = reader.GetPdfObject(i); //get the object at the index i in the objects collection
                if (po == null || !po.IsStream()) //object not found so continue
                    continue;
                pst = (PRStream)po; //cast object to stream
                PdfObject type = pst.Get(PdfName.SUBTYPE); //get the object type
                //check if the object is the image type object
                if (type != null && type.ToString().Equals(PdfName.IMAGE.ToString()))
                {
                    pio = new PdfImageObject(pst); //get the image
                    fs = new FileStream(pdf_image_extraction_path + "image" + i + ".jpg", FileMode.Create);
                    //read bytes of image in to an array
                    byte[] imgdata = pio.GetImageAsBytes();
                    //write the bytes array to file
                    fs.Write(imgdata, 0, imgdata.Length);
                    fs.Flush();
                    fs.Close();
                }
            }


        }
        catch (Exception e) { Console.WriteLine(e.Message); }
    }
}

上面的代码不检索 LaTeX 绘制的图像。

我听说在使用 LaTeX 准备 pdf 文档时,用户可以使用 LaTeX 在 PDF 中绘制图像。是否也有检索这些图像的代码?

【问题讨论】:

  • 问题是,如果图像是使用 tikz 绘制的,那么检索它们并不容易,因为它们或多或少是直接嵌入在 pdf 中的矢量图像,并且将这些图像作为图像需要你自己渲染。除了必须区分什么是文本和图形,甚至是混合的。我真的不太了解 pdf 格式,但我对 LaTeX 了解很多,我会说这很难做到。也许我会得到纠正:D

标签: c# image pdf latex


【解决方案1】:

我是 LaTeX2HTML 的前开发人员。 正如 +SkryptX 已经说过的,在 postscript 级别访问对象需要您自己渲染矢量图像。 最好的方法是让 PS 完成工作,然后从输出流中剪辑图像。 LaTeX2HTML 使用 pstoppm 工具将 ps 文件转换为 ppm(便携式像素图)图像,然后是 ppmtogif 或 ppmtopng。 除了 ps 文件,您需要知道要剪辑的图像的边界框大小,这些数据可以从 LaTeX 源中提取。

这都是很久以前的事了,所以我不能再详细说明了。 但您可能需要自行检查 l2h 源代码和 cmets 以了解更多技术问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多