0

我正在使用 MVC4 框架为 iTextSharp 构建一个上传表单,但我在尝试将布尔值转换为按位整数时遇到了困难。

iTextSharp 提供的方法使用按位或组合多个参数,如下所示:

    PdfEncryptor.Encrypt(new PdfReader(
                    ms.ToArray()),
                    fs,
                    true, 
                    null, 
                    "Pw", 
                    PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_FILL_IN .......);

但是,我将我的模型定义为使用布尔属性,以便轻松连接到网络表单。

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 类发送到第一个方法。我想做一个生成正确数字的方法。有谁知道如何做到这一点?是否可以只添加整数?我认为这不会产生与按位或运算相同的数字。

4

1 回答 1

0

You can construct a combination of "flag" enums from a set of bools as follows:

Permissions p = ...
var res = (p.AllowCopy   ? PdfWriter.ALLOW_COPY : 0)
        | (p.AllowFillIn ? PdfWriter.ALLOW_FILL_IN : 0)
        | // ...and so on.
于 2013-09-12T15:02:15.237 回答