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

Pri*_*wal 21 c# itextsharp

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

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

Chr*_*aas 11

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


小智 6

你可以使用ImageMagick将pdf转换为图像

convert -density 300"d:\ 1.pdf"-scale @ 1500000"d:\ a.jpg"

和split 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();

        }
    }
Run Code Online (Sandbox Code Playgroud)

  • "ImageMagick无法自行处理PostScript和PDF文件.为此,它使用名为Ghostscript的第三方软件作为'委托'." http://stackoverflow.com/a/6599718/893350 (2认同)

Ame*_*wan 5

您可以使用Ghostscript 将PDF文件转换为图像,我使用以下参数将所需的PDF转换为具有多个框架的tiff图像:

gswin32c.exe   -sDEVICE=tiff12nc -dBATCH -r200 -dNOPAUSE  -sOutputFile=[Output].tiff [PDF FileName]
Run Code Online (Sandbox Code Playgroud)

您还可以将-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]);
}
Run Code Online (Sandbox Code Playgroud)