想要删除合法页面格式顶部的大余量

时间:2017-05-16 20:12:29

标签: 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);

1 个答案:

答案 0 :(得分:2)

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进行更改。

如果您未指定PageFormat,则设置Clone()可按预期工作。

相关问题