2011-03-14 72 views

回答

2

我使用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(); 
+0

为了使这项工作适合我,我需要什么? (包含什么库?) – BIU 2011-06-14 13:13:24

+0

@BIU您需要下载[iText库](http://www.itextpdf.com/) – anu 2011-06-15 05:56:00

+0

@anu我收到以下错误:“不包含定义 ' getInstance“我已经使用iTextSharp.text添加并导入了iText库;使用iTextSharp.text.pdf的 ;使用iTextSharp的 ; – Ladessa 2013-03-18 12:43:26

3

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

东西沿着

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); 
+1

我通常使用iText的夏普,但我寻找到想要这个库,它似乎很容易使用。 – stephenbayer 2011-03-14 16:06:34

+0

mdmulinax,请参阅我的问题http://stackoverflow.com/questions/15437706/pdf-from-bitmap当我使用PDFSharp时,我得到了扭曲pdf – Ladessa 2013-03-18 12:42:12

2

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

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

如果您想要更精细的控制(即JPEG压缩级别或使用Flate或JPEG 2000),可以使用事件控制压缩。颜色配置文件将包含在JPEG中,如果您需要PDF/A-1b,它也会这样做。如果您愿意,还可以设置一些基本的支持来设置目录。免责声明 - 我为Atalasoft工作,亲自编写了FileSystemImageSource和PdfEncoder类(以及几乎所有的底层PDF生成工具)。

+0

你在使用哪种语言,以及使用哪些库? (即,为了执行此程序我需要什么?) – BIU 2011-06-14 13:10:47

+0

是否可以使用DotImage从未保存在文件目录中的jpegs生成PDF(例如,用于数据库中的文件)。因此,而不是FileSystemImageSource,有没有像MemoryStreamImageSource或什么让我喂它流输出到PDF? – Diskdrive 2015-06-24 06:08:40

+0

@Diskdrive ImageSource和RandomAccessImageSource是抽象类,可以根据需要进行子类化。我们还提供了针对数据库量身定制的DbImageSource。 – plinth 2015-06-24 12:44:14