首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导入PDF位置PDFStamper

导入PDF位置PDFStamper
EN

Stack Overflow用户
提问于 2013-03-06 17:32:10
回答 1查看 1.8K关注 0票数 2

我现在迷路了。我试图完成的是在一个PDF上添加另一个(就像一个水印)。问题是我似乎不理解所使用的坐标系,因为我的水印表现得出乎意料。

这两个PDF的尺寸不同。

我的目标有以下维度:

595高

842宽度

要添加的PDF具有以下维度:

41高

552宽度

在我的代码中,我做了以下事情:

代码语言:javascript
复制
public bool AddPdf(ref PdfReader pdfSource, ref PdfReader pdfTarget, ref FileStream destination)
    {
        PdfStamper stamper = null;
        try
        {
            stamper = new PdfStamper( pdfSource, destination );
            PdfImportedPage importatedPage = stamper.GetImportedPage(pdfTarget, 1);

            PdfContentByte background;
            for (int iPage = 1; iPage <= pdfSource.NumberOfPages; iPage++)
            {
                background = stamper.GetOverContent(iPage);                    
                background.AddTemplate(importatedPage, 0, 0 + importHeight);
            }
        }

当我这样做的时候,我希望我的水印会出现在左下角。相反,它在页面的某个地方(我没有看到它)。只是为了测试,我将600硬编码为y位置,然后它在页面上垂直居中。

有人能给我点小费吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-07 19:58:52

所以我解决了这个问题。问题是sourcepdf有一个裁剪框--我只需要用这个信息纠正我的x和y位置:

代码语言:javascript
复制
            PdfStamper stamper = null;
            try
            {
            stamper = new PdfStamper(pdfSource, destination);
            PdfImportedPage importatedPage = stamper.GetImportedPage(pdfTarget, 1);
            PdfContentByte background;
            for (int iPage = 1; iPage <= pdfSource.NumberOfPages; iPage++)
            {
                background = stamper.GetOverContent(iPage);

                // here comes the important part
                Rectangle cropBox = pdfSource.GetCropBox(iPage);

                float xCorrected = 0 + cropBox.Left;
                float yCorrected = 0 + cropBox.Bottom;

                background.AddTemplate(importatedPage, xCorrected, yCorrected);
            }
        }

请记住,如果您想要在原件上盖章的pdf也有裁剪框,则需要再次将该裁剪框的x,y减去x,y。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15243548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档