找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 43912|回复: 16

[问题求助] C#语句转成Powershell代码

[复制链接]
发表于 2022-8-7 10:07:57 | 显示全部楼层 |阅读模式
在下面的网站找到一段将doc转成pdf并加密的代码, C#代码如下
https://www.e-iceblue.cn/conversion/convert-word-to-protected-pdf-using-c.html

自己尝试将C#转成PS代码, 加载和保存都OK了, 只有一行加密参数的代码不知道怎么转, 即红色字部分,
请大佬指导, 多谢!

$doc = New-Object Spire.Doc.Document

$doc.LoadFromFile("$HOME\Desktop\test.docx")

$topdf = New-Object Spire.Doc.ToPdfParameterList
$topdf.PdfSecurity.Encrypt('open','permissions', PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit)

$doc.SaveToFile("$HOME\Desktop\result.pdf", $topdf)
  1. using Spire.Doc;
  2. using Spire.Pdf.Security;

  3. namespace WordToPDFAndEncrypt_PDF
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //加载Word测试文档
  10.             Document doc = new Document();
  11.             doc.LoadFromFile("test.docx");

  12.             //转为PDF时,设置PDF打开密码和权限密码
  13.             ToPdfParameterList topdf = new ToPdfParameterList();
  14.             topdf.PdfSecurity.Encrypt("open", "permission", PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit);
  15.             

  16.             //将文档保存为PDF格式
  17.             doc.SaveToFile("result.pdf", topdf);
  18.             System.Diagnostics.Process.Start("result.pdf");
  19.         }
  20.     }
  21. }
复制代码
发表于 2022-8-7 10:58:29 | 显示全部楼层
按位或操作, 竖线换成-bor试试
PdfPermissionsFlags.Print -bor PdfPermissionsFlags.CopyContent
 楼主| 发表于 2022-8-7 11:12:14 | 显示全部楼层
回复 2# went

谢谢大佬,
改成这样, 还是报错, 用()括起来也不行

$topdf.PdfSecurity.Encrypt('open','permissions', PdfPermissionsFlags.Print -bor PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit)
发表于 2022-8-7 14:06:24 | 显示全部楼层
  1. $doc = New-Object Spire.Doc.Document

  2. $doc.LoadFromFile("$HOME\Desktop\test.docx")

  3. $topdf = New-Object Spire.Doc.ToPdfParameterList
  4. $topdf.PdfSecurity.Encrypt('open','permissions', 'Print,CopyContent', 'Key128Bit')

  5. $doc.SaveToFile("$HOME\Desktop\result.pdf", $topdf)
复制代码

评分

参与人数 1技术 +1 收起 理由
小白龙 + 1 高手!!

查看全部评分

 楼主| 发表于 2022-8-7 20:55:50 | 显示全部楼层
回复 4# flashercs


  大佬, 又遇到一个C#奇怪的转换问题, 下面的红字部分,


//创建Word文档1,加载源文档
Document doc1 = new Document();
doc1.LoadFromFile("test.docx");

//创建一个空白文档,作为复制内容的目标文档
Document doc2 = new Document();

//获取Word文档1第一节的第2段和第3段
Section s = doc1.Sections[0];
Paragraph p1 = s.Paragraphs[1];
Paragraph p2 = s.Paragraphs[2];

//在Word文档2中添加Section,并将文档1中的第2、3段的内容复制到文档2中
Section s1 = doc2.AddSection();
Paragraph NewPara1 = (Paragraph)p1.Clone();
s1.Paragraphs.Add(NewPara1);
Paragraph NewPara2 = (Paragraph)p2.Clone();
s1.Paragraphs.Add(NewPara2);

//保存文档
doc2.SaveToFile("output.docx", FileFormat.Docx2010);
发表于 2022-8-8 11:25:05 | 显示全部楼层
回复 5# 小白龙
  1. $NewPara1 = $p1.Clone()
复制代码
 楼主| 发表于 2022-8-8 16:40:01 | 显示全部楼层
本帖最后由 小白龙 于 2022-8-8 17:05 编辑

回复 6# flashercs

大佬真是神了, 无所不通! 感谢!

我对C#刚懂点皮毛, 能再请教个小问题吗?
最下面的C#代码是能输出的, 但是函数的返回值是void, 从Console输出的, 我想将返回值设为string, 改为了如下, 却没有任何输出

public static string Execute(string command)
{
        var ps = PowerShell.Create());

        var results = ps.AddScript(command).Invoke();
       
        return results.ToString();
}
  1. public static void Execute(string command)
  2. {
  3.         var ps = PowerShell.Create());

  4.         var results = ps.AddScript(command).Invoke();
  5.         foreach (var result in results)
  6.         {
  7.                 Console.WriteLine(result.ToString());
  8.         }
  9. }
复制代码
发表于 2022-8-8 17:04:34 | 显示全部楼层
回复 7# 小白龙


    调试,出错截图.
 楼主| 发表于 2022-8-8 17:07:38 | 显示全部楼层
回复 8# flashercs


大佬, 是没有任何输出, 不报错
是这个链接的代码:
https://blog.51cto.com/u_15242344/4895426
 楼主| 发表于 2022-8-8 20:13:15 | 显示全部楼层
回复 8# flashercs

var results = ps.AddScript(command).Invoke();
foreach (var result in results)
{
        Console.WriteLine(result.ToString());
}


上面红色字这样就有输出, 下面改成蓝色字这样, 我加了return, 函数输出类型那里从void改为了string, 就没有输出了
感觉应该是下面第二行 tostring()的问题, 对象可能不支持转字符串, 但是我不知道怎么改

var results = ps.AddScript(command).Invoke();
return results.ToString();
发表于 2022-8-8 21:00:49 | 显示全部楼层
试试介样子:

  1. public static string[] Execute1(string command){
  2.                         var ps = PowerShell.Create();
  3.                         var results = ps.AddScript(command).Invoke();

  4.                         return results.ToList().Foreach(x => $"{x}").ToArray();
  5.                 }
复制代码
发表于 2022-8-8 21:08:23 | 显示全部楼层
或这样:

  1. public static string Execute2(string command){
  2.         var ps = PowerShell.Create();
  3.        var results = ps.AddScript(command).Invoke();
  4.        return results.FirstOrDefault().ToString();
  5. }
复制代码
 楼主| 发表于 2022-8-8 22:12:50 | 显示全部楼层
回复 12# xczxczxcz

感谢大佬帮助, 上面第二个好像可以了, 第一个好像不行, 我的是 .net 4.8
 楼主| 发表于 2022-8-8 23:01:20 | 显示全部楼层
本帖最后由 小白龙 于 2022-8-8 23:04 编辑

回复 12# xczxczxcz

大佬好, 试了好长时间, 我用下面的PS代码生成DLL, 一执行就报错, 用红色代码测试时是对的, 但是生成不了DLL, 用蓝色代码,测试时是错的, 但是能生成DLL
希望大佬帮忙, 谢谢

Add-Type -Path @(
        ".\System.Management.Automation.dll"
)
Add-Type -TypeDefinition @"
using System.Management.Automation;

public class Code
{
        public static string RunPs(string command)
        {
                using (var ps = PowerShell.Create())
                {
                        var results = ps.AddScript(command).Invoke();
                        return results.FirstOrDefault().ToString(); //测试时,结果是对的,但不能生成dll,报错
                        //return results.ToString(); //测试时,结果是错的, 但是能生成dll
                }
        }
}
"@ -outputType library -outputAssembly "code.dll"
 楼主| 发表于 2022-8-8 23:02:23 | 显示全部楼层
报错如下:
Add-Type : c:\Users\Administrator\AppData\Local\Temp\opdun1nt\opdun1nt.0.cs(10) : 'System.Collections.ObjectModel.Collection<System.Management.Automation.PSObj
ect>' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Collections.Object
Model.Collection<System.Management.Automation.PSObject>' could be found (are you missing a using directive or an assembly reference?)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 07:47 , Processed in 0.024022 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表