What value to use for .MoveUp of canvas

点点圈 提交于 2020-12-12 11:56:45

问题


The code below copies all pages from a PDF file to a new file and inserts on the first page a rectangle at the top with a red border holding a short text.

If I don't move it, a gap will be left at the top (here enlarged a lot, font size is 8 only):

However, if I move the rectangle up by an empiric value of 4:

    iText.Kernel.Geom.Rectangle pageSize = firstPage.GetCropBox().MoveUp(4);

there will be a perfect match at the top:

The value 4 is not related to the font size.

I dislike magic numbers in code so, my question is: Why 4? What expression would reveal this value of 4?

The code line is in the first method here. The second is where it is used, and third is called from the first; it just supplies a style:

private static void RegisterDocument(PdfDocument pdfDocument, string registration)
{
    // Magic value to close gap between top of page and top of rectangle with the registration.
    const float moveUp = 4F;

    Document document = new Document(pdfDocument, new PageSize(PageSize.A4));
    PdfPage firstPage = document.GetPdfDocument().GetFirstPage();

    Paragraph paragraph = new Paragraph(registration).AddStyle(RegistrationStyle());

    iText.Kernel.Geom.Rectangle pageSize = firstPage.GetCropBox().MoveUp(moveUp);
    LayoutContext layoutContext = new LayoutContext(new LayoutArea(1, pageSize));
    IRenderer renderer = paragraph.CreateRendererSubTree();
    renderer.SetParent(document.GetRenderer()).Layout(layoutContext);

    Canvas canvas = new Canvas(new PdfCanvas(firstPage, true), pageSize);
    canvas.Add(paragraph);          

    document.Close();   
}

public static void RegisterPdf(string sourceFilename, string targetFilename, string registration)
{
    if (registration.Length > 0)
    {
        // Open source and target PDF files.
        PdfDocument sourcePdf = new PdfDocument(new PdfReader(sourceFilename));
        PdfDocument targetPdf = new PdfDocument(new PdfWriter(targetFilename));

        // Copy all pages from source PDF to target PDF.
        sourcePdf.CopyPagesTo(1, sourcePdf.GetNumberOfPages(), targetPdf);

        // Add registration to page 1 of target and save the document.
        RegisterDocument(targetPdf, registration);

        // Close the files.
        sourcePdf.Close();
        targetPdf.Close();
    }
}

private static Style RegistrationStyle()
{
    // Fixed design values for font and rectangle.
    PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
    const float fontSize = 8F;
    const float rightPadding = 3F;
    TextAlignment textAlignment = TextAlignment.RIGHT;
    iText.Kernel.Colors.Color borderColor = ColorConstants.RED;
    iText.Kernel.Colors.Color fillColor = ColorConstants.WHITE;
    const float borderWidth = 0.7F;

    Style style = new Style()
        .SetFont(font)
        .SetFontSize(fontSize)
        .SetPaddingRight(rightPadding)
        .SetTextAlignment(textAlignment)
        .SetBackgroundColor(fillColor)
        .SetBorder(new SolidBorder(borderColor, borderWidth));

    return style;
}

来源:https://stackoverflow.com/questions/64972425/what-value-to-use-for-moveup-of-canvas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!