c# - 想要删除合法页面格式顶部的大边距

标签 c# pdfsharp migradoc

我在 MigraDoc GDI 1.50.4000-beta3b 和 1.32.4334.0 中尝试了以下代码。当我将页面大小设置为合法格式时,它要么不会转换为合法格式,要么在顶部留下很大的边距,就好像页面大小为 8.5 x 11,并且额外的长度插入在顶部PDF 的。我宁愿文本从页面顶部开始。我该如何解决这个问题?

在下面的示例中,顶部有较大的边距。

// Create a new MigraDoc document
Document document = new Document();
//document.UseCmykColor = true;

// Add a section to the document
Section section = document.AddSection();
section.PageSetup = document.DefaultPageSetup.Clone();
section.PageSetup.PageFormat = PageFormat.Legal; //setting page size here didn't seem to work
section.PageSetup.TopMargin = "0cm";

// Add a paragraph to the section
Paragraph paragraph = section.AddParagraph();

paragraph.Format.Font.Color = Color.FromCmyk(100, 30, 20, 50);

// Add some text to the paragraph
paragraph.AddFormattedText(@"Hello World!", TextFormat.Bold);

#if GDI
// Using GDI-specific routines.
// Make sure to use "#if GDI" for any usings you add for platform-specific code.
{
}
#endif

#if WPF
// Using WPF-specific routines.
// Make sure to use "#if GDI" for any usings you add for platform-specific code.
{
}
#endif

// Create a renderer for the MigraDoc document.
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);

// Associate the MigraDoc document with a renderer
pdfRenderer.Document = document;

// Layout and render document to PDF
pdfRenderer.RenderDocument();

pdfRenderer.PdfDocument.Pages[0].Size = PdfSharp.PageSize.Legal;

// Save the document...
const string filename = "HelloWorld.pdf";

pdfRenderer.PdfDocument.Save(filename);
// ...and start a viewer.
Process.Start(filename);

最佳答案

PageFormat 用于设置 PageWidthPageHeight(如果未设置)。

调用 section.PageSetup = document.DefaultPageSetup.Clone(); 会为 PageWidthPageHeight 分配 A4 尺寸的值。稍后更改 PageFormat 不会影响有效页面大小,仍为 A4。

调用 section.PageSetup = document.DefaultPageSetup.Clone(); 后,您必须将 PageWidthPageHeight 设置为正确的值.

section.PageSetup = document.DefaultPageSetup.Clone(); 用于初始化PageSetup的所有值。如果您使用 PageSetup 根据边距等进行计算,请使用此选项。

一般不建议调用section.PageSetup = document.DefaultPageSetup.Clone();。强烈建议使用 Clone() 而不是直接更改 DefaultPageSetup

如果您不分配 Clone(),设置 PageFormat 将按预期工作。

关于c# - 想要删除合法页面格式顶部的大边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44010755/

相关文章:

c# - MigraDoc:为文档设置字体

c# - 是否可以将 Func<bool> 作为 while 条件

c# - GenerateEmailConfirmationTokenAsync 默认过期时间跨度

vb.net - 使用 pdfsharp 将多个图像转换为 pdf

c# - MigraDoc - 加粗段落中的某些文本

nuget - PDFsharp-MigraDoc-WPF nuget 包

javascript - 如何从 WebAPI Controller 使用 javascript 函数发送 JSON(不带引号)

C# - Entity Framework - 了解一些基础知识

c# - PDFsharp 保存到 MemoryStream

c# - Migradoc 封面图片