【问题标题】:Code isn't drawing a horizontal line in my PDF代码未在我的 PDF 中绘制水平线
【发布时间】:2011-06-21 03:04:46
【问题描述】:

我正在尝试在顶部添加一条水平线,以将标题文本与我的 pdf 文件中的实际值分开:

这是我的代码:

public class StudentList
{
    public void PrintStudentList(int gradeParaleloID)
    {
        StudentRepository repo = new StudentRepository();
        var students = repo.FindAllStudents()
                            .Where(s => s.IDGradeParalelo == gradeParaleloID);

        try
        {
            Document document = new Document(PageSize.LETTER);

            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\Alumnos.pdf", FileMode.Create));
            document.Open();

            PdfContentByte cb = writer.DirectContent;
            cb.SetLineWidth(2.0f);   // Make a bit thicker than 1.0 default
            cb.SetGrayStroke(0.95f); // 1 = black, 0 = white
            cb.MoveTo(20, 30);
            cb.LineTo(400, 30);
            cb.Stroke();

            PdfPTable table = new PdfPTable(3);                
            float[] widths = new float[] { 0.6f, 0.75f, 2f };
            table.SetWidths(widths);
            PdfPCell numeroCell = new PdfPCell(new Phrase("Nro."));
            numeroCell.Border = 0;
            numeroCell.HorizontalAlignment = 0;
            table.AddCell(numeroCell);

            PdfPCell codigoCell = new PdfPCell(new Phrase("RUDE"));
            codigoCell.Border = 0;
            codigoCell.HorizontalAlignment = 0;
            table.AddCell(codigoCell);

            PdfPCell nombreCell = new PdfPCell(new Phrase("Apellidos y Nombres"));
            nombreCell.Border = 0;
            nombreCell.HorizontalAlignment = 0;
            table.AddCell(nombreCell);

            int c = 1;
            foreach (var student in students)
            {
                PdfPCell cell = new PdfPCell(new Phrase(c.ToString()));
                cell.Border = 0;
                cell.HorizontalAlignment = 0;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(student.Rude.ToString()));
                cell.Border = 0;
                cell.HorizontalAlignment = 0;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(student.LastNameFather + " " + student.LastNameMother + " " + student.Name));
                cell.Border = 0;
                cell.HorizontalAlignment = 0;
                table.AddCell(cell);
                c++;
            }
            table.SpacingBefore = 20f;
            table.SpacingAfter = 30f;

            document.Add(table);
            document.Close();
        }
        catch (DocumentException de)
        {
            Debug.WriteLine(de.Message);
        }
        catch (IOException ioe)
        {
            Debug.WriteLine(ioe.Message);
        }

    }
}

我不明白为什么 cb.Stroke() 不起作用。有什么建议吗?

【问题讨论】:

  • 仅供参考:SetGrayStroke 似乎实际上是 0 = 黑色,1 = 白色。

标签: c# itextsharp


【解决方案1】:

使用 iTextSharp 的 PdfContentByte 类绘图可能会有点混乱。高度实际上是相对于底部,而不是顶部。所以页面的顶部不是 0f,而是实际上是 document.Top,在你的页面大小 PageSize.LETTER 上是 726f。因此,如果您想从顶部绘制 30 个单位的线,请尝试:

cb.MoveTo(20, document.Top - 30f);
cb.LineTo(400, document.Top - 30f);

只是好奇——为什么不使用表格边框而不是使用困难的方式呢? (我注意到您将边框设置为 0)尝试手动分隔线条是最大的痛苦。如果您曾经移动 PDF 中的内容,则必须确保您的测量仍然有效。

试试这个:

numeroCell.Border = 0;
numeroCell.BorderColorBottom = new BaseColor(System.Drawing.Color.Black);
numeroCell.BorderWidthBottom = 1f;

codigoCell.Border = 0;
codigoCell.BorderColorBottom = new BaseColor(System.Drawing.Color.Black);
codigoCell.BorderWidthBottom = 1f;

nombreCell.Border = 0;
nombreCell.BorderColorBottom = new BaseColor(System.Drawing.Color.Black);
nombreCell.BorderWidthBottom = 1f;

这应该会在标题行下方为您提供一条漂亮的黑色实线,无需测量:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    相关资源
    最近更新 更多