C# iText(sharp) 边距宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1958951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:11:53  来源:igfitidea点击:

iText(sharp) side margins width

c#itextsharpitextmargin

提问by Velja Radenkovic

It looks like I can't set left margin to be less then 42pt width. I am setting it to 0f but it always ends as 42pt. If I set margin to any number greater then 0 it just adds it to initial margin of 42pt. I am setting margin of document object:

看起来我无法将左边距设置为小于 42pt 宽度。我将它设置为 0f 但它总是以 42pt 结尾。如果我将边距设置为大于 0 的任何数字,它只会将其添加到 42pt 的初始边距。我正在设置文档对象的边距:

iTextSharp.text.Rectangle docRect = new iTextSharp.text.Rectangle(pageWidth, pageHeight);
DC = new Document(docRect);
DC.SetMargins(0f, 0f, 0f, 0f);

Page width and height are 6x9 in.

页面宽度和高度为 6x9 英寸。

And I end with:

我以:

42pt margin|CONTENT CONTENT

42pt边距|内容内容

I would appreciate any help. Thanks.

我将不胜感激任何帮助。谢谢。

采纳答案by Velja Radenkovic

I am using PdfPTable and the problem was default PdfPTable horizontal alignment which is CENTER. As soon as I set alignmentof table to left problem went away.

我正在使用 PdfPTable,问题是默认的 PdfPTable 水平对齐方式是 CENTER。一旦我将alignmentof table 设置为 left 问题就消失了。

PdfPTable bTable = new PdfPTable(2);
bTable.HorizontalAlignment = Element.ALIGN_LEFT;

Thanks, Velja

谢谢,韦利亚

回答by Stewbob

Your problem may have to do with how you are adding the content to your document. If you are using a Table (instead of a PdfPTable), even if you have a left margin of 0, the table will still be placed indented from the left margin by default.

您的问题可能与您如何将内容添加到文档有关。如果您使用的是表格(而不是 PdfPTable),即使您的左边距为 0,默认情况下表格仍会从左边距缩进放置。

The default spacing works out to be about 10% of the width of the printable page area. So the left edge of the Table would be placed at ( 0.10 * (pageWidth - leftMargin - rightMargin)).

默认间距大约为可打印页面区域宽度的 10%。所以表格的左边缘将被放置在( 0.10 * (pageWidth - leftMargin - rightMargin)).

10% of 6 inches (at 72 pts per inch) is equal to 43.2 pts, pretty close to the 42 pts you are getting.

6 英寸的 10%(每英寸 72 pts)等于 43.2 pts,非常接近您得到的 42 pts。

回答by Evgeny Koblov

Well, I think that the problem is different. By default, PdfPTable.LockedWidthproperty is set to false, and PdfPTable.WidthPercentageis equal to 80f. As long as default table alignment is Centeryou get the impression that your margins are not honoured... but they are! You sipmly need to set PdfPTable.WidthPercentage = 100f.

嗯,我认为问题是不同的。默认情况下,PdfPTable.LockedWidth属性设置为 false,并且PdfPTable.WidthPercentage等于80f。只要默认表格对齐,Center你就会觉得你的边距没有得到尊重......但它们是!您只需设置PdfPTable.WidthPercentage = 100f.