123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- using ComPDFKit.Import;
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKit.Tool.DrawTool;
- using ComPDFKit.Viewer.Helper;
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using System.Windows.Media;
- using System.IO;
- namespace ComPDFKit.Tool.Help
- {
- public class PDFHelp
- {
- public static List<TextDrawRect> GetSelectTextRect(CPDFDocument document, int pageIndex, Point startPoint, Point endPoint, Point tolerance)
- {
- CPDFPage page = document.PageAtIndex(pageIndex);
- if (page != null)
- {
- CPDFTextPage textPage = page.GetTextPage();
- if (textPage != null)
- {
- List<CRect> selectList = textPage.GetCharsRectAtPos(
- DataConversionForWPF.PointConversionForCPoint(startPoint),
- DataConversionForWPF.PointConversionForCPoint(endPoint),
- new CPoint(10, 10)
- );
- List<TextDrawRect> drawList = new List<TextDrawRect>();
- foreach (CRect rawRect in selectList)
- {
- drawList.Add(new TextDrawRect() { DrawRect = DataConversionForWPF.CRectConversionForRect( rawRect), Text = "" });
- }
- return drawList;
- }
- }
- return new List<TextDrawRect>();
- }
-
- public static string GetSelectText(CPDFDocument document, int pageIndex, Point startPoint, Point endPoint,Point tolerance)
- {
- if (document != null)
- {
- CPDFPage page = document.PageAtIndex(pageIndex);
- if (page != null && page.IsValid())
- {
- CPDFTextPage textPage = page.GetTextPage();
- if (textPage != null && textPage.IsValid())
- {
- return textPage.GetSelectText(
- DataConversionForWPF.PointConversionForCPoint(startPoint),
- DataConversionForWPF.PointConversionForCPoint(endPoint),
- DataConversionForWPF.PointConversionForCPoint(tolerance)
- );
- }
- }
- }
- return string.Empty;
- }
- public static string GetDoubleClickText(CPDFDocument document, int pageIndex, Point startPoint, ref Rect uiRect)
- {
- CPDFPage page = document.PageAtIndex(pageIndex);
- if (page != null)
- {
- CPDFTextPage textPage = page.GetTextPage();
- if (textPage != null)
- {
- CRect wordRect = new CRect();
- string Word = textPage.GetSelectionForWordAtPos(
- DataConversionForWPF.PointConversionForCPoint(startPoint),
- new CPoint(5, 5), ref wordRect);
- uiRect= DataConversionForWPF.CRectConversionForRect(wordRect);
- return Word;
- }
- }
- return string.Empty;
- }
- public static string GetCurrentPdfTime()
- {
- DateTime localTime = DateTime.Now;
- DateTime utcTime = DateTime.UtcNow;
- int checkHour = (localTime - utcTime).Hours;
- if (checkHour > 0)
- {
- return "D:" + localTime.ToString("yyyyMMddHHmmss") + "+" + checkHour.ToString("D2") + "'00'";
- }
- if (checkHour < 0)
- {
- return "D:" + localTime.ToString("yyyyMMddHHmmss") + "-" + Math.Abs(checkHour).ToString("D2") + "'00'";
- }
- return "D:" + localTime.ToString("yyyyMMddHHmmss") + "Z00'00'";
- }
- public static bool IsTextAtPos(CPDFDocument document, int pageIndex, Point pagePoint)
- {
- if (document==null)
- {
- return false;
- }
- CPDFPage page = document.PageAtIndex(pageIndex);
- if (page == null)
- {
- return false;
- }
- CPDFTextPage textPage = page.GetTextPage();
- if (textPage == null)
- {
- return false;
- }
- var x = textPage.GetCharIndexAtPos(
- DataConversionForWPF.PointConversionForCPoint(pagePoint),
- new CPoint(10, 10)
- );
- return x != -1;
- }
- public static void ImagePathToByte(string imagePath, ref byte[] imageData, ref int imageWidth, ref int imageHeight)
- {
- if (!File.Exists(imagePath))
- return;
- imagePath = Path.GetFullPath(imagePath);
- BitmapFrame frame = null;
- BitmapDecoder decoder = BitmapDecoder.Create(new Uri(imagePath), BitmapCreateOptions.None, BitmapCacheOption.Default);
- if (decoder != null && decoder.Frames.Count > 0)
- {
- frame = decoder.Frames[0];
- }
- if (frame != null)
- {
- imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
- if (frame.Format != PixelFormats.Bgra32)
- {
- FormatConvertedBitmap covert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0);
- covert.CopyPixels(imageData, frame.PixelWidth * 4, 0);
- }
- else
- {
- frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
- }
- imageWidth = frame.PixelWidth;
- imageHeight = frame.PixelHeight;
- }
- }
- public static void ImageStreamToByte(Stream imageStream, ref byte[] imageData, ref int imageWidth, ref int imageHeight)
- {
- imageStream.Seek(0, SeekOrigin.Begin);
- BitmapDecoder decoder = BitmapDecoder.Create(imageStream,BitmapCreateOptions.None,BitmapCacheOption.Default);
- BitmapFrame frame = null;
- if (decoder != null && decoder.Frames.Count > 0)
- {
- frame = decoder.Frames[0];
- }
- if (frame != null)
- {
- imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
- if (frame.Format != PixelFormats.Bgra32)
- {
- FormatConvertedBitmap covert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0);
- covert.CopyPixels(imageData, frame.PixelWidth * 4, 0);
- }
- else
- {
- frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
- }
- imageWidth = frame.PixelWidth;
- imageHeight = frame.PixelHeight;
- }
- }
- }
- public static class PDFDateHelp
- {
- /// <summary>
- /// Gets image for the appearance stream of the text stamp.
- /// </summary>
- /// <param name="stampText">Text</param>
- /// <param name="date">Date</param>
- /// <param name="shape">Shape</param>
- /// <param name="color">Color</param>
- /// <param name="stampWidth">Width of stamp</param>
- /// <param name="stampHeight">Height of stamp</param>
- /// <param name="imageWidth">Width of image</param>
- /// <param name="imageHeight">Height of image</param>
- /// <param name="rotation">Rotation angle</param>
- /// <returns>Image data</returns>
- public static byte[] GetTempTextStampImage(string stampText, string date, C_TEXTSTAMP_SHAPE shape, C_TEXTSTAMP_COLOR color, out int stampWidth, out int stampHeight, out int imageWidth, out int imageHeight, int rotation = 0)
- {
- CPDFDocument doc = CPDFDocument.CreateDocument();
- string Path = null;
- doc.InsertPage(0, 800, 1200, Path);
- CPDFPage page = doc.PageAtIndex(0, false);
- CPDFStampAnnotation stampAnnot = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
- stampAnnot.SetTextStamp(stampText, date, shape, color, rotation);
- stampAnnot.UpdateAp();
- var flags = BindingFlags.NonPublic | BindingFlags.Static;
- var dpiProperty = typeof(SystemParameters).GetProperty("Dpi", flags);
- int dpi = (int)dpiProperty.GetValue(null, null);
- CRect rect = stampAnnot.GetRect();
- stampWidth = (int)rect.width();
- stampHeight = (int)rect.height();
- imageWidth = (int)(rect.width() * dpi / 72D * 2);
- imageHeight = (int)(rect.height() * dpi / 72D * 2);
- byte[] imageData = new byte[imageWidth * imageHeight * 4];
- stampAnnot.RenderAnnot(imageWidth, imageHeight, imageData);
- stampAnnot.ReleaseAnnot();
- doc.ReleasePages(0);
- doc.Release();
- return imageData;
- }
- }
- }
|