如何添加图像与iTextSharp的PDF文件? [英] How do I add images to a PDF with ITextSharp?

查看:584
本文介绍了如何添加图像与iTextSharp的PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与iTextSharp的5.0.5工作。但没有找到此版本的教程。我下载了一些以前版本的教程,但给错误。我只是想一些图像拼接成一个iTextSharp的pdf文件。任何人都可以给我的代码为

I want to work with iTextSharp 5.0.5. But did not find any tutorial on this version. I downloaded some previous version tutorials but giving error. I just want to stitch some images into a pdf file by iTextSharp. Can anyone please give me code for that ?

编辑:后这一段时间的消费,我终于想通了如何添加图像。但问题是,图片太大。现在我的问题是如何缩小图像或我想看看在普通格式页面图像。现在我该怎么办呢?

After some time spending on it I finally figured it out how to add image. But the problem is that image is too big. Now my problem is how to shrink image or I want to see the image in page in normal format. Now how can I do it ?

推荐答案

首先,例子。他们是在行动中,第2版的iText的一员。幸运的是,这本书的例子是在网上使用所有。他们标记的各种类和主题它们覆盖帮你找到任何你可能会寻找。好东西。

First, the examples. They're all a part of "iText in Action, 2nd edition". Fortunately, the book's examples are all available on line. They're tagged with the various classes and topics they cover to help you find whatever you might be looking for. Good Stuff.

许多旧的例子是,正如你指出,就不再有用。这就是为什么他们在网站上不再。 。书上的例子都为最新布鲁诺可以管理(和他保持他们目前的)

Many of the old examples were, as you point out, No Longer Helpful. That's why they're no longer on the web site. The book's examples are all as up to date as Bruno could manage (and he's keeping them current).

在一方面的是非常有用的,但不是必需的。在另一方面,花在书钱是用于支持iText的钱。我不亲自受益于你买的书,但考虑布鲁诺的朋友。帮帮忙,我的朋友。

On one hand, The Book is very useful, but not required. On the other hand, money spent on the book is money given to support iText. I do not personally benefit from you buying the book, but consider Bruno a friend. Help out my friend.

否则后果不堪设想。 。GRR

Or else. Grr.

如果您在使用图片文件,你有多种选择:

If you're using Image with a Document, you have a number of options:

image.scaleAbsolute(xScale, yScale); // 1.0f == same size

image.scalePercent(percent); // 100.0f == same size

image.scaleToFit(desiredX, desiredY); // in points, always maintains aspect ratio



你们随便挑。

Take your pick.

在另一方面,如果您要添加的图像内容流, PdfContentByte ,直接,你有以上,再加上:

On the other hand, if you're adding the image to a content stream, PdfContentByte, directly, you have the above, plus:

// scale/position the image Right Here.
contentByte.addImage(image, desiredX, 0, 0, desiredY, xLoc, yLoc );
// will NOT maintain aspect ratio.  That's a "2d transformation matrix".  Google it.

在添加图片到PdfContentByte(或其子类),您可以使用上面的矩阵技术,或设置图像的缩放(第一组呼叫)和它的位置。您的需要:当您使用以下使用绝对定位:

When adding an image to a PdfContentByte (or its subclasses), you can either use the matrix technique above, or set the image's scale (first set of calls) and its position. You are required to use absolute positioning when you use the following:

image.setAbsolutePosition(xLoc, yLoc);
contentByte.addImage(image);

如果你不这样做, addImage 将抛出一个 DocumentException 消息的形象必须有绝对的定位。 (或任何本地化该字符串的版本恰好是)。

If you don't, addImage will throw a DocumentException with the message "The image must have absolute positioning." (or whatever your localized version of that string happens to be).

这篇关于如何添加图像与iTextSharp的PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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