尝试将源 pdf 复制到特定 Y 位置的目标
Trying to copy source pdf to destination at specific Y position
我正在测试一种从名为 55PREMIUMPAYMENTWARRANTY.pdf 的现有 pdf 中读取的方法,该 pdf 在顶部有几个小段落,每段 3 行。然后我尝试使用 canvas.
将它复制到不同 Y 位置的新文档
我在不同位置的循环中调用了这个方法,并对结果感到惊讶。
Y位置通常从左下角的0开始,但只有当Y值为负时才会显示在新页面上..这是为什么?
通常,如果我只是写纯文本,400 的 Y 值将大致位于 595 x 842 的 A4 页面的中间。
但是如果我想在中间显示它,我需要将 Y 设置为 -300 左右,这对我来说毫无意义。
设置位置的那一行是
canvas.AddXObject(pageCopy, 0, 位置);
方法在这里..
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;
}
}
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.
这其实很自然:
如果您想定位文本,您必须使用该文本的 baseline 开始的坐标。对于垂直居中的文本,这将位于页面区域内部的某处,明显高于页面底部。
如果要定位一个窗体 XObject,则必须使用该 XObject 左下角 所在的坐标。要将目标页面上页面大小的表单 XObject 的顶部内容垂直居中,左下角将位于页面区域 下方 的某处:
很明显,源页面的左下角应位于目标页面底部下方。由于目标页面的坐标系通常以其左下角为原点,这意味着该位置的 y 坐标为负。
严格来说,上面所说的是一种简化,因为通常你不会看 XObject 形式的 左下角 应该去哪里,而是 [=26= XObject坐标系的原点应该去。
但是对于从某个源页面创建的表单 XObject,原始页面的坐标系原点(通常在其左下角)成为 XObject 的原点。因此,通常上面说的是正确的。但是,对于通用解决方案,您必须考虑 XObject 边界框的实际值。
我正在测试一种从名为 55PREMIUMPAYMENTWARRANTY.pdf 的现有 pdf 中读取的方法,该 pdf 在顶部有几个小段落,每段 3 行。然后我尝试使用 canvas.
将它复制到不同 Y 位置的新文档我在不同位置的循环中调用了这个方法,并对结果感到惊讶。
Y位置通常从左下角的0开始,但只有当Y值为负时才会显示在新页面上..这是为什么?
通常,如果我只是写纯文本,400 的 Y 值将大致位于 595 x 842 的 A4 页面的中间。
但是如果我想在中间显示它,我需要将 Y 设置为 -300 左右,这对我来说毫无意义。
设置位置的那一行是 canvas.AddXObject(pageCopy, 0, 位置);
方法在这里..
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;
}
}
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.
这其实很自然:
如果您想定位文本,您必须使用该文本的 baseline 开始的坐标。对于垂直居中的文本,这将位于页面区域内部的某处,明显高于页面底部。
如果要定位一个窗体 XObject,则必须使用该 XObject 左下角 所在的坐标。要将目标页面上页面大小的表单 XObject 的顶部内容垂直居中,左下角将位于页面区域 下方 的某处:
很明显,源页面的左下角应位于目标页面底部下方。由于目标页面的坐标系通常以其左下角为原点,这意味着该位置的 y 坐标为负。
严格来说,上面所说的是一种简化,因为通常你不会看 XObject 形式的 左下角 应该去哪里,而是 [=26= XObject坐标系的原点应该去。
但是对于从某个源页面创建的表单 XObject,原始页面的坐标系原点(通常在其左下角)成为 XObject 的原点。因此,通常上面说的是正确的。但是,对于通用解决方案,您必须考虑 XObject 边界框的实际值。