转换PDF文件的网页与iTextSharp的图像

问题描述 投票:18回答:4

我想PDF页面转换使用iTextSharp的LIB图像。

有任何想法如何给每个页面转换成图像文件

c# itextsharp
4个回答
11
投票

iText的/ iTextSharp的可以产生和/或修改现有的PDF文件,但它们不执行任何渲染这是你在找什么。我会建议您检查出Ghostscript或知道如何实际呈现一个PDF一些其他图书馆。


6
投票

你可以使用ImageMagick的PDF转换为图像

转换-density 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();

        }
    }

4
投票

您可以使用Ghostscript到PDF文件转换成图像,我用下面的参数所需要的PDF转换成具有多个帧的TIFF图像:

gswin32c.exe   -sDEVICE=tiff12nc -dBATCH -r200 -dNOPAUSE  -sOutputFile=[Output].tiff [PDF FileName]

您还可以使用静音模式-q参数可以获取有关从here其输出设备的更多信息

从那以后,我可以轻松地将类似下面的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]);
}

-3
投票

您可以从PDF中提取图像,并保存为JPG这里是你需要iText的夏普示例代码

 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();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.