本篇文章将介绍通过 spire.pdf for .net 实现 pdf 文档与 .tiff 图片文档相互转换的方法,包括:
- pdf转tiff
- tiff转pdf
pdf转tiff
c#
static void main(string[] args)
{
//创建pdfdocument对象,加载pdf文档
pdfdocument document = new pdfdocument();
document.loadfromfile("sample.pdf");
//保存文档为.tiff格式的文件
jointiffimages(saveasimage(document), "result.tiff", encodervalue.compressionlzw);
}
//自定义方法saveasimage()
private static image[] saveasimage(pdfdocument document)
{
image[] images = new image[document.pages.count];
for (int i = 0; i < document.pages.count; i )
{
images[i] = document.saveasimage(i);
}
return images;
}
//自定义方法getencoderinfo()
private static imagecodecinfo getencoderinfo(string mimetype)
{
imagecodecinfo[] encoders = imagecodecinfo.getimageencoders();
for (int j = 0; j < encoders.length; j )
{
if (encoders[j].mimetype == mimetype)
return encoders[j];
}
throw new exception(mimetype " mime type not found in imagecodecinfo");
}
//自定义方法jointiffimages()
public static void jointiffimages(image[] images, string outfile, encodervalue compressencoder)
{
encoder enc = encoder.saveflag;
encoderparameters ep = new encoderparameters(2);
ep.param[0] = new encoderparameter(enc, (long)encodervalue.multiframe);
ep.param[1] = new encoderparameter(encoder.compression, (long)compressencoder);
image pages = images[0];
int frame = 0;
imagecodecinfo info = getencoderinfo("image/tiff");
foreach (image img in images)
{
if (frame == 0)
{
pages = img;
pages.save(outfile, info, ep);
}
else
{
ep.param[0] = new encoderparameter(enc, (long)encodervalue.framedimensionpage);
pages.saveadd(img, ep);
}
if (frame == images.length - 1)
{
ep.param[0] = new encoderparameter(enc, (long)encodervalue.flush);
pages.saveadd(ep);
}
frame ;
}
}
vb.net
private shared sub main(args as string())
'创建pdfdocument对象,加载pdf文档
dim document as new pdfdocument()
document.loadfromfile("sample.pdf")
'保存文档为.tiff格式的文件
jointiffimages(saveasimage(document), "result.tiff", encodervalue.compressionlzw)
end sub
'自定义方法saveasimage()
private shared function saveasimage(document as pdfdocument) as image()
dim images as image() = new image(document.pages.count - 1) {}
for i as integer = 0 to document.pages.count - 1
images(i) = document.saveasimage(i)
next
return images
end function
'自定义方法getencoderinfo()
private shared function getencoderinfo(mimetype as string) as imagecodecinfo
dim encoders as imagecodecinfo() = imagecodecinfo.getimageencoders()
for j as integer = 0 to encoders.length - 1
if encoders(j).mimetype = mimetype then
return encoders(j)
end if
next
throw new exception(mimetype & convert.tostring(" mime type not found in imagecodecinfo"))
end function
'自定义方法jointiffimages()
public shared sub jointiffimages(images as image(), outfile as string, compressencoder as encodervalue)
dim enc as encoder = encoder.saveflag
dim ep as new encoderparameters(2)
ep.param(0) = new encoderparameter(enc, clng(encodervalue.multiframe))
ep.param(1) = new encoderparameter(encoder.compression, clng(compressencoder))
dim pages as image = images(0)
dim frame as integer = 0
dim info as imagecodecinfo = getencoderinfo("image/tiff")
for each img as image in images
if frame = 0 then
pages = img
pages.save(outfile, info, ep)
else
ep.param(0) = new encoderparameter(enc, clng(encodervalue.framedimensionpage))
pages.saveadd(img, ep)
end if
if frame = images.length - 1 then
ep.param(0) = new encoderparameter(enc, clng(encodervalue.flush))
pages.saveadd(ep)
end if
frame = 1
next
end sub
转换结果:
tiff转pdf
c#
static void main(string[] args)
{
//创建pdfdocument对象
pdfdocument pdfdocument = new pdfdocument();
//加载tiff图片
image tiffimage = image.fromfile("test.tiff");
//调用方法拆分tiff图片
image[] images = splittiffimage(tiffimage);
for (int i = 0; i < images.length; i )
{
pdfimage pdfimg = pdfimage.fromimage(images[i]);//获取图片
pdfpagebase page = pdfdocument.pages.add();//添加pdf页面
//指定图片在pdf中的高度、宽度及位置
float width = pdfimg.width * 0.5f;
float height = pdfimg.height * 0.5f;
float x = (page.canvas.clientsize.width - width) / 2;
//将图片绘制到pdf页面
page.canvas.drawimage(pdfimg, x, 0, width, height);
}
//保存文档
pdfdocument.savetofile("result.pdf");
}
//自定义方法splittiffimage()
public static image[] splittiffimage(image tiffimage)
{
int framecount = tiffimage.getframecount(framedimension.page);
image[] images = new image[framecount];
guid objguid = tiffimage.framedimensionslist[0];
framedimension objdimension = new framedimension(objguid);
for (int i = 0; i < framecount; i )
{
tiffimage.selectactiveframe(objdimension, i);
using (memorystream ms = new memorystream())
{
tiffimage.save(ms, imageformat.tiff);
images[i] = image.fromstream(ms);
}
}
return images;
}
vb.net
private shared sub main(args as string())
'创建pdfdocument对象
dim pdfdocument as new pdfdocument()
'加载tiff图片
dim tiffimage as image = image.fromfile("test.tiff")
'调用方法拆分tiff图片
dim images as image() = splittiffimage(tiffimage)
for i as integer = 0 to images.length - 1
dim pdfimg as pdfimage = pdfimage.fromimage(images(i))
'获取图片
dim page as pdfpagebase = pdfdocument.pages.add()
'添加pdf页面
'指定图片在pdf中的高度、宽度及位置
dim width as single = pdfimg.width * 0.5f
dim height as single = pdfimg.height * 0.5f
dim x as single = (page.canvas.clientsize.width - width) / 2
'将图片绘制到pdf页面
page.canvas.drawimage(pdfimg, x, 0, width, height)
next
'保存文档
pdfdocument.savetofile("result.pdf")
end sub
'自定义方法splittiffimage()
public shared function splittiffimage(tiffimage as image) as image()
dim framecount as integer = tiffimage.getframecount(framedimension.page)
dim images as image() = new image(framecount - 1) {}
dim objguid as guid = tiffimage.framedimensionslist(0)
dim objdimension as new framedimension(objguid)
for i as integer = 0 to framecount - 1
tiffimage.selectactiveframe(objdimension, i)
using ms as new memorystream()
tiffimage.save(ms, imageformat.tiff)
images(i) = image.fromstream(ms)
end using
next
return images
end function
转换结果: