Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在将图像添加到PDF之前旋转图像?_C#_.net Core_Itext_Itext7 - Fatal编程技术网

C# 如何在将图像添加到PDF之前旋转图像?

C# 如何在将图像添加到PDF之前旋转图像?,c#,.net-core,itext,itext7,C#,.net Core,Itext,Itext7,我正在尝试向我的PDF的每个页面添加多个随机动态水印图像。这些生成的图像应旋转45度。当我将图像添加到PDF中时,即使在指定旋转后,图像仍然显示为平面。知道为什么不应用旋转吗?我用的是Itext7 iText.Kernel.Geom.Rectangle pagesize; Image img = DrawText(watermark); ImageData imageData = ImageDataFactory.CreatePng(ImageToByteArray(img)); float

我正在尝试向我的PDF的每个页面添加多个随机动态水印图像。这些生成的图像应旋转45度。当我将图像添加到PDF中时,即使在指定旋转后,图像仍然显示为平面。知道为什么不应用旋转吗?我用的是Itext7

iText.Kernel.Geom.Rectangle pagesize;
Image img = DrawText(watermark);
ImageData imageData = ImageDataFactory.CreatePng(ImageToByteArray(img));

float x, y;
// loop over every page
for (int i = 1; i <= n; i++)
{
    int watermarksPerPage = GetRandomNumber(1, MAX_WATERMARKS_PER_PAGE);
    PdfPage page = doc.GetPage(i);
    pagesize = page.GetPageSizeWithRotation();
    PdfCanvas canvas = new PdfCanvas(page);

    x = (pagesize.GetLeft() + pagesize.GetRight());
    y = (pagesize.GetRight() + pagesize.GetBottom());

    canvas.SetExtGState(gs1);

    int boundaryWidth = (int)(x * INVISIBLE_BOUNDARY_PERCENT);
    int boundaryHeight = (int)(y * INVISIBLE_BOUNDARY_PERCENT);

    for (int m = 0; m <= watermarksPerPage; m++)
    {
        //create an invisble boudary that the watermark should not cross using x% of width      
        float newx = GetRandomNumber(0, (int)x - boundaryHeight);
        float newy = GetRandomNumber(0, (int)y - boundaryWidth);
        imageData.SetRotation(ROTATION);
        canvas.AddImage(imageData, newx, newy, false);
    }
}
iText.Kernel.Geom.Rectangle页面大小;
图像img=绘图文本(水印);
ImageData ImageData=ImageDataFactory.CreatePng(ImageToByteArray(img));
浮动x,y;
//在每一页上循环

对于(int i=1;i
ImageData.SetRotation
不会设置iText渲染此
ImageData
实例时使用的旋转值,它只会覆盖图像的元数据,指示如何渲染图像以使其看起来垂直。据我所知,不会立即调用相应的
GetRotation
ll当前由iText代码执行,因此将忽略旋转值

因此,不必设置图像数据旋转值,只需在插入图像之前旋转画布即可,例如:

canvas.SaveState();
canvas.ConcatMatrix(AffineTransform.GetRotateInstance(Math.PI / 4, x, y));
canvas.AddImage(imageData, x - imageData.GetWidth() / 2, y - imageData.GetHeight() / 2, false);
canvas.RestoreState();

谢谢,它工作了!我只需要调整一些东西,但图像确实出现了旋转。