我正在使用MVC4框架为iTextSharp构建一个上传表单,而我正试图将布尔值转换为按位整数。
iTextSharp方法提供按位使用或组合多个参数,如下所示:
PdfEncryptor.Encrypt(new PdfReader(
ms.ToArray()),
fs,
true,
null,
"Pw",
PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_FILL_IN .......);
但是,我将我的模型定义为使用布尔属性,因此很容易挂钩到webform。
internal static class PermissionConstants
{
public const int NumAllowAssembly = 1024;
public const int NumAllowCopy = 16;
public const int NumAllowDegradedPrinting = 4;
public const int NumAllowFillIn = 256;
public const int NumAllowModifyAnnotations = 32;
public const int NumAllowModifyContents = 8;
public const int NumAllowPrinting = 2052;
public const int NumAllowScreenReaders = 512;
public const int NumHideMenubar = 8192;
public const int NumHideToolbar = 4096;
public const int NumHideWindowUI = 16384;
}
public class Permissions
{
public bool AllowAssembly { get; set; }
public bool AllowCopy { get; set; }
public bool AllowDegradedPrinting { get; set; }
public bool AllowFillIn { get; set; }
public bool AllowModifyAnnotations { get; set; }
public bool AllowModifyContents { get; set; }
public bool AllowPrinting { get; set; }
public bool AllowScreenReaders { get; set; }
//[System.ComponentModel.DefaultValue(true)]
public bool HideMenubar { get; set; }
//[System.ComponentModel.DefaultValue(true)]
public bool HideToolbar { get; set; }
//[System.ComponentModel.DefaultValue(true)]
public bool HideWindowUI { get; set; }
public Permissions()
{
HideMenubar = true;
HideToolbar = true;
HideWindowUI = true;
}
}
现在问题是将Permissions Class中的值发送到第一个方法。我想创建一个生成正确数字的方法。有谁知道如何做到这一点?是否可以只添加整数?我不认为这会产生与按位或操作相同的数字。
答案 0 :(得分:0)
您可以构建一组enum
中“flag”bool
的组合,如下所示:
Permissions p = ...
var res = (p.AllowCopy ? PdfWriter.ALLOW_COPY : 0)
| (p.AllowFillIn ? PdfWriter.ALLOW_FILL_IN : 0)
| // ...and so on.