【发布时间】: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