Trying to copy source pdf to destination at specific Y position

孤人 提交于 2019-12-24 07:17:52

问题


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.

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?

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.

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.

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

Here is the method..

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;
    }
}

回答1:


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

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.

This actually is quite natural:

  • 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.

  • 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:

    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.


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.

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.



来源:https://stackoverflow.com/questions/57316944/trying-to-copy-source-pdf-to-destination-at-specific-y-position

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