|
@@ -23,6 +23,8 @@ using System.Windows;
|
|
|
using ComPDFKitViewer;
|
|
|
using Size = System.Windows.Size;
|
|
|
using System.Diagnostics;
|
|
|
+using System.Windows.Controls;
|
|
|
+using ComPDFKit.Import;
|
|
|
|
|
|
namespace PDF_Office.ViewModels.EditTools.Watermark
|
|
|
{
|
|
@@ -34,6 +36,8 @@ namespace PDF_Office.ViewModels.EditTools.Watermark
|
|
|
public CPDFViewer PDFViewer;
|
|
|
public CPDFDocument Document;
|
|
|
private CPDFWatermark watermark;
|
|
|
+
|
|
|
+ private CPDFBackground background;
|
|
|
private WatermarkInfo watermarkInfo;
|
|
|
|
|
|
|
|
@@ -63,79 +67,75 @@ namespace PDF_Office.ViewModels.EditTools.Watermark
|
|
|
public void SetWatermark(WatermarkInfo watermarkInfo)
|
|
|
{
|
|
|
this.watermarkInfo = watermarkInfo;
|
|
|
- // CreateWatermark(PDFViewer.Document);
|
|
|
- PDFViewer.Document.PdfToImage("0-1", "C:\\Users\\kdanmobile\\Desktop\\888");
|
|
|
- PDFViewer.Document.WriteToFilePath("C:\\Users\\kdanmobile\\Desktop\\888\\sb22222.pdf");
|
|
|
- PDFViewer.InvalidateVisual();
|
|
|
+ //CreateWatermark(PDFViewer.Document);
|
|
|
+ //PDFViewer.Document.PdfToImage("0-1", "C:\\Users\\kdanmobile\\Desktop\\888\\");
|
|
|
+ //PDFViewer.Document.WriteToFilePath("C:\\Users\\kdanmobile\\Desktop\\888\\sb22222.pdf");
|
|
|
+ PDFViewer.InvalidChildVisual(false);
|
|
|
}
|
|
|
|
|
|
- public void CreateWatermark(CPDFDocument document)
|
|
|
+ public void CreateWatermark(CPDFDocument document, bool IsNewDoc = false)
|
|
|
{
|
|
|
- if (watermarkInfo != null)
|
|
|
- {
|
|
|
- document.DeleteWatermarks();
|
|
|
- if (watermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT)
|
|
|
- {
|
|
|
- watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
|
|
|
- watermark.SetText(watermarkInfo.Text);
|
|
|
- if (watermarkInfo.TextSize == null)
|
|
|
- {
|
|
|
- watermarkInfo.TextSize = "6";
|
|
|
- }
|
|
|
- watermark.SetFontName(watermarkInfo.FontName);
|
|
|
- watermark.SetFontSize(float.Parse(watermarkInfo.TextSize));
|
|
|
- watermark.SetTextRGBColor(watermarkInfo.TextColor);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
-
|
|
|
- watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
|
|
|
- if (watermarkInfo.ImageArray != null)
|
|
|
- {
|
|
|
- watermark.SetImage(watermarkInfo.ImageArray, watermarkInfo.ImageWidth, watermarkInfo.ImageHeight);
|
|
|
- }
|
|
|
- watermark.SetScale(0.1f);
|
|
|
- }
|
|
|
- watermark.SetRotation(watermarkInfo.Rotation);
|
|
|
- watermark.SetOpacity(watermarkInfo.Opacity);
|
|
|
- watermark.SetFront(watermarkInfo.IsFront);
|
|
|
- watermark.SetVertalign(watermarkInfo.WatermarkVertalign);
|
|
|
- watermark.SetHorizalign(watermarkInfo.WatermarkHorizalign);
|
|
|
- watermark.SetFullScreen(watermarkInfo.Isfull);
|
|
|
- watermark.SetVertOffset(watermarkInfo.VertOffset);
|
|
|
- watermark.SetHorizOffset(watermarkInfo.HorizOffset);
|
|
|
- watermark.SetHorizontalSpacing(watermarkInfo.HorizontalSpacing);
|
|
|
- watermark.SetVerticalSpacing(watermarkInfo.VerticalSpacing);
|
|
|
- watermark.SetPages(watermarkInfo.PageRange);
|
|
|
- watermark.CreateWatermark();
|
|
|
- watermark.UpdateWatermark();
|
|
|
- }
|
|
|
+ if (watermarkInfo != null) {
|
|
|
+ background = document.GetBackground();
|
|
|
+
|
|
|
+ background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
|
|
|
+
|
|
|
+ background.SetScale(1);
|
|
|
+ background.SetColor(watermarkInfo.TextColor);
|
|
|
+
|
|
|
+ background.SetRotation(0);
|
|
|
+ background.SetOpacity(255);
|
|
|
+ background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
|
|
|
+ background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
|
|
|
+ background.SetYOffset(0);
|
|
|
+ background.SetXOffset(0);
|
|
|
+ background.SetAllowsPrint(true);
|
|
|
+ background.SetAllowsView(true);
|
|
|
+ background.SetPages("0");
|
|
|
+ background.Update();
|
|
|
}
|
|
|
-
|
|
|
- public BitmapSource ToBitmapSource(System.Drawing.Bitmap image)
|
|
|
- {
|
|
|
- IntPtr ptr = image.GetHbitmap();//obtain the Hbitmap
|
|
|
- BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
|
|
|
- (
|
|
|
- ptr,
|
|
|
- IntPtr.Zero,
|
|
|
- Int32Rect.Empty,
|
|
|
- System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
|
|
|
- );
|
|
|
-
|
|
|
- return bs;
|
|
|
+ //if (watermarkInfo != null)
|
|
|
+ //{
|
|
|
+ // document.DeleteWatermarks();
|
|
|
+ // if (watermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT)
|
|
|
+ // {
|
|
|
+ // watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
|
|
|
+ // watermark.SetText(watermarkInfo.Text);
|
|
|
+ // if (watermarkInfo.TextSize == null)
|
|
|
+ // {
|
|
|
+ // watermarkInfo.TextSize = "6";
|
|
|
+ // }
|
|
|
+ // watermark.SetFontName(watermarkInfo.FontName);
|
|
|
+ // watermark.SetFontSize(float.Parse(watermarkInfo.TextSize));
|
|
|
+ // watermark.SetTextRGBColor(watermarkInfo.TextColor);
|
|
|
+ // }
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+
|
|
|
+ // watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
|
|
|
+ // if (watermarkInfo.ImageArray != null)
|
|
|
+ // {
|
|
|
+ // watermark.SetImage(watermarkInfo.ImageArray, watermarkInfo.ImageWidth, watermarkInfo.ImageHeight);
|
|
|
+ // }
|
|
|
+ // watermark.SetScale(0.1f);
|
|
|
+ // }
|
|
|
+ // watermark.SetRotation(watermarkInfo.Rotation);
|
|
|
+ // watermark.SetOpacity(watermarkInfo.Opacity);
|
|
|
+ // watermark.SetFront(watermarkInfo.IsFront);
|
|
|
+ // watermark.SetVertalign(watermarkInfo.WatermarkVertalign);
|
|
|
+ // watermark.SetHorizalign(watermarkInfo.WatermarkHorizalign);
|
|
|
+ // watermark.SetFullScreen(watermarkInfo.Isfull);
|
|
|
+ // watermark.SetVertOffset(watermarkInfo.VertOffset);
|
|
|
+ // watermark.SetHorizOffset(watermarkInfo.HorizOffset);
|
|
|
+ // watermark.SetHorizontalSpacing(watermarkInfo.HorizontalSpacing);
|
|
|
+ // watermark.SetVerticalSpacing(watermarkInfo.VerticalSpacing);
|
|
|
+ // if (IsNewDoc) { watermark.SetPages("0"); }
|
|
|
+ // else { watermark.SetPages(watermarkInfo.PageRange); }
|
|
|
+ // watermark.CreateWatermark();
|
|
|
+ // watermark.UpdateWatermark();
|
|
|
+ //}
|
|
|
}
|
|
|
|
|
|
- public async Task RenderBitmap(CPDFDocument doc)
|
|
|
- {
|
|
|
-
|
|
|
- CPDFPage page = doc.PageAtIndex(0, true);
|
|
|
- byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
|
|
|
- Bitmap bitmap = await ToolMethod.RenderPageBitmap(Document, (int)(page.PageSize.Width * 1 * 1.4), (int)(page.PageSize.Height * 1 * 1.4), 0, true, true);
|
|
|
- ImageSource = ToBitmapSource(bitmap);
|
|
|
- doc.ReleasePages();
|
|
|
-
|
|
|
- }
|
|
|
|
|
|
private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
|
|
|
{
|
|
@@ -145,10 +145,27 @@ namespace PDF_Office.ViewModels.EditTools.Watermark
|
|
|
{
|
|
|
if (e.DrawPages.Count > 0 && e.DrawContext != null)
|
|
|
{
|
|
|
+ List<int> PageIndexLists = new List<int>();
|
|
|
foreach (DrawPageData drawPageData in e.DrawPages)
|
|
|
- {
|
|
|
- WriteableBitmap waterMarkBitmap = GetWaterMark(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
|
|
|
- e.DrawContext.DrawImage(waterMarkBitmap, drawPageData.PageBound);
|
|
|
+ {
|
|
|
+ if(watermarkInfo != null) {
|
|
|
+ char[] enumerationSeparator = new char[] { ',' };
|
|
|
+ char[] rangeSeparator = new char[] { '-' };
|
|
|
+ if (CommonHelper.GetPagesInRange(ref PageIndexLists, watermarkInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true))
|
|
|
+ { //TODO
|
|
|
+ Trace.WriteLine(CommonHelper.GetPagesInRange(ref PageIndexLists, watermarkInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true));
|
|
|
+ Trace.WriteLine(drawPageData.PageIndex);
|
|
|
+ foreach (var abbb in PageIndexLists ) {
|
|
|
+
|
|
|
+ Trace.WriteLine("smgui _______"+abbb);
|
|
|
+ }
|
|
|
+ if (PageIndexLists.Contains(drawPageData.PageIndex-1)) {
|
|
|
+
|
|
|
+ WriteableBitmap waterMarkBitmap = GetWaterMark(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
|
|
|
+ e.DrawContext.DrawImage(waterMarkBitmap, drawPageData.PageBound);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -158,14 +175,12 @@ namespace PDF_Office.ViewModels.EditTools.Watermark
|
|
|
|
|
|
CPDFDocument newDoc = CPDFDocument.CreateDocument();
|
|
|
newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null);
|
|
|
-
|
|
|
- CreateWatermark(newDoc);
|
|
|
+ CreateWatermark(newDoc,true);
|
|
|
CPDFPage newPage = newDoc.PageAtIndex(0);
|
|
|
double scale = 96.0 / 72.0;
|
|
|
zoom = zoom * scale;
|
|
|
Rect renderRect = new Rect(0, 0, (int)(pageSize.Width * scale), (int)(pageSize.Height * scale));
|
|
|
byte[] imageArray = new byte[(int)(renderRect.Width * renderRect.Height * 4)];
|
|
|
-
|
|
|
newPage.RenderPageBitmapWithMatrix((float)zoom, renderRect, 0x00FFFFFF, imageArray, 1, true);
|
|
|
WriteableBitmap WirteBitmap = new WriteableBitmap((int)renderRect.Width, (int)renderRect.Height, 96, 96, PixelFormats.Bgra32, null);
|
|
|
WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)renderRect.Width, (int)renderRect.Height), imageArray, WirteBitmap.BackBufferStride, 0);
|