尝试将源pdf复制到特定Y位置的目标 [英] Trying to copy source pdf to destination at specific Y position

查看:106
本文介绍了尝试将源pdf复制到特定Y位置的目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试一种方法,该方法将从一个名为55PREMIUMPAYMENTWARRANTY.pdf的现有pdf中读取,该文件顶部有几个小段,每段3行.然后,我尝试使用画布将其复制到其他Y位置的新文档中.

I am testing a method which reads from an existing pdf called 55PREMIUMPAYMENTWARRANTY.pdf which has a couple of small paragraphs 3 lines each at the top. I am then trying to copy it to a new document at a different Y position using the canvas.

我在具有不同位置的循环中调用了此方法,并对结果感到惊讶.

I have called this method in a loop with various positions and surprised at the results.

Y位置通常从左下角的0开始,但是如果Y值为负,则仅在新页面上显示.为什么?

The Y position usually starts at 0 at the bottom left, yet it only shows on the new page if the Y value is a negative.. why is that?

通常,如果我只是写纯文本,则Y值400大约在A4页面的中间,即595 x842.

Usually if I was just writing plain text the Y value of 400 would be roughly in the middle of an A4 page which is 595 x 842.

但是在这里,如果我想在中间显示它,我需要将Y设置为-300左右,这对我来说是没有意义的.

But here if I want to show it in the middle I need to set the Y to around -300, which makes no sense to me.

设置位置的行是 canvas.AddXObject(pageCopy,0,position);

The line that sets the position is canvas.AddXObject(pageCopy, 0, position);

这是方法.

public static byte[] WritePPWToPosition(float position)
{
    try
    {
        //write PPW to different positions on the pdf

        var link = "D:\\Repo\\website3.0\\LeisureInsure\\Content\\CertificateDocuments\\55PREMIUMPAYMENTWARRANTY.pdf";
        byte[] buffer;
        using (Stream stream = new FileStream(@link, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            buffer = new byte[stream.Length - 1];
            stream.Read(buffer, 0, buffer.Length);
        }

        using (var ms = new MemoryStream())
        {                    
            //read source page
            var readerSource = new PdfReader(new MemoryStream(buffer));
            PdfDocument sourcePdf = new PdfDocument(readerSource);                                     
            PdfPage sourcePage = sourcePdf.GetPage(1);
            //create destination page
            PdfDocument newpdf = new PdfDocument(new PdfWriter(ms));
            PageSize a4Page = PageSize.A4;
            PdfPage newpage = newpdf.AddNewPage(a4Page);
            PdfCanvas canvas = new PdfCanvas(newpage);
            //copy source page to destination page
            PdfFormXObject pageCopy = sourcePage.CopyAsFormXObject(newpdf);                    
            //add destination page to canvas at position
            canvas.AddXObject(pageCopy, 0, position);
            sourcePdf.Close();
            newpdf.Close();

            var result = ms.ToArray();
            return result;
        }
    }
    catch (Exception ex)
    {
        throw;
    }
}

推荐答案

通常,如果我只是写纯文本,则Y值400大约在A4页面的中间.

Usually if I was just writing plain text the Y value of 400 would be roughly in the middle of an A4 page.

但是在这里,如果我想在中间显示它,我需要将Y设置为-300左右,这对我来说是没有意义的.

But here if I want to show it in the middle I need to set the Y to around -300, which makes no sense to me.

这实际上是很自然的:

  • 如果要放置文本,则必须使用该文本的基线起始位置的坐标.对于垂直居中的文本,它将位于页面区域内 内,明显位于页面底部上方.

  • If you want to position text, you have to use the coordinates of where the baseline of that text shall start. For vertically centered text this will be somewhere inside the page area, clearly above the page bottom.

如果要放置表单XObject,则必须使用该XObject的左下角所在的坐标.要将内容垂直放在目标页面上页面大小的表单XObject顶部的中心,该左下角将位于页面区域下方的某处:

If you want to position a form XObject, you have to use the coordinates of where the lower left corner of that XObject shall be. To vertically center content at the top of a page-sized form XObject on a target page, this lower left corner will be somewhere below the page area:

显然,源页面的左下角应该位于目标页面底部的 下.通常,目标页面的坐标系的原点位于左下角,这意味着该位置的 y 坐标为负.

Clearly the lower left corner of the source page shall be positioned beneath the bottom of the target page. As usually the coordinate system of the target page has its origin in its lower left corner, this implies that the y coordinate of that position is negative.

严格来说,以上所述只是一种简化,因为通常您不会看到XObject形式的左下角应该去的地方,而是XObject的 origin 的去处. XObject坐标系就可以了.

Strictly speaking the above said is a simplification because in general you don't look where the lower left corner of the form XObject shall go but instead where the origin of the XObject coordinate system shall go.

但是,如果从某个源页面创建表单XObject,则原始页面坐标系的原点(通常在其左下角)将成为XObject的原点.因此,通常上述说法是正确的.但是,对于通用解决方案,您将不得不考虑XObject边界框的实际值.

But in case of a form XObject created from some source page, the origin of the coordinate system of the original page (usually in its lower left corner) becomes the origin of the XObject. Thus, usually the above said is correct. For a generic solution, though, you will have to consider the actual value of the XObject bounding box.

这篇关于尝试将源pdf复制到特定Y位置的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆