Преглед изворни кода

ComPDFKit.Demo - Rotation Test

liuaoran пре 2 месеци
родитељ
комит
d50c66651e
1 измењених фајлова са 64 додато и 42 уклоњено
  1. 64 42
      Demo/Examples/Samples/AnnotationTest/CS/AnnotationTest.cs

+ 64 - 42
Demo/Examples/Samples/AnnotationTest/CS/AnnotationTest.cs

@@ -4,8 +4,12 @@ using ComPDFKit.PDFDocument;
 using ComPDFKit.PDFPage;
 using System;
 using System.Collections.Generic;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.Drawing.Printing;
 using System.IO;
-using ImageMagick;
+using System.Linq;
+using System.Runtime.InteropServices;
 
 namespace AnnotationTest
 {
@@ -29,7 +33,7 @@ namespace AnnotationTest
             #endregion
 
             #region Sample 1: Create annotations
-            
+
             if (CreateAnnots(document))
             {
                 Console.WriteLine("Create annots done.");
@@ -44,7 +48,7 @@ namespace AnnotationTest
             if (DeleteAnnotations(annotsDocument))
             {
                 Console.WriteLine("Create annots done.");
-            } 
+            }
 
             Console.WriteLine("--------------------");
 
@@ -90,7 +94,6 @@ namespace AnnotationTest
             List<List<CPoint>> points = new List<List<CPoint>>();
             ink.SetInkPath(points);
             ink.SetThickness(8);
-
             points.Clear();
             points.Add(new List<CPoint>()
             {
@@ -98,6 +101,8 @@ namespace AnnotationTest
                 new CPoint(100,10),
             });
             ink.SetInkPath(points);
+            ink.SetRect(new CRect(10, 10, 200, 200));
+
             ink.UpdateAp();
         }
 
@@ -116,17 +121,18 @@ namespace AnnotationTest
 
             // Square
             CPDFSquareAnnotation square = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE) as CPDFSquareAnnotation;
-            square.SetRect(new CRect(10, 250, 200, 200));
+            square.SetSourceRect(new CRect(10, 250, 200, 200)); 
             square.SetLineColor(lineColor);
             square.SetBgColor(bgColor);
             square.SetTransparency(120);
             square.SetLineWidth(1);
             square.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
+            square.AnnotationRotator.SetRotation(45);
             square.UpdateAp();
 
             // Circle
             CPDFCircleAnnotation circle = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE) as CPDFCircleAnnotation;
-            circle.SetRect(new CRect(10, 410, 110, 300));
+            circle.SetRect(new CRect(10, 300, 110, 410));
             circle.SetLineColor(lineColor);
             circle.SetBgColor(bgColor);
             circle.SetTransparency(120);
@@ -156,7 +162,7 @@ namespace AnnotationTest
             textAnnotation.SetColor(new byte[] { 255, 0, 0 });
             textAnnotation.SetTransparency(255);
             textAnnotation.SetContent("ComPDFKit");
-            textAnnotation.SetRect(new CRect(300, 650, 350, 600));
+            textAnnotation.SetRect(new CRect(300, 600, 350, 650));
             textAnnotation.UpdateAp();
         }
 
@@ -166,15 +172,10 @@ namespace AnnotationTest
         /// <param name="document"></param>
         static private void CreateSoundAnnotation(CPDFDocument document)
         {
-            CPDFPage page = document.PageAtIndex(1);
+            CPDFPage page = document.PageAtIndex(0);
             CPDFSoundAnnotation sound = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND) as CPDFSoundAnnotation;
-            sound.SetRect(new CRect(400, 750, 450, 700));
-
-            using (var image = new MagickImage("SoundAnnot.png"))
-            {
-                byte[] byteArray = image.ToByteArray(MagickFormat.Bgra);
-                sound.SetSoundPath(byteArray, image.Width, image.Height, "Bird.wav");
-            }
+            sound.SetRect(new CRect(400, 700, 450, 750));
+            sound.SetSoundPath("", "Bird.wav");
             sound.UpdateAp();
         }
 
@@ -185,7 +186,7 @@ namespace AnnotationTest
         static private void CreateMarkupAnnotation(CPDFDocument document)
         {
             List<CRect> cRectList = new List<CRect>();
-            CRect rect = new CRect(300, 300, 400, 240);
+            CRect rect = new CRect(300, 240, 400, 300);
             cRectList.Add(rect);
             byte[] color = { 255, 0, 0 };
 
@@ -226,7 +227,30 @@ namespace AnnotationTest
             squiggy.UpdateAp();
 
         }
-        
+
+        public static byte[] BitmapToByteArray(Bitmap bitmap)
+        {
+
+            BitmapData bmpdata = null;
+
+            try
+            {
+                bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
+                int numbytes = bmpdata.Stride * bitmap.Height;
+                byte[] bytedata = new byte[numbytes];
+                IntPtr ptr = bmpdata.Scan0;
+
+                Marshal.Copy(ptr, bytedata, 0, numbytes);
+
+                return bytedata;
+            }
+            finally
+            {
+                if (bmpdata != null)
+                    bitmap.UnlockBits(bmpdata);
+            }
+
+        }
 
         /// <summary>
         /// Create stamp annotation
@@ -235,37 +259,34 @@ namespace AnnotationTest
         static private void CreateStampAnnotation(CPDFDocument document)
         {
             CPDFPage page = document.PageAtIndex(0);
-            // Standard
             CPDFStampAnnotation standard = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
             standard.SetStandardStamp("Approved");
-            standard.SetRect(new CRect(300, 160, 450, 100));
+             
+            standard.SetRect(new CRect(100, 100, 250, 150));
+            // standard.AnnotationRotator.SetRotation(45);
+
             standard.UpdateAp();
 
             // Text
             CPDFStampAnnotation text = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
             text.SetTextStamp("test", "detail text", C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE, C_TEXTSTAMP_COLOR.TEXTSTAMP_RED);
-            text.SetRect(new CRect(300, 300, 450, 220));
-
+            text.SetRect(new CRect(300, 220, 450, 300));
             text.UpdateAp();
 
             // Image
-            CPDFStampAnnotation stampAnnotation = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
-            
-            using (var image = new MagickImage("ComPDFKit_Logo.ico"))
-            {
-                byte[] byteArray = image.ToByteArray(MagickFormat.Bgra);
-                stampAnnotation.SetImageStamp(byteArray, image.Width, image.Height);
-            }
-            
-            stampAnnotation.SetRect(new CRect(300, 400, 380, 320));
-            stampAnnotation.SetTransparency(255);
-            stampAnnotation.UpdateAp();
+            Bitmap bitmap = new Bitmap("logo.png");
+            CPDFStampAnnotation image = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
+            image.SetImageStamp(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height);
+            image.SetRect(new CRect(300, 320, 380, 400));
+            image.SetTransparency(255);
+            standard.AnnotationRotator.SetRotation(45);
+            image.UpdateAp();
         }
 
 
         private static void CreateLinkAnnotation(CPDFDocument document)
         {
-            CPDFPage page = document.PageAtIndex(0); 
+            CPDFPage page = document.PageAtIndex(0);
             CPDFDestination dest = new CPDFDestination();
             dest.PageIndex = 1;
             CPDFLinkAnnotation link = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINK) as CPDFLinkAnnotation;
@@ -280,20 +301,21 @@ namespace AnnotationTest
         /// <returns></returns>
         static private bool CreateAnnots(CPDFDocument document)
         {
-            CreateFreetextAnnotation(document);
-            CreateFreehandAnnotation(document);
+            //CreateFreetextAnnotation(document);
+            //CreateFreehandAnnotation(document);
             CreateShapeAnnotation(document);
-            CreateNoteAnnotation(document);
-            CreateShapeAnnotation(document);
-            CreateSoundAnnotation(document);
-            CreateMarkupAnnotation(document);
+            //CreateNoteAnnotation(document);
+            //CreateShapeAnnotation(document);
+            //CreateSoundAnnotation(document);
+            //CreateMarkupAnnotation(document);
             CreateStampAnnotation(document);
-            CreateLinkAnnotation(document);
+            //CreateLinkAnnotation(document);
             string path = Path.Combine(outputPath, "CreateAnnotsTest.pdf");
             if (!document.WriteToFilePath(path))
             {
                 return false;
             }
+
             Console.WriteLine("Browse the changed file in " + path);
             return true;
         }
@@ -305,7 +327,7 @@ namespace AnnotationTest
         /// <param name="document"></param>
         /// <returns></returns>
         static private bool DeleteAnnotations(CPDFDocument document)
-        { 
+        {
             CPDFPage page = document.PageAtIndex(0);
 
             List<CPDFAnnotation> annotList = page.GetAnnotations();
@@ -314,7 +336,7 @@ namespace AnnotationTest
                 return false;
             }
 
-            string path = Path.Combine(outputPath , "DeleteAnnotsTest.pdf"); 
+            string path = Path.Combine(outputPath, "DeleteAnnotsTest.pdf");
             if (!document.WriteToFilePath(path))
             {
                 return false;