微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iText7 如何设置现有PDF 文档的边距并保存新创建的文件?

如何解决iText7 如何设置现有PDF 文档的边距并保存新创建的文件?

我在 C# 中使用 iText7,我有一个 PDF 文件,我必须在其中以编程方式更改边距。 我已经尝试了一切,但都没有取得多大成功,唯一对我有用的有点就是这段代码

PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFile),new PdfWriter(destinationFile));
float margin = 30;

for (int i = 0; i < pdfDoc.GetNumberOfPages(); i++)
{
    pdfpage page = pdfDoc.GetPage(i+1);
    Rectangle mediaBox = page.GetMediaBox();
    Rectangle newMediaBox = new Rectangle(mediaBox.GetLeft() - margin,mediaBox.GetBottom() + margin,mediaBox.GetWidth(),mediaBox.GetHeight());
    page.SetMediaBox(newMediaBox);
}

pdfDoc.Close();

我说有点,因为它所做的只是推送 PDF 文件内容

有什么方法可以更改现有 PDF 文件的边距吗?

任何帮助将不胜感激!

更新

我尝试过古斯塔夫的回答,但没有任何改变 这是我的代码

public static void Main(String[] args)
{
    RegisterPdfImproved(source,target);
}

public static void RegisterPdfImproved(string sourceFilename,string targetFilename)
{
    using (PdfDocument pdf = new PdfDocument(new PdfReader(sourceFilename),new PdfWriter(targetFilename)))
    using (Document document = new Document(pdf))
    {
        document.SetMargins(2000,2000,2000);
        //Paragraph paragraph = new Paragraph(registration).AddStyle(RegistrationStyle()).SetMarginTop(0);
        //document.Add(paragraph);
    }
}

private static Style RegistrationStyle()
{
    // Fixed design values for font and rectangle.
    PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
    const float fontSize = 8F;
    const float rightPadding = 3F;
    TextAlignment textAlignment = TextAlignment.RIGHT;
    iText.Kernel.Colors.Color borderColor = ColorConstants.RED;
    iText.Kernel.Colors.Color fillColor = ColorConstants.WHITE;
    const float borderWidth = 0.7F;

    Style style = new Style()
        .SetFont(font)
        .SetFontSize(fontSize)
        .SetPaddingRight(rightPadding)
        .SetTextAlignment(textAlignment)
        .SetBackgroundColor(fillColor)
        .SetBorder(new SolidBorder(borderColor,borderWidth));

    return style;
}

也许我做错了什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。