图像中自动调整大小PdfPCell与iTextSharp的 [英] Image auto resizes in PdfPCell with iTextSharp

查看:3667
本文介绍了图像中自动调整大小PdfPCell与iTextSharp的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用中iTextSharp的图像库中一个奇怪的问题。
我加入了图像的PdfPCell并由于某种原因它得到推广。
我如何保持它原来的大小?
这里是100%的PDF并在打开paint.net原始尺寸图像的图像

我虽然打印时图像会是相同的,但在PIC的区别是在印刷版相同的。我怎么把图像中原来有与ScaleXXX手动缩放图像,得到它的权利似乎有点不合逻辑,不给一个好的结果。



所以, ?表的PdfPCell内的大小,而无需缩放



下面是我的代码:

 私人PdfPTable CreateTestPDF()
{
PdfPTable表=新PdfPTable(1);
table.WidthPercentage = 100;

短语词组=新词(我的标题,_font24Bold);
table.AddCell(短语);

PdfPTable nestedTable =新PdfPTable(5);
table.WidthPercentage = 100;

乐句CELLTEXT =新词(小区1,_font9BoldBlack);
nestedTable.AddCell(CELLTEXT);

CELLTEXT =新词(小区2,_font9BoldBlack);
nestedTable.AddCell(CELLTEXT);

CELLTEXT =新词(细胞3,_font9BoldBlack);
nestedTable.AddCell(CELLTEXT);

iTextSharp.text.Image图像= iTextSharp.text.Image.GetInstance(@D:\MyPic.jpg);
image.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
PdfPCell电池=新PdfPCell(图片);
cell.Horizo​​ntalAlignment = PdfPCell.ALIGN_MIDDLE;
nestedTable.AddCell(单元);

CELLTEXT =新词(细胞5,_font9BoldBlack);
nestedTable.AddCell(CELLTEXT);

nestedTable.AddCell();

串articleInfo =测试文本;
CELLTEXT =新词(articleInfo,_font8Black);
nestedTable.AddCell(CELLTEXT);

nestedTable.AddCell();
nestedTable.AddCell();
nestedTable.AddCell();

table.AddCell(nestedTable);
SetBorderSizeForAllCells(表,iTextSharp.text.Rectangle.NO_BORDER);
返回表;
}

静态BaseColor _textColor =新BaseColor(154,154,154);
iTextSharp.text.Font _font8 =新iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA,8,iTextSharp.text.Font.NORMAL,_textColor);
iTextSharp.text.Font _font8Black =新iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA,8,iTextSharp.text.Font.NORMAL,BaseColor.BLACK);
iTextSharp.text.Font _font9 =新iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA,9,iTextSharp.text.Font.NORMAL,_textColor);
iTextSharp.text.Font _font9BoldBlack =新iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA,9,iTextSharp.text.Font.BOLD,BaseColor.BLACK);
iTextSharp.text.Font _font10 =新iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA,10,iTextSharp.text.Font.NORMAL,_textColor);
iTextSharp.text.Font _font10Black =新iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA,10,iTextSharp.text.Font.NORMAL,BaseColor.BLACK);
iTextSharp.text.Font _font10BoldBlack =新iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA,10,iTextSharp.text.Font.BOLD,BaseColor.BLACK);
iTextSharp.text.Font _font24Bold =新iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA,24,iTextSharp.text.Font.BOLD,_textColor);


解决方案

我使用iTextSharp的V4.1.2和我得到以下行为:



使用该代码,通过AddCell方法直接添加图像的表,图像被放大到适合的小区

  nestedTable.AddCell(图片); 



使用这种代码,添加图像至细胞,然后加入细胞的表,该图像原始大小显示:

  PdfPCell电池=新PdfPCell(图片); 
cell.Horizo​​ntalAlignment = PdfPCell.ALIGN_CENTER;
nestedTable.AddCell(单元);



结果
结果
你有没有直接添加图像(表外),PDF文档只是比较/双检查图像的大小?

  document.add(图片); 



结果






我假设你想和周围的一些空间细胞中心的形象。作为最后的手段,你可以改变你的形象。使它成为一个具有透明背景PNG和只要确保有一些透明的'保证金'周围的所有图像的边缘。



修改



我刚刚下载的V5.0.2和我得到相同的结果如上所述。我与那些更小并且比细胞的尺寸较大的图像试了一下,行为是相同的;第一种方法缩放图像,第二种方法不。



修改



好了,显然我已经错了对整个事情DPI年,当涉及到的图像。我似乎无法看到任何区别可言什么形象的DPI是。结果
我在三个不同的分辨率,为72dpi,96 DPI,110 DPI创造了一个600x400px图像。然后我说这些图像到一个新的文件,正是600x400。

 昏暗pSize作为矩形=新的Rectangle(600,1000 )
尺寸文档文件=新建文档(pSize,0,0,0,0)

为三个图像文件,当添加到文档中以

  document.add(图片)

他们适合文档完美,与不同的DPI设置没有区别。


i'm having a weird problem with images in iTextSharp library. I'm adding the image to the PdfPCell and for some reason it gets scaled up. How do i keep it to original size? Here's the image of the PDF at 100% and the image in its original size opened in paint.net. I though that the images would be same when printed but the difference on the pic is the same on the printed version. Having to manually scale the image with ScaleXXX to get it to right seems a bit illogical and does not give a good result.

So how do I put the image in its original size inside a PdfPCell of a table without having to scale it?

Here's my code:

private PdfPTable CreateTestPDF()
{
    PdfPTable table = new PdfPTable(1);
    table.WidthPercentage = 100;

    Phrase phrase = new Phrase("MY TITLE", _font24Bold);
    table.AddCell(phrase);

    PdfPTable nestedTable = new PdfPTable(5);
    table.WidthPercentage = 100;

    Phrase cellText = new Phrase("cell 1", _font9BoldBlack);
    nestedTable.AddCell(cellText);

    cellText = new Phrase("cell 2", _font9BoldBlack);
    nestedTable.AddCell(cellText);

    cellText = new Phrase("cell 3", _font9BoldBlack);
    nestedTable.AddCell(cellText);

    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(@"d:\MyPic.jpg");
    image.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
    PdfPCell cell = new PdfPCell(image);
    cell.HorizontalAlignment = PdfPCell.ALIGN_MIDDLE;
    nestedTable.AddCell(cell);

    cellText = new Phrase("cell 5", _font9BoldBlack);
    nestedTable.AddCell(cellText);

    nestedTable.AddCell("");

    string articleInfo = "Test Text";
    cellText = new Phrase(articleInfo, _font8Black);
    nestedTable.AddCell(cellText);

    nestedTable.AddCell("");
    nestedTable.AddCell("");
    nestedTable.AddCell("");

    table.AddCell(nestedTable);
    SetBorderSizeForAllCells(table, iTextSharp.text.Rectangle.NO_BORDER);
    return table;
}

static BaseColor _textColor = new BaseColor(154, 154, 154);
iTextSharp.text.Font _font8 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8, iTextSharp.text.Font.NORMAL, _textColor);
iTextSharp.text.Font _font8Black = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
iTextSharp.text.Font _font9 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 9, iTextSharp.text.Font.NORMAL, _textColor);
iTextSharp.text.Font _font9BoldBlack = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 9, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _font10 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, _textColor);
iTextSharp.text.Font _font10Black = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
iTextSharp.text.Font _font10BoldBlack = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _font24Bold = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 24, iTextSharp.text.Font.BOLD, _textColor);

解决方案

I'm using iTextSharp v4.1.2 and I get the following behavior:

Using this code, adding the image directly to the table via the AddCell method, the image is scaled up to fit the cell:

nestedTable.AddCell(image);

Using this code, adding the image to a cell, then adding the cell to the table, the image is displayed at its original size:

PdfPCell cell = new PdfPCell(image);
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
nestedTable.AddCell(cell);



Have you added the image directly to the pdf document (outside the table) just to compare/double-check the image sizes?

document.add(image);



I assume that you want the image centered in the cell with some space around it. As a last resort, you can change your image. Make it a png with a transparent background, and just make sure that there is some transparent 'margin' around all the edges of your image.

EDIT

I just downloaded the v5.0.2 and I get the same results as mentioned above. I've tried it with images that are both smaller and larger than the size of the cell, and the behavior is the same; the first method scales the image, the second method does not.

EDIT

Well, apparently I have been wrong for years about the whole DPI thing when it comes to images. I can't seem to see that it makes any difference at all what the DPI of the image is.
I created a 600x400px image at three different resolutions, 72dpi, 96 dpi, and 110 dpi. Then I added each these images to a new document that was exactly 600x400.

Dim pSize As Rectangle = New Rectangle(600, 1000)
Dim document As Document = New Document(pSize, 0, 0, 0, 0)

For each of the three image files, when added to the document with

document.add(image)

they fit the document perfectly, with no differences for the different DPI settings.

这篇关于图像中自动调整大小PdfPCell与iTextSharp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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