我现在迷路了。我试图完成的是在一个PDF上添加另一个(就像一个水印)。问题是我似乎不理解所使用的坐标系,因为我的水印表现得出乎意料。
这两个PDF的尺寸不同。
我的目标有以下维度:
595高
842宽度
要添加的PDF具有以下维度:
41高
552宽度
在我的代码中,我做了以下事情:
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位置,然后它在页面上垂直居中。
有人能给我点小费吗?
发布于 2013-03-07 19:58:52
所以我解决了这个问题。问题是sourcepdf有一个裁剪框--我只需要用这个信息纠正我的x和y位置:
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。
https://stackoverflow.com/questions/15243548
复制相似问题