How to add paragraph wise texts separated by vertical line into itextsharp created PDF

笑着哭i 提交于 2019-12-11 00:54:15

问题


I have a requirement where i need to divide the newly created PDF file into two parts separated by vertical line.Now as per my requirement i have to add texts into the new PDF paragraph wise means at first all the records should go to paragraph one and one its over it should show into second paragraph from the top.

Here is the code that i am trying to do with ,but i am getting vertical line in only half portion of the PDF.And i am totally unaware of how to add the texts paragraph wise.

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();
} 

Please help me..


回答1:


Let's assume that you want to add paragraphs into columns the way it's done in this PDF: column_paragraphs.pdf.

In that case, you'd define two columns, one for each half of the page. For instance new Rectangle(36, 36, 290, 806) for the left half and new Rectangle(305, 36, 559, 806) for the right half. Suppose that these are two values in a COLUMNS array.

You would then use this COLUMNS array to position a ColumnText object. In Java, this would look like this:

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]);
    }
}

When the value of side_of_the_page is 0, you're working on the left side of the page, and you need to draw a line when you switch to the right side. When the value of side_of_the_page is 1, you're working on the right side of the page, and you need to go to a new page before you switch to the left side again.

I know that you're working in C#, but please look at the Java code as if it were pseudo-code. The C# code is very similar. For more info, please take a look at the other ColumnText examples from my book (I'm the original developer who wrote iText). For the corresponding C# examples, please use this link.




回答2:


Here is the code in C#, although it is not going over to the second column, for some reason, the HasMoreText(ct.Go()) is always false? @Bruno, any ideas on what I can do?

  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();


来源:https://stackoverflow.com/questions/23968209/how-to-add-paragraph-wise-texts-separated-by-vertical-line-into-itextsharp-creat

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