【问题标题】:ItextSharp horizontal aligning issue with fixed Y cordinates具有固定 Y 坐标的 ItextSharp 水平对齐问题
【发布时间】:2018-12-11 15:52:51
【问题描述】:

我正在使用 ItextSharp lib 编写 pdf。我必须在 pdf 证书模板上打印学生的姓名。我无法将学生姓名与固定的 y 坐标居中对齐。

 private byte[] CreatePDF2(UserCourseViewModel userCourse)
    {
        PdfReader reader = new PdfReader(new RandomAccessFileOrArray(Request.MapPath("/Templates/Certificate.pdf")), null);
        Rectangle size = reader.GetPageSizeWithRotation(1);
        Document doc = new Document(size);

        using (MemoryStream output = new MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(doc, output);
            doc.Open();


            PdfContentByte cb = writer.DirectContent;

            cb.BeginText();
            try
            {
                PdfImportedPage page = writer.GetImportedPage(reader, 1);
                cb.AddTemplate(page,0,0);
                cb.SetFontAndSize(BaseFont.CreateFont(), 24);
                cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Student Name", (size.Width) / 2, 350, 0);
            }
            finally
            {
                cb.EndText();
            }


            doc.Close();


            return output.ToArray();
        }

Y 坐标似乎是正确的,因为 x 坐标肯定不在中心。我无法弄清楚无论学生姓名如何,它都将始终水平居中在证书上。 我对任何其他工具持开放态度,因为我的要求非常简单,我只需要打开 pdf 模板并在课程证书上写下学生姓名即可。 (ITextSharp 对于这个任务似乎太复杂了)

【问题讨论】:

    标签: c# asp.net-mvc itext pdf-generation


    【解决方案1】:

    这对我有用,用于对齐段落。

        cb.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-29
      • 2014-05-18
      • 1970-01-01
      • 2012-09-22
      • 2020-10-22
      • 1970-01-01
      • 2011-01-26
      • 2014-05-27
      相关资源
      最近更新 更多