C# 要删除合法页面格式顶部的大边距吗
我在GDI1.50.4000-beta3b和1.32.4334.0中尝试了下面的代码。当我将页面大小设置为合法格式时,它要么没有转换为合法格式,要么在顶部留有一个很大的空白,就好像页面大小是8.5 x 11,额外的长度插入到PDF的顶部。我宁愿文本从页首开始。我怎样才能避开这件事 在下面的示例中,顶部有一个很大的空白C# 要删除合法页面格式顶部的大边距吗,c#,pdfsharp,migradoc,C#,Pdfsharp,Migradoc,我在GDI1.50.4000-beta3b和1.32.4334.0中尝试了下面的代码。当我将页面大小设置为合法格式时,它要么没有转换为合法格式,要么在顶部留有一个很大的空白,就好像页面大小是8.5 x 11,额外的长度插入到PDF的顶部。我宁愿文本从页首开始。我怎样才能避开这件事 在下面的示例中,顶部有一个很大的空白 // Create a new MigraDoc document Document document = new Document(); //document.UseCmykCo
// 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
用于设置未设置的PageWidth
和PageHeight
调用section.PageSetup=document.DefaultPageSetup.Clone()
为PageWidth
和PageHeight
分配A4大小的值。以后更改PageFormat
对保持A4的有效页面大小没有影响
调用section.PageSetup=document.DefaultPageSetup.Clone()后
必须将PageWidth
和PageHeight
设置为正确的值
section.PageSetup=document.DefaultPageSetup.Clone()代码>用于初始化页面设置的所有值。如果使用PageSetup根据页边距等进行计算,请使用此选项
通常不建议调用section.PageSetup=document.DefaultPageSetup.Clone()代码>。强烈建议使用Clone()
而不是直接更改DefaultPageSetup
如果不指定Clone()
在呈现文档后设置页面大小,则设置PageFormat
的效果与预期一样。如果使用section.PageSetup=document.DefaultPageSetup.Clone()行,它是否有效代码>退出?非常感谢!!!这就成功了。那么为什么这个URL会说“您永远不应该修改DefaultPageSetup,而应该使用Clone()”?渲染后,我也停止设置页面大小。这只是一个在黑暗中拍摄,看看它是否解决了问题的要点是:不要修改DefaultPageSetup。修改节的页面设置,无论是否指定DefaultPageSetup的克隆。