1

请参考我在pdf上写值的代码。但我想将数字字段向右对齐。
是否有任何属性/方法。我正在使用此属性PdfFormField.MK_CAPTION_RIGHT,但它不起作用。

var pdfReader = new PdfReader(outputPath);
string fontsfolder1 = @"D:\ItextSharp\Fonts\acmesab.TTF";
var pdfStamper1 = new PdfStamper(pdfReader, new FileStream(outputPath3, FileMode.Create));
BaseFont customfont1 = BaseFont.CreateFont(fontsfolder, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
   AcroFields af = pdfStamper1.AcroFields;
   List<BaseFont> list1 = new List<BaseFont>();
   list.Add(customfont1);
   iTextSharp.text.Font bold1 = new iTextSharp.text.Font(customfont1, 6, 0, BaseColor.BLACK);
   af.SubstitutionFonts = list;
   foreach (var field1 in af.Fields)
   {
            af.SetFieldProperty(field1.Key, "textalignment", PdfFormField.MK_CAPTION_RIGHT, null);
     af.SetField(field1.Key, "123");
   }
     pdfStamper1.FormFlattening = false;
     pdfStamper1.Close();
4

2 回答 2

1

我不知道如何使用 ItextSharp 属性直接执行此操作。但一种方法是在内容左侧添加空格,为此您可以获得字段长度并根据内容的长度和长度,您可以计算要添加到内容左侧的空格。它会自动将您的内容放在PDF字段的右侧。希望这种方式可以帮助您解决问题...

于 2013-02-13T07:48:54.160 回答
1

您可以使用带有对齐参数的 ShowTextAligned 方法对齐文本,如下所示:

cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT,"Text is left aligned",200,800,0);
cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,"Text is right aligned",200,788,0);

cb 在哪里PdfContentByte

详情

于 2013-02-13T07:56:29.067 回答