2017-05-16 1 views
1

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); 
+2

ドキュメントのレンダリング後にページサイズを設定するのが悪い考え。 'section.PageSetup = document.DefaultPageSetup.Clone();'という行を取るとうまくいくのですか? –

+0

ありがとうございました!!!それがトリックでした。それでは、なぜこのURLは「あなたはDefaultPageSetupを変更してはいけません、代わりにClone()を使うべきです」と言っていますか? http://stackoverflow.com/questions/32757196/how-can-set-the-page-size-of-migradocレンダリング後のページサイズの設定もやめました。それは、暗闇の中でちょうどそのショットが問題を解決したかどうかを確認することでした – Hoppe

+0

ポイントは:DefaultPageSetupを変更しないでください。 DefaultPageSetupのクローンを割り当てるかどうかにかかわらず、セクションのPageSetupを変更します。 –

答えて

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();を呼び出すのは一般的に推奨されていません。 DefaultPageSetupに直接変更するのではなく、Clone()を使用することを強くお勧めします。

PageFormatを設定すると、Clone()を割り当てないと正常に動作します。

関連する問題