iText7 ShowTextAligned 与 MoveText

iText7 ShowTextAligned vs MoveText

我正在更新我的 pdf 工具的逻辑,我看到了一些设置。在旧版本中,我有这个 overContent 有一个方法 ShowTextAligned 有五个值。新对象 canvas 有 MoveText 只有两个。下面的 MoveTextShowTextAligned

是否正确

新代码

   float n2 = 15F;
   float n3 = pageSizeWithRotation.GetHeight() - 10F;
   
   canvas.BeginText() 
    .SetFillColorRgb(0,0,0)                                        
    .SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 7F)
    .MoveText(n2, n3)
    .ShowText(disclaimerText)
    .EndText();

旧版本

PdfContentByte overContent = pdfStamper.GetOverContent(i);
overContent.BeginText();
BaseFont baseFont = BaseFont.CreateFont("Helvetica", "Cp1250", false);
overContent.SetFontAndSize(baseFont, 7F);
overContent.SetRGBColorFill(0, 0, 0);
float n2 = 15F;
float n3 = pageSizeWithRotation.Height - 10F;

overContent.ShowTextAligned(0, disclaimerText, n2, n3, 0F);
                                   


                                

我建议您使用 document.ShowTextAligned(),它更易于在 iText 7 中处理文本和段落。

    public virtual void CreatePdf(String dest) {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        // Initialize document
        Document document = new Document(pdf);
        Paragraph title = new Paragraph("The Strange Case of Dr. Jekyll and Mr. Hyde");
        document.ShowTextAligned(title, 36, 806, TextAlignment.LEFT);
        Paragraph author = new Paragraph("by Robert Louis Stevenson");
        document.ShowTextAligned(author, 36, 806, TextAlignment.LEFT, VerticalAlignment.TOP);
        document.ShowTextAligned("Jekyll", 300, 800, TextAlignment.CENTER, 0.5f * (float)Math.PI);
        document.ShowTextAligned("Hyde", 300, 800, TextAlignment.CENTER, -0.5f * (float)Math.PI);
        document.ShowTextAligned("Jekyll", 350, 800, TextAlignment.CENTER, VerticalAlignment.TOP, 0.5f * (float)Math
            .PI);
        document.ShowTextAligned("Hyde", 350, 800, TextAlignment.CENTER, VerticalAlignment.TOP, -0.5f * (float)Math
            .PI);
        document.ShowTextAligned("Jekyll", 400, 800, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0.5f * (float
            )Math.PI);
        document.ShowTextAligned("Hyde", 400, 800, TextAlignment.CENTER, VerticalAlignment.MIDDLE, -0.5f * (float)
            Math.PI);
        document.Close();
    }

请参考linkhttps://kb.itextpdf.com/home/it7kb/examples/itext-7-building-blocks-chapter-2-rootelement-examples和范例编号c02e14_showtextaligned