从JPEG集生成PDF

时间:2021-09-16 00:29:45

I have a set of JPEG's on my server all the same size. Can I convert this into a PDF file server side?

我的服务器上有一组JPEG大小相同。我可以将其转换为PDF文件服务器端吗?

3 个解决方案

#1


2  

I am using iText for this requirement

我正在使用iText来满足这个要求

Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(yourOutFile));
document.open();

for(int i=0;i<numberOfImages;i++){
  Image image1 = Image.getInstance("myImage"+i+".jpg");
  image1.scalePercent(23f);
  document.newPage();
  document.add(image1);
}
document.close();

#2


3  

I'd try using http://www.pdfsharp.net/

我尝试使用http://www.pdfsharp.net/

Something along the lines of

有点像

PdfPage page = outputDocument.AddPage();
page.Size = PdfSharp.PageSize.A4;
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage image = XImage.FromFile("MyJPGFileXXX.jpg");
gfx.DrawImage(image, 0, 0);

#3


2  

DotImage has built-in classes to do this. If all your jpegs are in one folder, you can do this:

DotImage有内置的类来执行此操作。如果你的所有jpeg都在一个文件夹中,你可以这样做:

FileSystemImageSource source = new FileSystemImageSource(pathToDirectory, "*.jpg", true);
PdfEncoder encoder = new PdfEncoder();
using (FileStream outstm = new FileStream(outputPath, FileMode.Create)) {
    encoder.Save(outstm, source, null);
}

Which will stream all of the images ending with .jpg into an output PDF file. Each page will be fit to the size of the image (this is settable). As far as I know, there is no practical limit to the number of pages you can encode (I'm pretty sure you will exceed the PDF limit before you exhaust your heap memory). In testing, I've run hundreds of images through it without stressing the machine.

这会将以.jpg结尾的所有图像流式传输到输出PDF文件中。每个页面都适合图像的大小(这是可设置的)。据我所知,你可以编码的页数没有实际限制(我很确定你在耗尽堆内存之前会超过PDF限制)。在测试中,我通过它运行了数百张图像而不会给机器带来压力。

Compression can be controlled with an event if you want finer control (ie, JPEG compression level or using Flate or JPEG 2000). Color profiles will be included if present in the JPEG and if you want PDF/A-1b, it will do that too. There is also some basic support for setting up a table of contents, if you want.

如果您想要更精细的控制(即JPEG压缩级别或使用Flate或JPEG 2000),可以使用事件控制压缩。如果出现在JPEG中,将包含颜色配置文件,如果您想要PDF / A-1b,它也会这样做。如果需要,还可以为设置目录提供一些基本支持。

Disclaimer - I work for Atalasoft and personally wrote the FileSystemImageSource and PdfEncoder classes (as well as nearly all the underlying PDF generation tools).

免责声明 - 我为Atalasoft工作并亲自编写了FileSystemImageSource和PdfEncoder类(以及几乎所有底层的PDF生成工具)。

#1


2  

I am using iText for this requirement

我正在使用iText来满足这个要求

Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(yourOutFile));
document.open();

for(int i=0;i<numberOfImages;i++){
  Image image1 = Image.getInstance("myImage"+i+".jpg");
  image1.scalePercent(23f);
  document.newPage();
  document.add(image1);
}
document.close();

#2


3  

I'd try using http://www.pdfsharp.net/

我尝试使用http://www.pdfsharp.net/

Something along the lines of

有点像

PdfPage page = outputDocument.AddPage();
page.Size = PdfSharp.PageSize.A4;
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage image = XImage.FromFile("MyJPGFileXXX.jpg");
gfx.DrawImage(image, 0, 0);

#3


2  

DotImage has built-in classes to do this. If all your jpegs are in one folder, you can do this:

DotImage有内置的类来执行此操作。如果你的所有jpeg都在一个文件夹中,你可以这样做:

FileSystemImageSource source = new FileSystemImageSource(pathToDirectory, "*.jpg", true);
PdfEncoder encoder = new PdfEncoder();
using (FileStream outstm = new FileStream(outputPath, FileMode.Create)) {
    encoder.Save(outstm, source, null);
}

Which will stream all of the images ending with .jpg into an output PDF file. Each page will be fit to the size of the image (this is settable). As far as I know, there is no practical limit to the number of pages you can encode (I'm pretty sure you will exceed the PDF limit before you exhaust your heap memory). In testing, I've run hundreds of images through it without stressing the machine.

这会将以.jpg结尾的所有图像流式传输到输出PDF文件中。每个页面都适合图像的大小(这是可设置的)。据我所知,你可以编码的页数没有实际限制(我很确定你在耗尽堆内存之前会超过PDF限制)。在测试中,我通过它运行了数百张图像而不会给机器带来压力。

Compression can be controlled with an event if you want finer control (ie, JPEG compression level or using Flate or JPEG 2000). Color profiles will be included if present in the JPEG and if you want PDF/A-1b, it will do that too. There is also some basic support for setting up a table of contents, if you want.

如果您想要更精细的控制(即JPEG压缩级别或使用Flate或JPEG 2000),可以使用事件控制压缩。如果出现在JPEG中,将包含颜色配置文件,如果您想要PDF / A-1b,它也会这样做。如果需要,还可以为设置目录提供一些基本支持。

Disclaimer - I work for Atalasoft and personally wrote the FileSystemImageSource and PdfEncoder classes (as well as nearly all the underlying PDF generation tools).

免责声明 - 我为Atalasoft工作并亲自编写了FileSystemImageSource和PdfEncoder类(以及几乎所有底层的PDF生成工具)。