VintaSoft PDF .NET Plug-in Discussions
Questions, comments and suggestions concerning VintaSoft PDF .NET Plug-in.
Board index < VintaSoft Imaging < VintaSoft PDF .NET Plug-in Discussions
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Vintasoft.Imaging;
using Vintasoft.Imaging.Annotation.UI.Comments;
using Vintasoft.Imaging.Annotation.UI.VisualTools;
using Vintasoft.Imaging.Pdf.Print;
using Vintasoft.Imaging.Utils;
namespace DemosCommonCode.Pdf
{
/// <summary>
/// Sends images with comment controls to a printer.
/// </summary>
public class CommentedPdfPrintDocument : PdfPrintDocument
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="CommentedPdfPrintDocument"/> class.
/// </summary>
public CommentedPdfPrintDocument()
: this(null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CommentedPdfPrintDocument"/> class.
/// </summary>
/// <param name="commentTool">The comment visual tool.</param>
public CommentedPdfPrintDocument(CommentVisualTool commentTool)
{
_commentTool = commentTool;
}
#endregion
#region Properties
CommentVisualTool _commentTool;
/// <summary>
/// Gets or sets the comment visual tool.
/// </summary>
public CommentVisualTool CommentTool
{
get
{
return _commentTool;
}
set
{
_commentTool = value;
}
}
#endregion
#region Methods
/// <summary>
/// Draws the specified rectangular area of <see cref="T:Vintasoft.Imaging.VintasoftImage" /> on
/// the specified rectangular area of page's <see cref="T:System.Drawing.Graphics" />.
/// </summary>
/// <param name="image">Image to draw.</param>
/// <param name="graphics">Page's graphics, where image should be drawn.</param>
/// <param name="sourceRect">The rectangular area, in device-independent pixels (1/96th inch),
/// of image to draw.</param>
/// <param name="destRect">The rectangular area, in pixels, of page's graphics
/// where image should be drawn.</param>
/// <param name="printerResolutionX">The resolution of printer along X-axis.</param>
/// <param name="printerResolutionY">The resolution of printer along Y-axis.</param>
protected override void DrawImage(
VintasoftImage image,
Graphics graphics,
RectangleF sourceRect,
RectangleF destRect,
float printerResolutionX,
float printerResolutionY)
{
base.DrawImage(image, graphics, sourceRect, destRect, printerResolutionX, printerResolutionY);
// if comment visual tool is specified
if (_commentTool != null)
{
// get comment controls of current image
IEnumerable<ICommentControl> commentsControl = _commentTool.GetCommentControlsByImage(image);
foreach (ICommentControl commentControl in commentsControl)
{
// create transform from device independent pixels (DIPs) to pixels for specified resolution
PointFTransform transformFromDipToPixels = PointFAffineTransform.FromMatrix(
UnitOfMeasureConverter.GetTransformFromDip(UnitOfMeasure.Pixels,
new Resolution(printerResolutionX, printerResolutionY)));
// get comment bounding box in pixels
RectangleF commentBBoxInPixels = PointFAffineTransform.TransformBoundingBox(
transformFromDipToPixels, commentControl.Comment.BoundingBox);
// draw comment control
DrawCommentControlOnGraphics(
commentControl,
graphics,
Rectangle.Round(commentBBoxInPixels));
}
}
}
/// <summary>
/// Draws the comment control on graphics.
/// </summary>
/// <param name="commentControl">The comment control for draw.</param>
/// <param name="graphics">The graphics.</param>
/// <param name="destRect">The destination rect on graphics in pixels.</param>
public static void DrawCommentControlOnGraphics(
ICommentControl commentControl,
Graphics graphics,
RectangleF destRect)
{
Control control = commentControl as Control;
if (control == null)
return;
Rectangle rectOfControl = new Rectangle(0, 0, control.Width, control.Height);
// create bitmap
using (Bitmap bmp = new Bitmap(rectOfControl.Width, rectOfControl.Height,
System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
// draw contol on bitmap
control.DrawToBitmap(bmp, rectOfControl);
// draw control bitmap on graphics
graphics.DrawImage(bmp, destRect);
}
}
#endregion
}
}
Best regards, Alexander