iTextSharp的。为什么细胞背景图像顺时针旋转90度? [英] iTextSharp. Why cell background image is rotated 90 degrees clockwise?

查看:655
本文介绍了iTextSharp的。为什么细胞背景图像顺时针旋转90度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用它的自然尺寸,如果他们不适合细胞具有细胞背景 - 图像应该被裁剪。
一件事,当我使用图案填充的图像实际上是90辈分旋转。所以在这个问题上的主要就是为什么图案像它添加
我用Google搜索的答案,阅读文档后旋转,但找不到任何解释。
下面是代码:

I would like to have cell background using it's natural dimensions and if they don't fit cell - image should be cropped. One more thing that when I use pattern fill image is actually 90 degress rotated. So the main thing in this question is why pattern image is rotated after it adding I have googled for answer and read documentation, but can't find any explanation. Here is code:

Image img = Image.GetInstance("someImage.png");

var cell = new PdfPCell()
{
    BorderWidthTop = 0,
    BorderWidthBottom = 0,
    BorderWidthLeft = 0,
    BorderWidthRight = 0,
    Padding = 0,
    BackgroundColor = new BaseColor(244, 244, 244),
    BorderColor = new BaseColor(217, 217, 217),
    HorizontalAlignment = Element.ALIGN_CENTER
};
cell.CellEvent = new CellBackgroundEvent() {Image = img};
table.AddCell(cell)
;

这是事件处理程序类:

private class CellBackgroundEvent : IPdfPCellEvent
{
    public Image Image { get; set; }

    void IPdfPCellEvent.CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
    {
        PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
        PdfPatternPainter patternPainter = cb.CreatePattern(position.Right - position.Left, position.Top - position.Bottom);
        patternPainter.AddImage(Image, position.Right - position.Left, 0, 0, position.Top - position.Bottom, 0, 0);
        cb.SaveState();
        cb.SetPatternFill(patternPainter);
        cb.Rectangle(position.Left, position.Bottom, position.Width, position.Height);
        cb.Fill();
        cb.RestoreState();
    }
}



执行单元格背景图像顺时针旋转90度之后,为什么

After execution cell background image is rotated 90 degrees clockwise, why?

图片:

Image is:

实际结果细胞是:

C#iTextSharp的库版本:5.5.0.0

C# iTextSharp lib version: 5.5.0.0

推荐答案

您正在尝试使用非常复杂的代码实现很简单的东西。这是发生了什么,当你由谷歌的,而不是阅读文档的<代码!/ A>

You're trying to achieve something very simple using very complex code. This is what happens when you code by Google instead of reading the documentation!

请试试这个:

PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
Image.ScaleToFit( position.Top - position.Bottom,position.Right - position.Left);
Image.SetAbsolutePosition(position.Bottom, position.Left);
cb.AddImage(image);

现在你缩放图像以适应单元格,并在左下角的坐标添加它,细胞角

Now you are scaling the image to fit the cell and adding it at the coordinate of the lower-left corner of the cell.

更新1:

在确定了你之后居然要平铺图像,我写了一个例子试图重现该问题。因为我不理解你的代码(*),我不得不重写它的很大一部分。

After having established that you actually want to tile the image, I've written an example trying to reproduce the problem. As I didn't understand your code (*), I had to rewrite large parts of it.

总之,你会发现运行一个Java样品的此处。生成的PDF看起来像这个的图像不旋转,是吗?它看起来它会在你的问题完全相同的方式

Anyway, you'll find a working Java sample here. The resulting PDF looks like this The image isn't rotated, is it? It looks the exact same way as it looks in your question.

这是相关代码:

public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
    try {
        PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
        PdfPatternPainter patternPainter = cb.createPattern(image.getScaledWidth(), image.getScaledHeight());
        image.setAbsolutePosition(0, 0);
        patternPainter.addImage(image);
        cb.saveState();
        cb.setPatternFill(patternPainter);
        cb.rectangle(position.getLeft(), position.getBottom(), position.getWidth(), position.getHeight());
        cb.fill();
        cb.restoreState();
    } catch (DocumentException e) {
        throw new ExceptionConverter(e);
    }
}



(*)请注意,您忽略了这样一个事实: 长方形也可以给你它的宽度和高度。您首选使用 AddImage()方法的困难版本。你混淆了电池的尺寸与图像的尺寸...

(*) Note that you ignored the fact that a Rectangle can also give you its width and height. You preferred using the difficult version of the AddImage() method. You confused the dimensions of the cell with the dimensions of the image...

更新2:

Java示例被移植到C#(使用iTextSharp的5.5.0.0):

The Java example was ported to C# (using iTextSharp 5.5.0.0):

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace ConsoleApplication1 {
    public class TiledImageBackground : IPdfPCellEvent {
        protected Image image;

        public TiledImageBackground(Image image) {
            this.image = image;
        }

        public void CellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
            PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
            PdfPatternPainter patternPainter = cb.CreatePattern(image.ScaledWidth, image.ScaledHeight);
            image.SetAbsolutePosition(0, 0);
            patternPainter.AddImage(image);
            cb.SaveState();
            cb.SetPatternFill(patternPainter);
            cb.Rectangle(position.Left, position.Bottom, position.Width, position.Height);
            cb.Fill();
            cb.RestoreState();
        }
    }


    public class TiledBackground {
        public const String DEST = "results/tables/tiled_pattern.pdf";
        public const String IMG1 = "resources/images/ALxRF.png";
        public const String IMG2 = "resources/images/bulb.gif";

        private static void Main(string[] args) {
            Directory.CreateDirectory(Directory.GetParent(DEST).FullName);
            new TiledBackground().CreatePdf(DEST);
        }

        public void CreatePdf(String dest) {
            Document document = new Document();
            PdfWriter.GetInstance(document, new FileStream(dest, FileMode.Create));
            document.Open();
            PdfPTable table = new PdfPTable(2);
            PdfPCell cell = new PdfPCell();
            Image image = Image.GetInstance(IMG1);
            cell.CellEvent = new TiledImageBackground(image);
            cell.FixedHeight = 770;
            table.AddCell(cell);
            cell = new PdfPCell();
            image = Image.GetInstance(IMG2);
            cell.CellEvent = new TiledImageBackground(image);
            cell.FixedHeight = 770;
            table.AddCell(cell);
            document.Add(table);
            document.Close();
        }
    }
}



这个问题不能与此代码复制。换句话说,没有在iTextSharp的无BUG,功能正确移植

The problem couldn't be reproduced with this code. In other words: there is no bug in iTextSharp, the functionality was ported correctly.

请花点时间从这个答案尝试示例代码,以确保自己的错误是在你的代码。如果你仍然有一个改变方向的问题,你需要看看什么是你的代码不同。

Please take a moment to try the sample code from this answer to make assure yourself that the error is in your code. If you still have a problem with a changed orientation, you need to take a look at what is different in your code.

更新3:

一个额外的评论之后,我们现在已经建立的90度旋转是故意的。该文件是使用创建的:

After an additional comment, we've now established that the 90 degree rotation was intentional. The document was created using:

Document document = new Document(PageSize.A4.Rotate());

在这种情况下,您可以创建一个文件,它的宽度大于高度(在媒体框被定义为在纵向长方形),但你转动该页面(添加旋转项等于90的页字典)。

In this case, you create a document of which the width is smaller than the height (the MediaBox is defined as a rectangle in portrait), but you rotate that page (adding a Rotate entry equal to 90 to the page dictionary).

如果您希望要匹配的模式在横向页面的方向,你有两个选择:

If you want the pattern to match the orientation of a page in landscape, you have two options:


  • 任你旋转模式: img_pattern.setPatternMatrix(0,1,-1,0,0,0);

  • 或创建媒体框景观:新文件(新的Rectangle(842,595));

  • either you rotate the pattern: img_pattern.setPatternMatrix(0, 1, -1, 0, 0, 0);
  • or you create a MediaBox in landscape: new Document(new Rectangle(842, 595));

这篇关于iTextSharp的。为什么细胞背景图像顺时针旋转90度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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