iText7将PDF转换为图像 [英] iText7 Convert PDF to Image

查看:578
本文介绍了iText7将PDF转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请让我知道可以使用哪种方法在iText7中将pdf转换为图像. 在Itexsharp中,有一个将pdf文件转换为图像的选项.以下是链接.使用iTextSharp将PDF转换为图像 http://www .c-sharpcorner.com/UploadFile/a0927b/create-pdf-document-and-convert-to-image-programmatically/

Please let me know what method can be used to convert pdf to image in iText7. In Itexsharp, there was an option to convert pdf file to images. Following is the link. PDF to Image Using iTextSharp http://www.c-sharpcorner.com/UploadFile/a0927b/create-pdf-document-and-convert-to-image-programmatically/

下面是使用以下引用链接创建的示例代码. itext7 pdf到图像 这没有按预期工作.它没有将pdf转换为图像.它正在创建一个1kb的空白图像.

Below is the sample code created using the following refernce link. itext7 pdf to image this is not working as expected. It is not converting the pdf to image. It is creating a 1kb blank image.

string fileName = System.IO.Path.GetFileNameWithoutExtension(inputFilePath);
var pdfReader = new PdfReader(inputFilePath);
var pdfDoc = new iText.Kernel.Pdf.PdfDocument(pdfReader);
int pagesLength = pdfDoc.GetNumberOfPages()+1;
for (int i = 1; i < pagesLength; i++)
{
    if (!File.Exists(System.IO.Path.Combine(imageFileDir, fileName + "_" + 
 `enter code here`(startIndex + i) + ".png")) && i < pagesLength)
    {

        PdfPage pdfPages = pdfDoc.GetPage(i);
        PdfWriter writer = new PdfWriter(System.IO.Path.Combine(imageFileDir, fileName + "_" + (startIndex + i) + ".png"), new WriterProperties().SetFullCompressionMode(true));
        PdfDocument pdf = new PdfDocument(writer);
        PdfFormXObject pageCopy = pdfPages.CopyAsFormXObject(pdf);
        iText.Layout.Element.Image image = new iText.Layout.Element.Image(pageCopy);
    }
}

推荐答案

引用Bruno:

iText不会将PDF转换为光栅图像(例如.jpg,.png等).您误解了基于现有页面创建Image实例的示例.这些示例创建了一个XObject,可以像在矢量图像中一样在新的PDF中重用它.他们不会将PDF页面转换为光栅图像.

iText does not convert PDFs to raster images (such as .jpg, .png,...). You are misinterpreting the examples that create an Image instance based on an existing page. Those examples create an XObject that can be reused in a new PDF as if it were a vector image; they don't convert a PDF page to a raster image.

您可以使用此功能(这是iText内部用于测试的功能)是GhostScript.它以pdf为输入,并将其转换为一系列图像(每页一个图像).

What you can use for this (which is what we at iText internally use for testing) is GhostScript. It takes a pdf as input and converts it to a series of images (one image per page).

这篇关于iText7将PDF转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆