itextsharp how to add a full line break

痴心易碎 提交于 2019-12-19 04:02:45

问题


I use itextsharp and i need to draw a dotted linebreak from left to right of the page(100% width) but don't know how. The doc always has a margin left right. Please help

var pageSize = PageSize.A4;

        if (_pdfSettings.LetterPageSizeEnabled)
        {
            pageSize = PageSize.LETTER;
        }


        var doc = new Document(pageSize);
        PdfWriter.GetInstance(doc, stream);
        doc.Open();

        //fonts

        var titleFont = GetFont();
        titleFont.SetStyle(Font.BOLD);
        titleFont.Color = BaseColor.BLACK;
        titleFont.Size = 16;

        var largeFont = GetFont();
        largeFont.SetStyle(Font.BOLD);
        largeFont.Color = BaseColor.BLACK;
        largeFont.Size = 18;

        int ordCount = orders.Count;
        int ordNum = 0;

        foreach (var order in orders)
        {

            var addressTable = new PdfPTable(3);
            addressTable.WidthPercentage = 100f;
            addressTable.SetWidths(new[] { 25, 37, 37 });


            // sender address

            cell = new PdfPCell();
            //cell.Border = Rectangle.NO_BORDER;
            cell.AddElement(new Paragraph("Người Gửi", titleFont));
            cell.AddElement(new Paragraph(_localizationService.GetResource("admin.orders.pdfinvoice.sender", lang.Id), smallFont));
            cell.AddElement(new Paragraph(_localizationService.GetResource("admin.orders.pdfinvoice.senderaddress", lang.Id), smallFont));
            cell.AddElement(new Paragraph(_localizationService.GetResource("PDFInvoice.Hotline", lang.Id), smallFont));
            cell.AddElement(new Paragraph("TAKARA.VN", largeFont));

            addressTable.AddCell(cell);

            ......
           Chunk linebreak = new Chunk(new DottedLineSeparator());
                doc.Add(linebreak);   

                doc.Add(new Paragraph(""));
           ....
}

回答1:


Please take a look at the example FullDottedLine.

You're creating a DottedLineSeparator of which the width percentage is 100% by default. This 100% is the full available width withing the margins of the page. If you want the line to exceed the available width, you need a percentage that is higher than 100%.

In the example, the default page size (A4) and the default margins (36) are used. This means that the width of the page is 595 user units and the available width equals 595 - (2 x 36) user units. The percentage needed to span the complete width of the page equals 100 x (595 / 523).

Take a look at the resulting PDF file full_dotted_line.pdf and you'll see that the line now runs through the margins.



来源:https://stackoverflow.com/questions/20233630/itextsharp-how-to-add-a-full-line-break

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!