开发者

Generate PDF from set of JPEGs

I have a set of JPEG's on my server all the same size. Can I convert this into a PDF file serve开发者_如何转开发r side?


I'd try using 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);


I am using iText for this requirement

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


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

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.

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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜