Как повернуть изображение с аннотациями?
В этом разделе
Растровое изображение (TIFF, JPEG и т.д.) с аннотациями можно повернуть с помощью метода
AnnotationDataController.RotateImageWithAnnotations.
Векторное изображение (PDF страница) с аннотациями можно повернуть двумя способами:
- Растеризовать векторное изображение перед поворотом и повернуть растровое изображение с аннотациями. Это можно сделать с помощью метода AnnotationDataController.RotateImageWithAnnotations.
- Поворнуть векторное изображение с аннотациями. Смотрите пример ниже.
Вот C#/VB.NET код, который демонстрирует, как повернуть PDF страницу с аннотациями без растеризации PDF страницы на заданный ортогональный угол.
namespace UserGuide.Programming.Annotation
{
class HowTo_RotatePdfPageWithAnnotations
{
/// <summary>
/// Rotates current image, in annotation viewer, by a specified orthogonal angle.
/// </summary>
/// <param name="viewer">Annotation viewer.</param>
/// <param name="angle">Rotation angle.</param>
public static void RotateImageWithAnnotations(Vintasoft.Imaging.Annotation.UI.AnnotationViewer viewer, int angle)
{
if (angle % 90 != 0)
throw new System.ArgumentOutOfRangeException();
// if image is vector image (PDF)
if (viewer.Image.SourceInfo.DecoderName == "Pdf")
// rotate PDF page with annotation, without rastering PDF page,
// by a specified orthogonal angle
RotatePdfPageWithAnnotations(viewer.Image, viewer.AnnotationDataCollection, angle);
// if image is raster image (not PDF)
else
// rotate a raster image
viewer.AnnotationDataController.RotateImageWithAnnotations(viewer.Image, angle);
}
/// <summary>
/// Rotate PDF page with annotation, without rastering PDF page,
/// by a specified orthogonal angle.
/// </summary>
/// <param name="image">Image associated with PDF page.</param>
/// <param name="annotations">Annotation data collection.</param>
/// <param name="angle">Rotation angle.</param>
public static void RotatePdfPageWithAnnotations(
Vintasoft.Imaging.VintasoftImage image,
Vintasoft.Imaging.Annotation.AnnotationDataCollection annotations,
int angle)
{
if (angle % 90 != 0)
throw new System.ArgumentOutOfRangeException();
// get PDF page associated with image
Vintasoft.Imaging.Pdf.Tree.PdfPage page =
Vintasoft.Imaging.Pdf.PdfDocumentController.GetPageAssociatedWithImage(image);
if (page == null)
throw new System.ArgumentException();
// get width and height, in pixels, of not rotated image
int notRotatedImageWidth = image.Width;
int notRotatedImageHeight = image.Height;
// rotate PDF page
page.Rotate = (page.Rotate + angle) % 360;
// reload image from PDF document
image.Reload(true);
// get width and height, in pixels, of rotated image
int rotatedImageWidth = image.Width;
int rotatedImageHeight = image.Height;
// get scale factor for converting values from pixels to DIP (device independent pixels)
float scalePixelsToDipX = 96f / (float)image.Resolution.Horizontal;
float scalePixelsToDipY = 96f / (float)image.Resolution.Vertical;
// calculate offset
System.Drawing.PointF offset = new System.Drawing.PointF(
((rotatedImageWidth - notRotatedImageWidth) / 2f) * scalePixelsToDipX,
((rotatedImageHeight - notRotatedImageHeight) / 2f) * scalePixelsToDipY);
// calcualte the rotation center
System.Drawing.PointF rotationCenter = new System.Drawing.PointF(
(notRotatedImageWidth / 2f) * scalePixelsToDipX,
(notRotatedImageHeight / 2f) * scalePixelsToDipY);
// rotate annotations
RotateAnnotations(annotations, angle, offset, rotationCenter);
}
/// <summary>
/// Rotates annotations by a specified angle.
/// </summary>
/// <param name="annotations">Annotation data collection.</param>
/// <param name="angle">Rotation angle.</param>
/// <param name="offset">Offset.</param>
/// <param name="rotationCenter">Rotation center.</param>
private static void RotateAnnotations(
Vintasoft.Imaging.Annotation.AnnotationDataCollection annotations,
float angle,
System.Drawing.PointF offset,
System.Drawing.PointF rotationCenter)
{
// gets annotation locations
int annotationsCount = annotations.Count;
if (annotationsCount == 0)
return;
System.Drawing.PointF[] points = new System.Drawing.PointF[annotationsCount];
for (int i = 0; i < annotationsCount; i++)
points[i] = annotations[i].Location;
using (System.Drawing.Drawing2D.Matrix m = new System.Drawing.Drawing2D.Matrix())
{
// offset
m.Translate(offset.X, offset.Y);
// rotate at
m.RotateAt(angle, rotationCenter);
// execute transformation of annotations location
m.TransformPoints(points);
}
// change annotation location and rotation property
for (int i = 0; i < annotationsCount; i++)
{
annotations[i].Rotation += angle;
annotations[i].Location = points[i];
}
}
}
}
Namespace UserGuide.Programming.Annotation
Class HowTo_RotatePdfPageWithAnnotations
''' <summary>
''' Rotates current image, in annotation viewer, by a specified orthogonal angle.
''' </summary>
''' <param name="viewer">Annotation viewer.</param>
''' <param name="angle">Rotation angle.</param>
Public Shared Sub RotateImageWithAnnotations(viewer As Vintasoft.Imaging.Annotation.UI.AnnotationViewer, angle As Integer)
If angle Mod 90 <> 0 Then
Throw New System.ArgumentOutOfRangeException()
End If
' if image is vector image (PDF)
If viewer.Image.SourceInfo.DecoderName = "Pdf" Then
' rotate PDF page with annotation, without rastering PDF page,
' by a specified orthogonal angle
RotatePdfPageWithAnnotations(viewer.Image, viewer.AnnotationDataCollection, angle)
Else
' if image is raster image (not PDF)
' rotate a raster image
viewer.AnnotationDataController.RotateImageWithAnnotations(viewer.Image, angle)
End If
End Sub
''' <summary>
''' Rotate PDF page with annotation, without rastering PDF page,
''' by a specified orthogonal angle.
''' </summary>
''' <param name="image">Image associated with PDF page.</param>
''' <param name="annotations">Annotation data collection.</param>
''' <param name="angle">Rotation angle.</param>
Public Shared Sub RotatePdfPageWithAnnotations(image As Vintasoft.Imaging.VintasoftImage, annotations As Vintasoft.Imaging.Annotation.AnnotationDataCollection, angle As Integer)
If angle Mod 90 <> 0 Then
Throw New System.ArgumentOutOfRangeException()
End If
' get PDF page associated with image
Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = Vintasoft.Imaging.Pdf.PdfDocumentController.GetPageAssociatedWithImage(image)
If page Is Nothing Then
Throw New System.ArgumentException()
End If
' get width and height, in pixels, of not rotated image
Dim notRotatedImageWidth As Integer = image.Width
Dim notRotatedImageHeight As Integer = image.Height
' rotate PDF page
page.Rotate = (page.Rotate + angle) Mod 360
' reload image from PDF document
image.Reload(True)
' get width and height, in pixels, of rotated image
Dim rotatedImageWidth As Integer = image.Width
Dim rotatedImageHeight As Integer = image.Height
' get scale factor for converting values from pixels to DIP (device independent pixels)
Dim scalePixelsToDipX As Single = 96F / CSng(image.Resolution.Horizontal)
Dim scalePixelsToDipY As Single = 96F / CSng(image.Resolution.Vertical)
' calculate offset
Dim offset As New System.Drawing.PointF(((rotatedImageWidth - notRotatedImageWidth) / 2F) * scalePixelsToDipX, ((rotatedImageHeight - notRotatedImageHeight) / 2F) * scalePixelsToDipY)
' calcualte the rotation center
Dim rotationCenter As New System.Drawing.PointF((notRotatedImageWidth / 2F) * scalePixelsToDipX, (notRotatedImageHeight / 2F) * scalePixelsToDipY)
' rotate annotations
RotateAnnotations(annotations, angle, offset, rotationCenter)
End Sub
''' <summary>
''' Rotates annotations by a specified angle.
''' </summary>
''' <param name="annotations">Annotation data collection.</param>
''' <param name="angle">Rotation angle.</param>
''' <param name="offset">Offset.</param>
''' <param name="rotationCenter">Rotation center.</param>
Private Shared Sub RotateAnnotations(annotations As Vintasoft.Imaging.Annotation.AnnotationDataCollection, angle As Single, offset As System.Drawing.PointF, rotationCenter As System.Drawing.PointF)
' gets annotation locations
Dim annotationsCount As Integer = annotations.Count
If annotationsCount = 0 Then
Return
End If
Dim points As System.Drawing.PointF() = New System.Drawing.PointF(annotationsCount - 1) {}
For i As Integer = 0 To annotationsCount - 1
points(i) = annotations(i).Location
Next
Using m As New System.Drawing.Drawing2D.Matrix()
' offset
m.Translate(offset.X, offset.Y)
' rotate at
m.RotateAt(angle, rotationCenter)
' execute transformation of annotations location
m.TransformPoints(points)
End Using
' change annotation location and rotation property
For i As Integer = 0 To annotationsCount - 1
annotations(i).Rotation += angle
annotations(i).Location = points(i)
Next
End Sub
End Class
End Namespace