Aspose.word保存PDF时进行授权访问设置

来源:互联网 发布:lte双流占比优化 编辑:程序博客网 时间:2024/05/21 19:50
  1. public void ConvertDocToPDF(string docPath,string savePdfPath)  
  2.       {  
  3.           Aspose.Words.Document wordDocument = new Aspose.Words.Document(docPath);  
  4.           InsertWatermarkText(wordDocument, "内部资料 注意保密\r\n XX公司  ");  
  5.           wordDocument.Save("D:\\abc.doc");  
  6.           Aspose.Words.Saving.PdfSaveOptions saveOption = new Aspose.Words.Saving.PdfSaveOptions();  
  7.           saveOption.SaveFormat = Aspose.Words.SaveFormat.Pdf;  
  8. //user pass 设置了打开时,需要密码   
  9.               //owner pass 控件编辑等权限  
  10.           PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails(string.Empty, "PasswordHere", PdfEncryptionAlgorithm.RC4_128);  
  11.           encryptionDetails.Permissions = PdfPermissions.DisallowAll;  
  12.   
  13.   
  14.           saveOption.EncryptionDetails = encryptionDetails;  
  15.           wordDocument.Save(savePdfPath, saveOption);  
  16.       }  
  17.   
  18.       private static void InsertWatermarkText(Aspose.Words.Document doc, string watermarkText)  
  19.       {  
  20.           // Create a watermark shape. This will be a WordArt shape.   
  21.           // You are free to try other shape types as watermarks.  
  22.           Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);  
  23.   
  24.           // Set up the text of the watermark.  
  25.           watermark.TextPath.Text = watermarkText;  
  26.           watermark.TextPath.FontFamily = "宋体";  
  27.           watermark.Width = 500;  
  28.           watermark.Height = 100;  
  29.           // Text will be directed from the bottom-left to the top-right corner.  
  30.           watermark.Rotation = -40;  
  31.           // Remove the following two lines if you need a solid black text.  
  32.           watermark.Fill.Color = System.Drawing.Color.Gray; // Try LightGray to get more Word-style watermark  
  33.           watermark.StrokeColor = System.Drawing.Color.Gray; // Try LightGray to get more Word-style watermark  
  34.   
  35.           // Place the watermark in the page center.  
  36.           watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;  
  37.           watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;  
  38.           watermark.WrapType = WrapType.None;  
  39.           watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center;  
  40.           watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;  
  41.   
  42.           // Create a new paragraph and append the watermark to this paragraph.  
  43.           Aspose.Words.Paragraph watermarkPara = new Aspose.Words.Paragraph(doc);  
  44.           watermarkPara.AppendChild(watermark);  
  45.           foreach (Section sect in doc.Sections)  
  46.           {  
  47.               // There could be up to three different headers in each section, since we want  
  48.               // the watermark to appear on all pages, insert into all headers.  
  49.               InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);  
  50.               InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);  
  51.               InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);  
  52.           }  
  53.   
  54.   
  55.       }  
  56.   
  57.       private static void InsertWatermarkIntoHeader(Aspose.Words.Paragraph watermarkPara, Section sect, HeaderFooterType headerType)  
  58.       {  
  59.           Aspose.Words.HeaderFooter header = sect.HeadersFooters[headerType];  
  60.   
  61.           if (header == null)  
  62.           {  
  63.               // There is no header of the specified type in the current section, create it.  
  64.               header = new Aspose.Words.HeaderFooter(sect.Document, headerType);  
  65.               sect.HeadersFooters.Add(header);  
  66.           }  
  67.   
  68.           // Insert a clone of the watermark into the header.  
  69.           header.AppendChild(watermarkPara.Clone(true));  
  70.       }  
以上代码,主要实现添加水印及生成PDF时加上安全设置
0 0
原创粉丝点击