使用iTextSharp自动缩放图像 [英] Auto scaling of Images with iTextSharp
问题描述
我正在使用iTextSharp API从C#4.0 Windows应用程序生成PDF文件.我将传递HTML字符串,其中将包含RTF和图片.我的PDF文件大小为A4,带有默认边距.请注意,当我的图片尺寸较大(例如,height ="701px" width ="935px")时,该图片将无法转换为PDF.看起来,我必须按比例缩小图像尺寸,以适合PDF A4尺寸.我通过将图片粘贴到A4大小的Word文档中进行了检查,MS Word自动将图片缩小36%,即MS Word仅占用原始图片尺寸的64%,并设置了绝对高度&宽度.
I’m generating PDF files from my C# 4.0 windows application using iTextSharp API. I’ll be passing HTML string which will contain Rich Text and Images. My PDF file size is A4 with default margins. Noticed that when I have a large image in dimension (e.g. height="701px" width="935px") , the image is not coming to PDF. Looks like, I have to scale down the image dimension which should be able to fit in the PDF A4 size. I checked this by pasting the image to a word document of A4 size, MS Word automatically scales down the image by 36% i.e. MS Word takes only 64% of the original image dimension and sets the absolute height & width.
有人可以帮助模仿C#中的类似行为吗?
Can someone please help to mimic similar behavior in C#?
让我知道如何自动设置图像高度和宽度以适合A4 PDF文件.
Let me know how to automatically set an image height & width to fit in A4 PDF file.
推荐答案
是的,iTextSharp 不会自动调整对于文档太大的图像的大小.因此,这只是一个问题:
That's correct, iTextSharp won't automatically size images that are too big for the document. So it's just a matter of:
- 使用左/右和上/下页边距计算可用文档的宽度和高度.
- 获取图像的宽度和高度.
- 将文档的宽度和高度与图像的宽度和高度进行比较.
- 根据需要缩放图像.
这是一种方法,请参见内联注释:
Here's one way, see inline comments:
// change this to any page size you want
Rectangle defaultPageSize = PageSize.A4;
using (Document document = new Document(defaultPageSize)) {
PdfWriter.GetInstance(document, STREAM);
document.Open();
// if you don't account for the left/right margins, the image will
// run off the current page
float width = defaultPageSize.Width
- document.RightMargin
- document.LeftMargin
;
float height = defaultPageSize.Height
- document.TopMargin
- document.BottomMargin
;
foreach (string path in imagePaths) {
Image image = Image.GetInstance(path);
float h = image.ScaledHeight;
float w = image.ScaledWidth;
float scalePercent;
// scale percentage is dependent on whether the image is
// 'portrait' or 'landscape'
if (h > w) {
// only scale image if it's height is __greater__ than
// the document's height, accounting for margins
if (h > height) {
scalePercent = height / h;
image.ScaleAbsolute(w * scalePercent, h * scalePercent);
}
}
else {
// same for image width
if (w > width) {
scalePercent = width / w;
image.ScaleAbsolute(w * scalePercent, h * scalePercent);
}
}
document.Add(image);
}
}
唯一值得注意的一点是,上面的imagePaths
是string[]
,因此您可以测试在添加大到适合页面大小的图像集合时发生的情况.
The only point worth noting is that imagePaths
above is a string[]
so that you can test what happens when adding a collection of images that are to big to fit in a page.
Another way is to put the image in a one column, single cell PdfPTable:
PdfPTable table = new PdfPTable(1);
table.WidthPercentage = 100;
foreach (string path in imagePaths) {
Image image = Image.GetInstance(path);
PdfPCell cell = new PdfPCell(image, true);
cell.Border = Rectangle.NO_BORDER;
table.AddCell(cell);
}
document.Add(table);
这篇关于使用iTextSharp自动缩放图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!