您好我正在使用PDF sharp将用户输入打印到模板文档中的位置。C#PDF文档中的清晰位置
数据(字段)从用户(网页)收集,并使用拉绳方法在文档的适当位置写入。
目前我正在通过试验和错误找到每个页面中每个模板字段的像素位置。如果有方法确定pdf页面中每个字段的像素位置,将会容易得多。
任何建议将是最有帮助的。
感谢
您好我正在使用PDF sharp将用户输入打印到模板文档中的位置。C#PDF文档中的清晰位置
数据(字段)从用户(网页)收集,并使用拉绳方法在文档的适当位置写入。
目前我正在通过试验和错误找到每个页面中每个模板字段的像素位置。如果有方法确定pdf页面中每个字段的像素位置,将会容易得多。
任何建议将是最有帮助的。
感谢
当我使用PDF夏普,我的做法是利用的xUnit结构和引用文档的左上角点作为我的出发点为X/Y位置。
很显然,为PdfPage上的每个元素引用文档(0,0)的左上角点会变得杂乱无章。为了解决这个问题,我使用了XRect类来创建元素坐标的矩形。一旦XRect被绘制到页面上,您就可以通过XRect的属性来引用矩形的X/Y位置。然后通过使用这些坐标和XRect的宽度/高度的一些基本数学运算,您应该能够计算要添加到PdfPage的下一个元素位置的坐标。
请按照下面的代码示例,我已经提供了最终结果的粗略草图。该代码未经测试,但现在基于生产中的代码非常繁重。
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Create an empty page with the default size/orientation
PdfPage page = document.AddPage();
page.Orientation = PageOrientation.Landscape;
page.Width = XUnit.FromMillimeter(300);
page.Height = XUnit.FromMillimeter(200);
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
// Add the first rectangle
XUnit horizontalOffset = XUnit.FromMillimeter(5);
XUnit verticalOffset = XUnit.FromMillimeter(5);
XUnit columnWidth = XUnit.FromMillimeter(100);
XUnit columnHeight = page.Height - (2 * verticalOffset);
XRect columnRect = new XRect(horizontalOffset, verticalOffset, columnWidth, columnHeight);
gfx.DrawRectangle(XBrushes.Teal, columnRect);
// Insert an image inside the rectangle, referencing the Left and Top properties of the rectangle for image placement
XImage topLogo = XImage.FromFile(GetFilePath(@"content\img\pdfs\standard\logo-no-strapline.jpg")); // GetFilePath is a private method, not shown for brevity
gfx.DrawImage(topLogo,
columnRect.Left + XUnit.FromMillimeter(5),
columnRect.Top + XUnit.FromMillimeter(5),
columnRect.Width - XUnit.FromMillimeter(10),
XUnit.FromMillimeter(38));
和输出:
最后,我敢肯定,你是知道的,但有PdfSharp样品here的一个很好的资源。
PDF文件没有像素,也没有像素位置。
您可以打印PDF页面(使用“实际尺寸”尺寸选项)并使用标尺测量位置(这对我们的打印机来说效果很好)。
或者您可以使用Adobe Acrobat来测量PDF页面上的项目位置。
有些试验和错误仍然存在,因为您可能需要半毫米或半毫米。
根据您的标尺,您可以使用XUnit.FromMillimeter或XUnit.FromInch获取PDFsharp的点位置(但点不是像素)。
目前PDFsharp样品在这里:
http://www.pdfsharp.net/wiki/PDFsharpSamples.ashx
我有同样的问题,因为你,josephj1989,我发现了我认为是我们的答案。
按照PDFSharp文档中this page,
当前实现PDFsharp的只有一个图形上下文的布局。原点(0,0)在左上角,坐标向右和向下增长。度量单位始终为点(1/72英寸)。
所以,说我想画一个从左边3英寸和7英寸的图像。从8.5×11页的前5英寸,那么我会做这样的事情:
PdfDocument document = GetBlankPdfDocument();
PdfPage myPage = document.AddPage();
myPage.Orientation = PdfSharp.PageOrientation.Portrait;
myPage.Width = XUnit.FromInch(8.5);
myPage.Height = XUnit.FromInch(11);
XImage myImage = GetSampleImage();
double myX = 3 * 72;
double myY = 7.5 * 72;
double someWidth = 126;
double someHeight = 36;
XGraphics gfx = XGraphics.FromPdfPage(myPage);
gfx.DrawImage(myBarcode, myX, myY, someWidth, someHeight);
我有一个条码图像测试了这一点我自己,我发现,当你用自己的公式,用于测量定位,您可以在1/72英寸的范围内获得精确度。这并不坏。
因此,在这一点上,为这种测量创建某种封装会很好,所以我们主要关注手头的任务而不是转换的细节。
public static double GetCoordinateFromInch(double inches)
{
return inches * 72;
}
我们可以继续做其他的助手这样...
public static double GetCoordinateFromCentimeter(double centimeters)
{
return centimeters * 0.39370 * 72;
}
有了这样的辅助方法,我们可以用前面的示例代码做到这一点:
double myX = GetCoordinateFromInch(3);
double myY = GetCoordinateFromInch(7.5);
double someWidth = 126;
double someHeight = 36;
XGraphics gfx = XGraphics.FromPdfPage(myPage);
gfx.DrawImage(myBarcode, myX, myY, someWidth, someHeight);
我希望这是有帮助的。我相信你会写出比我的例子更干净的代码。另外,可能有更多更智能的方法来简化这个过程,但我只是想在这里放置一些能立即使用我们在文档中看到的东西。
快乐的PDF渲染!