首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何使用iText API获取当前页边距

要使用iText API获取当前页边距,您需要首先创建一个PdfDocument对象,然后获取其PdfPage对象,最后通过PdfPage对象的GetMarginLeftGetMarginRightGetMarginTopGetMarginBottom方法来获取页边距。

以下是一个简单的示例代码:

代码语言:csharp
复制
using System;
using iText.Kernel.Pdf;

class Program
{
    static void Main(string[] args)
    {
        // 创建一个PdfDocument对象
        PdfDocument pdfDoc = new PdfDocument(new PdfReader("path/to/your/pdf/file.pdf"));

        // 获取第一页的PdfPage对象
        PdfPage pdfPage = pdfDoc.GetPage(1);

        // 获取页边距
        float marginLeft = pdfPage.GetMarginLeft();
        float marginRight = pdfPage.GetMarginRight();
        float marginTop = pdfPage.GetMarginTop();
        float marginBottom = pdfPage.GetMarginBottom();

        // 输出页边距
        Console.WriteLine("Margin Left: " + marginLeft);
        Console.WriteLine("Margin Right: " + marginRight);
        Console.WriteLine("Margin Top: " + marginTop);
        Console.WriteLine("Margin Bottom: " + marginBottom);

        // 关闭PdfDocument对象
        pdfDoc.Close();
    }
}

请注意,这个示例代码仅适用于iText 7。如果您使用的是其他版本的iText,请参考相应的文档进行修改。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

itext7知识点研究(PDF编辑)

static class MyEventListener implements IEventListener { private List<Rectangle> rectangles = new ArrayList<>(); @Override public void eventOccurred(IEventData data, EventType type) { if (type == EventType.RENDER_TEXT) { TextRenderInfo renderInfo = (TextRenderInfo) data; Vector startPoint = renderInfo.getDescentLine().getStartPoint(); Vector endPoint = renderInfo.getAscentLine().getEndPoint(); float x1 = Math.min(startPoint.get(0), endPoint.get(0)); float x2 = Math.max(startPoint.get(0), endPoint.get(0)); float y1 = Math.min(startPoint.get(1), endPoint.get(1)); float y2 = Math.max(startPoint.get(1), endPoint.get(1)); rectangles.add(new Rectangle(x1, y1, x2 - x1, y2 - y1)); } } @Override public Set<EventType> getSupportedEvents() { return new LinkedHashSet<>(Collections.singletonList(EventType.RENDER_TEXT)); } public List<Rectangle> getRectangles() { return rectangles; } public void clear() { rectangles.clear(); } } static class MyCharacterEventListener extends MyEventListener { @Override public void eventOccurred(IEventData data, EventType type) { if (type == EventType.RENDER_TEXT) { TextRenderInfo renderInfo = (TextRenderInfo) data; for (TextRenderInfo tri : renderInfo.getCharacterRenderInfos()) { super.eventOccurred(tri, type); } } } }

02
领券