【问题标题】:How to add paragraph wise texts separated by vertical line into itextsharp created PDF如何将由垂直线分隔的段落文本添加到 itextsharp 创建的 PDF 中
【发布时间】:2014-05-31 08:55:52
【问题描述】:

我有一个要求,我需要将新创建的 PDF 文件分成由垂直线分隔的两部分。现在根据我的要求,我必须将文本添加到新的 PDF 段落中,这意味着首先所有记录都应该转到第一段和第一段应该从顶部显示到第二段。

这是我正在尝试使用的代码,但我在 PDF 的仅一半部分中得到垂直线。而且我完全不知道如何逐段添加文本。

public static void paraPDF(string pdffile){

   Document pdfDoc = new Document();
   PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(pdffile, FileMode.OpenOrCreate));

   pdfDoc.Open();
   pdfDoc.Add(new Paragraph("Some Text added"));
   PdfContentByte cb = writer.DirectContent;
   cb.MoveTo(pdfDoc.PageSize.Width / 2, pdfDoc.PageSize.Height / 2);
   cb.LineTo(pdfDoc.PageSize.Width / 2, pdfDoc.PageSize.Height);
   cb.Stroke();

   pdfDoc.Close();

   Console.WriteLine("The file was created.");
   Console.ReadLine();
} 

请帮帮我..

【问题讨论】:

  • 请搜索使用ColumnText 对象的示例。您提出了一个非常简单的问题“我如何在一页的两列中显示Paragraph 对象?” 如此复杂以至于没有人理解您想要实现的目标。

标签: c# pdf itextsharp


【解决方案1】:

假设您希望按照此 PDF 中的方式将段落添加到列中:column_paragraphs.pdf

在这种情况下,您需要定义两列,每半页各一列。例如,new Rectangle(36, 36, 290, 806) 用于左半部分,new Rectangle(305, 36, 559, 806) 用于右半部分。假设这些是COLUMNS 数组中的两个值。

然后您将使用此 COLUMNS 数组来定位 ColumnText 对象。在 Java 中,这看起来像这样:

PdfContentByte canvas = writer.getDirectContent();
ColumnText ct = new ColumnText(canvas);
int side_of_the_page = 0;
ct.setSimpleColumn(COLUMNS[side_of_the_page]);
int paragraphs = 0;
while (paragraphs < 30) {
    ct.addElement(new Paragraph(String.format("Paragraph %s: %s", ++paragraphs, TEXT)));
    while (ColumnText.hasMoreText(ct.go())) {
        if (side_of_the_page == 0) {
            side_of_the_page = 1;
            canvas.moveTo(297.5f, 36);
            canvas.lineTo(297.5f, 806);
            canvas.stroke();
        }
        else {
            side_of_the_page = 0;
            document.newPage();
        }
        ct.setSimpleColumn(COLUMNS[side_of_the_page]);
    }
}

side_of_the_page的值为0时,你在页面左侧工作,切换到右侧需要画一条线。当side_of_the_page的值为1时,你正在页面的右侧工作,你需要进入一个新的页面才能再次切换到左侧。

我知道您正在使用 C#,但请把 Java 代码看作是伪代码。 C# 代码非常相似。有关更多信息,请查看来自my book 的其他ColumnText 示例(我是编写 iText 的原始开发人员)。对应的C#示例请使用this link

【讨论】:

  • @Will 你请发布 c# 代码,因为我无法使用 c# 获得写入方法。\
  • 我不会写C#。请说明您找不到哪些方法。
【解决方案2】:

这是 C# 中的代码,虽然它没有转到第二列,但由于某种原因,HasMoreText(ct.Go()) 总是错误的? @Bruno,对我能做什么有什么想法吗?

  var doc = new Document();
        var pdf = "D:/Temp/pdfs/" + DateTime.Now.ToString("yyyymmdd") + ".pdf"; // mm ??

        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(pdf, FileMode.Create));
        doc.Open();
        PdfContentByte canvas = writer.DirectContent;
        ColumnText ct = new ColumnText(canvas);
        int side_of_the_page = 0;
        ct.SetSimpleColumn(COLUMNS[side_of_the_page]);
        int paragraphs = 0;
        while (paragraphs < 30)
        {
            ct.AddElement(new iTextSharp.text.Paragraph(String.Format("Paragraph %s: %s", ++paragraphs, "SOME STUFF")));
            while (ColumnText.HasMoreText(ct.Go()))
            {
                if (side_of_the_page == 0)
                {
                    side_of_the_page = 1;
                    canvas.MoveTo(297.5f, 36);
                    canvas.LineTo(297.5f, 806);
                    canvas.Stroke();
                }
                else
                {
                    side_of_the_page = 0;
                    doc.NewPage();
                }
                ct.SetSimpleColumn(COLUMNS[side_of_the_page]);
            }
        }
        doc.Close();

【讨论】:

    猜你喜欢
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多