C# 使用itextsharp将Pdf文件页转换为图像

C# 使用itextsharp将Pdf文件页转换为图像,c#,itextsharp,C#,Itextsharp,我想使用ItextSharp库转换图像中的Pdf页面 知道如何转换图像文件中的每个页面吗?iText/iTextSharp可以生成和/或修改现有的PDF,但它们不会执行您正在寻找的任何渲染。我建议您签出或其他知道如何实际呈现PDF的库。您可以使用ImageMagick将PDF转换为图像 转换-密度300“d:\1.pdf”-刻度为1500000“d:\a.jpg” 而拆分pdf可以使用itextsharp 这是其他人的代码 void SplitePDF(string filepath)

我想使用ItextSharp库转换图像中的Pdf页面


知道如何转换图像文件中的每个页面吗?

iText/iTextSharp可以生成和/或修改现有的PDF,但它们不会执行您正在寻找的任何渲染。我建议您签出或其他知道如何实际呈现PDF的库。

您可以使用ImageMagick将PDF转换为图像

转换-密度300“d:\1.pdf”-刻度为1500000“d:\a.jpg”

而拆分pdf可以使用itextsharp

这是其他人的代码

void SplitePDF(string filepath)
    {
        iTextSharp.text.pdf.PdfReader reader = null;
        int currentPage = 1;
        int pageCount = 0;
        //string filepath_New = filepath + "\\PDFDestination\\";

        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        //byte[] arrayofPassword = encoding.GetBytes(ExistingFilePassword);
        reader = new iTextSharp.text.pdf.PdfReader(filepath);
        reader.RemoveUnusedObjects();
        pageCount = reader.NumberOfPages;
        string ext = System.IO.Path.GetExtension(filepath);
        for (int i = 1; i <= pageCount; i++)
        {
            iTextSharp.text.pdf.PdfReader reader1 = new iTextSharp.text.pdf.PdfReader(filepath);
            string outfile = filepath.Replace((System.IO.Path.GetFileName(filepath)), (System.IO.Path.GetFileName(filepath).Replace(".pdf", "") + "_" + i.ToString()) + ext);
            reader1.RemoveUnusedObjects();
            iTextSharp.text.Document doc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(currentPage));
            iTextSharp.text.pdf.PdfCopy pdfCpy = new iTextSharp.text.pdf.PdfCopy(doc, new System.IO.FileStream(outfile, System.IO.FileMode.Create));
            doc.Open();
            for (int j = 1; j <= 1; j++)
            {
                iTextSharp.text.pdf.PdfImportedPage page = pdfCpy.GetImportedPage(reader1, currentPage);
                pdfCpy.SetFullCompression();
                pdfCpy.AddPage(page);
                currentPage += 1;
            }
            doc.Close();
            pdfCpy.Close();
            reader1.Close();
            reader.Close();

        }
    }
void SplitePDF(字符串文件路径)
{
iTextSharp.text.pdf.PdfReader reader=null;
int currentPage=1;
int pageCount=0;
//字符串filepath_New=filepath+“\\pdfdestation\\”;
System.Text.UTF8Encoding encoding=新的System.Text.UTF8Encoding();
//byte[]arrayofPassword=encoding.GetBytes(ExistingFilePassword);
reader=new iTextSharp.text.pdf.PdfReader(文件路径);
reader.RemoveUnusedObjects();
pageCount=reader.NumberOfPages;
字符串ext=System.IO.Path.GetExtension(filepath);
对于(inti=1;i您可以使用
为了将PDF文件转换为图像,我使用以下参数将所需的PDF转换为具有多帧的tiff图像:

gswin32c.exe   -sDEVICE=tiff12nc -dBATCH -r200 -dNOPAUSE  -sOutputFile=[Output].tiff [PDF FileName]
您还可以将-q参数用于静默模式 您可以从以下站点获得有关其输出设备的更多信息:

之后,我可以像下面这样轻松加载tiff帧

using (FileStream stream = new FileStream(@"C:\tEMP\image_$i.tiff", FileMode.Open, FileAccess.Read, FileShare.Read))
{
    BitmapDecoder dec = BitmapDecoder.Create(stream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.None);
    BitmapEncoder enc = BitmapEncoder.Create(dec.CodecInfo.ContainerFormat);
    enc.Frames.Add(dec.Frames[frameIndex]);
}

您可以从PDF中提取图像 另存为JPG 下面是示例代码 你需要一把锋利的剑

 public IEnumerable<System.Drawing.Image> ExtractImagesFromPDF(string sourcePdf)
    {
        // NOTE:  This will only get the first image it finds per page.
        var pdf = new PdfReader(sourcePdf);
        var raf = new RandomAccessFileOrArray(sourcePdf);

        try
        {
            for (int pageNum = 1; pageNum <= pdf.NumberOfPages; pageNum++)
            {
                PdfDictionary pg = pdf.GetPageN(pageNum);

                // recursively search pages, forms and groups for images.
                PdfObject obj = ExtractImagesFromPDF_FindImageInPDFDictionary(pg);
                if (obj != null)
                {
                    int XrefIndex = Convert.ToInt32(((PRIndirectReference)obj).Number.ToString(CultureInfo.InvariantCulture));
                    PdfObject pdfObj = pdf.GetPdfObject(XrefIndex);
                    PdfStream pdfStrem = (PdfStream)pdfObj;
                    PdfImageObject pdfImage = new PdfImageObject((PRStream)pdfStrem);
                    System.Drawing.Image img = pdfImage.GetDrawingImage();
                    yield return img;
                }
            }
        }
        finally
        {
            pdf.Close();
            raf.Close();
        }
    }
public IEnumerable ExtractImagesFromPDF(字符串源PDF)
{
//注意:这将仅获取每页找到的第一个图像。
var pdf=新的pdf阅读器(sourcePdf);
var raf=新的随机访问文件阵列(sourcePdf);
尝试
{

对于(int pageNum=1;pageNum)“ImageMagick无法自行处理PostScript和PDF文件。为此,它使用名为Ghostscript的第三方软件作为“代理”。“从PDF中提取图像是什么?”_FindImageInPDFDictionary@JDPeckham我认为它的下两个功能是AGPL许可证:(