【问题标题】:how to align numeric filed to right using itextsharp(pdfstamper)如何使用 itextsharp(pdfstamper) 将数字字段右对齐
【发布时间】:2013-01-28 16:45:39
【问题描述】:

请参考我在 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();

【问题讨论】:

    标签: c# pdf alignment itextsharp


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您可以使用带有对齐参数的 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

      For Detail

      【讨论】:

      • 感谢您的回复....正如我看到他们使用 writer 的链接...但在这里我使用 Pdfstamper...如何将此属性与 pdfstamper 一起使用?
      猜你喜欢
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      相关资源
      最近更新 更多