使用iTextSharp从左到右填充表格

时间:2017-06-03 13:19:21

标签: c# pdf itext

我正在使用C#.net。

中的iTextSharp.dll生成PDF

要求:

我需要创建包含信息的PDF目录。我的PDF文档在单页中包含以下部分:

  1. 标题(横幅图片)

  2. 标题(作为目录)

  3. 目录(包含目录信息)

    3.1。左侧内容

    3.2。右侧内容

  4. 页脚(包含页码)

  5. 以下是文档外观的图片或布局:enter image description here

    从上面可以看出,所有页面的页眉,标题和页脚都是相同的。

    有关内容表部分的详细说明:

    内容表部分包含人员信息。我们有以下类型的信息需要填写。

    1. 图像
    2. 有地址的人员详细信息。
    3. 填写信息的过程如下:

      1. 首先填写 左内容表 中的图像和人员详细信息。
      2. 在添加内容时,如果 左侧内容表 到达页脚或者超出页面的页脚,则移至 正确的内容表
      3. 在填写 正确内容表 时,如果超出页脚,则转到下一页并继续 左侧内容表 的新页面。
      4. 我尝试了什么:

        我尝试为单个页面完成此方法。

        class Program
        {
            static void Main(string[] args)
            {
                ///// ---- Table creation in PDF using iTextSharp -----///
        
        
        
                /// Creating a document
                ///
                Document document = new Document(PageSize.A4, 10, 10, 10, 10);
        
                if (File.Exists("F:\\Output.pdf"))
                    File.Delete("F:\\Output.pdf");
                FileStream pdfFileStream = new FileStream("F:\\Output.pdf", FileMode.Create);
                string bannerPath = "F:\\banner.png";
                iTextSharp.text.Image bannerImage = iTextSharp.text.Image.GetInstance(bannerPath);
                bannerImage.ScaleToFit(50f, 90f);
                using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, pdfFileStream))
                {
        
                    document.Open();
                    PdfContentByte cb = pdfWriter.DirectContent;
        
        
                    ///// ---- Table creation in PDF using iTextSharp -----///
        
                    PdfPTable documentTable = new PdfPTable(1);
        
                    documentTable.HorizontalAlignment = 0;//0=Left, 1=Centre, 2=Right
                    documentTable.TotalWidth = 570;
                    documentTable.LockedWidth = true;
                    documentTable.DefaultCell.Border = Rectangle.NO_BORDER;
                    documentTable.SplitLate = false;
                    documentTable.SplitRows = true;
                    //firstRowTable first column
        
                    //Header name table
                    PdfPTable headerTable = new PdfPTable(1);
                    documentTable.HorizontalAlignment = 1;
                    headerTable.TotalWidth = 570;
                    headerTable.LockedWidth = true;
                    headerTable.DefaultCell.Border = Rectangle.NO_BORDER;
        
                    //Add Image to the cell with auto fit true.
                    PdfPCell headerCell = new PdfPCell(bannerImage);
                    headerTable.AddCell(headerCell);
                    documentTable.AddCell(headerTable);
        
        
                    PdfPTable titleTable = new PdfPTable(1);
                    titleTable.HorizontalAlignment = 0;//0=Left, 1=Centre, 2=Right
                    titleTable.TotalWidth = 200;
                    titleTable.LockedWidth = true;
        
                    //Title.
                    Phrase titlePhrase = new Phrase();
                    var FontColour = new BaseColor(31, 73, 125);
                    var normalFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 15, FontColour);
                    Chunk titleChunk = new Chunk("Physician Directory", normalFont);
                    titlePhrase.Add(titleChunk);
                    PdfPCell cell1 = new PdfPCell(titlePhrase);
                    cell1.Border = 0;//No border
                    titleTable.AddCell(cell1);
        
                    documentTable.AddCell(titleTable);
        
                    #region "Physician's table"
        
                    //Create Physician table.
                    PdfPTable physicianTable = new PdfPTable(2);
                    physicianTable.HorizontalAlignment = 0;//0=Left, 1=Centre, 2=Right
                    physicianTable.TotalWidth = 570;
                    physicianTable.LockedWidth = true;
                    physicianTable.DefaultCell.Border = Rectangle.NO_BORDER;
        
                    #region "Physician's left side table"
        
                    //Create Physician table.
                    PdfPTable physicianLeftTable = new PdfPTable(2);
                    physicianLeftTable.HorizontalAlignment = 0;//0=Left, 1=Centre, 2=Right
                    physicianLeftTable.TotalWidth = 285;
                    physicianLeftTable.LockedWidth = true;
                    physicianLeftTable.DefaultCell.Border = Rectangle.NO_BORDER;
        
                    #region "Physician's left side table contents 1"
        
                    #region "Physician's column1"
        
                    string doctor1Path = "F:\\Doctor1.jpg";
                    iTextSharp.text.Image doctor1Image = iTextSharp.text.Image.GetInstance(doctor1Path);
                    doctor1Image.ScaleToFit(50f, 90f);
                    PdfPCell physicianLeftTableContent1Column1 = new PdfPCell(doctor1Image);
        
                    physicianLeftTableContent1Column1.Border = Rectangle.NO_BORDER;
                    physicianLeftTable.AddCell(physicianLeftTableContent1Column1);
                    #endregion "Physician's column1"
        
                    #region "Physician's column2"
                    Phrase paraPhrase = new Phrase();
                    var nameChunkFontColour = new BaseColor(31, 73, 125);
                    var nameChunkNormalFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8, nameChunkFontColour);
                    Chunk nameChunk = new Chunk("Name1\n", nameChunkNormalFont);
                    paraPhrase.Add(new Paragraph(nameChunk));
        
                    var doctorType1ChunkNormalFont = FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 8);
                    Chunk doctorType1Chunk = new Chunk("Pediatric Neurologist,\n", doctorType1ChunkNormalFont);
                    paraPhrase.Add(new Paragraph(doctorType1Chunk));
        
                    var doctorType2ChunkNormalFont = FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 8);
                    Chunk doctorType2Chunk = new Chunk("Headache Medicine\n", doctorType1ChunkNormalFont);
                    paraPhrase.Add(new Paragraph(doctorType2Chunk));
        
                    paraPhrase.Add(new Paragraph("\n"));
        
                    var healthGroupChunkNormalFont = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk healthGroupChunk = new Chunk("Health Medical Group\n", healthGroupChunkNormalFont);
                    paraPhrase.Add(new Paragraph(healthGroupChunk));
        
                    var addressLine1ChunkNormalFont = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine1Chunk = new Chunk("Pediatric Neurology, \n", addressLine1ChunkNormalFont);
                    paraPhrase.Add(new Paragraph(addressLine1Chunk));
        
                    var addressLine2ChunkNormalFont = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine2Chunk = new Chunk("701 Road\n", addressLine2ChunkNormalFont);
                    paraPhrase.Add(new Paragraph(addressLine2Chunk));
        
                    var addressLine3ChunkNormalFont = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine3Chunk = new Chunk("Village, IL 7\n", addressLine3ChunkNormalFont);
                    paraPhrase.Add(new Paragraph(addressLine3Chunk));
        
                    var zipCodeChunkNormalFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8);
                    Chunk zipCodeChunk = new Chunk("123-456 -789\n", zipCodeChunkNormalFont);
                    paraPhrase.Add(new Paragraph(zipCodeChunk));
        
                    paraPhrase.Add(new Paragraph("\n"));
        
                    var healthGroup1ChunkNormalFont = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk healthGroup1Chunk = new Chunk("Health Medical Group\n", healthGroup1ChunkNormalFont);
                    paraPhrase.Add(new Paragraph(healthGroup1Chunk));
        
                    var addressLine1ChunkNormalFont1 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine1Chunk1 = new Chunk("Pediatric Neurology, \n", addressLine1ChunkNormalFont1);
                    paraPhrase.Add(new Paragraph(addressLine1Chunk1));
        
                    var addressLine2ChunkNormalFont1 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine2Chunk1 = new Chunk("701 Road\n", addressLine2ChunkNormalFont1);
                    paraPhrase.Add(new Paragraph(addressLine2Chunk1));
        
                    var addressLine3ChunkNormalFont1 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine3Chunk1 = new Chunk("Village, IL 7\n", addressLine3ChunkNormalFont1);
                    paraPhrase.Add(new Paragraph(addressLine3Chunk1));
        
                    var zipCodeChunkNormalFont1 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8);
                    Chunk zipCodeChunk1 = new Chunk("123-456 -7890\n", zipCodeChunkNormalFont1);
                    paraPhrase.Add(new Paragraph(zipCodeChunk1));
        
                    paraPhrase.Add(new Paragraph("\n"));
                    var healthGroup2ChunkNormalFont = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk healthGroup2Chunk = new Chunk("Health Medical Group\n", healthGroup2ChunkNormalFont);
                    paraPhrase.Add(new Paragraph(healthGroup2Chunk));
        
                    var addressLine1ChunkNormalFont2 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine1Chunk2 = new Chunk("Pediatric Neurology\n", addressLine1ChunkNormalFont2);
                    paraPhrase.Add(new Paragraph(addressLine1Chunk2));
        
                    var addressLine2ChunkNormalFont2 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine2Chunk2 = new Chunk("701 Road\n", addressLine2ChunkNormalFont2);
                    paraPhrase.Add(new Paragraph(addressLine2Chunk2));
        
                    var addressLine3ChunkNormalFont2 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine3Chunk2 = new Chunk("Village, IL 12345\n", addressLine3ChunkNormalFont2);
                    paraPhrase.Add(new Paragraph(addressLine3Chunk2));
        
                    var zipCodeChunkNormalFont2 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8);
                    Chunk zipCodeChunk2 = new Chunk("123-456 -7890\n", zipCodeChunkNormalFont2);
                    paraPhrase.Add(new Paragraph(zipCodeChunk2));
        
                    paraPhrase.Add(new Paragraph("\n"));
        
                    var healthGroup3ChunkNormalFont = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk healthGroup3Chunk = new Chunk("Health Medical Group\n", healthGroup3ChunkNormalFont);
                    paraPhrase.Add(new Paragraph(healthGroup3Chunk));
        
                    var addressLine1ChunkNormalFont3 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine1Chunk3 = new Chunk("Pediatric Neurology\n", addressLine1ChunkNormalFont3);
                    paraPhrase.Add(new Paragraph(addressLine1Chunk3));
        
                    var addressLine2ChunkNormalFont3 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine2Chunk3 = new Chunk("701 Road\n", addressLine2ChunkNormalFont3);
                    paraPhrase.Add(new Paragraph(addressLine2Chunk3));
        
                    var addressLine3ChunkNormalFont3 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine3Chunk3 = new Chunk("Village, IL 12345\n", addressLine3ChunkNormalFont3);
                    paraPhrase.Add(new Paragraph(addressLine3Chunk3));
        
                    var zipCodeChunkNormalFont3 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8);
                    Chunk zipCodeChunk3 = new Chunk("123-456 -7890\n", zipCodeChunkNormalFont3);
                    paraPhrase.Add(new Paragraph(zipCodeChunk3));
        
                    paraPhrase.Add(new Paragraph("\n"));
        
                    var healthGroup4ChunkNormalFont = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk healthGroup4Chunk = new Chunk("Health Medical Group\n", healthGroup4ChunkNormalFont);
                    paraPhrase.Add(new Paragraph(healthGroup4Chunk));
        
                    var addressLine1ChunkNormalFont4 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine1Chunk4 = new Chunk("Pediatric Neurology\n", addressLine1ChunkNormalFont4);
                    paraPhrase.Add(new Paragraph(addressLine1Chunk4));
        
                    var addressLine2ChunkNormalFont4 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine2Chunk4 = new Chunk("Road\n", addressLine2ChunkNormalFont4);
                    paraPhrase.Add(new Paragraph(addressLine2Chunk4));
        
                    var addressLine3ChunkNormalFont4 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine3Chunk4 = new Chunk("Village, IL 12345\n", addressLine3ChunkNormalFont4);
                    paraPhrase.Add(new Paragraph(addressLine3Chunk4));
        
                    var zipCodeChunkNormalFont4 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8);
                    Chunk zipCodeChunk4 = new Chunk("123-456 -7890\n", zipCodeChunkNormalFont4);
                    paraPhrase.Add(new Paragraph(zipCodeChunk4));
        
                    paraPhrase.Add(new Paragraph("\n"));
        
                    PdfPCell physicianLeftTableContent1Column2 = new PdfPCell(paraPhrase);
                    physicianLeftTableContent1Column2.Border = Rectangle.NO_BORDER;
                    physicianLeftTable.AddCell(physicianLeftTableContent1Column2);
        
                    int count = pdfWriter.PageNumber;
        
                    #endregion "Physician's column2"
        
                    //physicianLeftTable.AddCell(physicianLeftTableContent1);
        
                    #endregion "Physician's left side content table"
        
                    #region "Physician's left side table contents 2"
                    //Create left side table.
                    PdfPTable physicianLeftTableContent2 = new PdfPTable(2);
                    physicianLeftTableContent2.HorizontalAlignment = 0;//0=Left, 1=Centre, 2=Right
                    physicianLeftTableContent2.TotalWidth = 142.5f;
                    physicianLeftTableContent2.LockedWidth = true;
                    physicianLeftTableContent2.DefaultCell.Border = Rectangle.NO_BORDER;
        
                    #region "Physician's column1"
        
                    string doctor2Path = "F:\\Image2.jpg";
                    iTextSharp.text.Image doctor2Image = iTextSharp.text.Image.GetInstance(doctor2Path);
                    doctor2Image.ScaleToFit(50f, 90f);
                    PdfPCell physicianLeftTableContent2Column1 = new PdfPCell(doctor2Image);
        
                    physicianLeftTableContent2Column1.Border = Rectangle.NO_BORDER;
                    physicianLeftTable.AddCell(physicianLeftTableContent2Column1);
        
                    #endregion "Physician's column1"
        
                    #region "Physician's column2"
                    Phrase paraPhrase1 = new Phrase();
                    var nameChunkFontColour1 = new BaseColor(31, 73, 125);
                    var nameChunkNormalFont1 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8, nameChunkFontColour1);
                    Chunk nameChunk1 = new Chunk("Name1 M.D.\n", nameChunkNormalFont1);
                    paraPhrase.Add(new Paragraph(nameChunk1));
        
                    var doctorType3ChunkNormalFont = FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 8);
                    Chunk doctorType3Chunk = new Chunk("General Surgery\n", doctorType3ChunkNormalFont);
                    paraPhrase.Add(new Paragraph(doctorType3Chunk));
        
                    paraPhrase.Add(new Paragraph("\n"));
        
                    var healthGroupChunkNormalFont5 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk healthGroupChunk5 = new Chunk("Medical Center\n", healthGroupChunkNormalFont5);
                    paraPhrase.Add(new Paragraph(healthGroupChunk5));
        
                    var addressLine1ChunkNormalFont5 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine1Chunk5 = new Chunk("First Avenue\n", addressLine1ChunkNormalFont5);
                    paraPhrase.Add(new Paragraph(addressLine1Chunk5));
        
                    var addressLine2ChunkNormalFont5 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine2Chunk5 = new Chunk("Room 3230\n", addressLine2ChunkNormalFont5);
                    paraPhrase.Add(new Paragraph(addressLine2Chunk5));
        
                    var addressLine3ChunkNormalFont5 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine3Chunk5 = new Chunk("IL 60153\n", addressLine3ChunkNormalFont5);
                    paraPhrase.Add(new Paragraph(addressLine3Chunk5));
        
                    var zipCodeChunkNormalFont5 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8);
                    Chunk zipCodeChunk5 = new Chunk("123-456 -7890\n", zipCodeChunkNormalFont5);
                    paraPhrase.Add(new Paragraph(zipCodeChunk5));
        
                    paraPhrase.Add(new Paragraph("\n"));
        
                    var healthGroupChunkNormalFont6 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk healthGroupChunk6 = new Chunk("Medical Center\n", healthGroupChunkNormalFont6);
                    paraPhrase.Add(new Paragraph(healthGroupChunk6));
        
                    var addressLine1ChunkNormalFont6 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine1Chunk6 = new Chunk("First Avenue\n", addressLine1ChunkNormalFont6);
                    paraPhrase.Add(new Paragraph(addressLine1Chunk6));
        
                    var addressLine2ChunkNormalFont6 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine2Chunk6 = new Chunk("Room 3230\n", addressLine2ChunkNormalFont6);
                    paraPhrase.Add(new Paragraph(addressLine2Chunk6));
        
                    var addressLine3ChunkNormalFont6 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
                    Chunk addressLine3Chunk6 = new Chunk("IL 60153\n", addressLine3ChunkNormalFont6);
                    paraPhrase.Add(new Paragraph(addressLine3Chunk6));
        
                    var zipCodeChunkNormalFont6 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8);
                    Chunk zipCodeChunk6 = new Chunk("123-456 -7890\n", zipCodeChunkNormalFont6);
                    paraPhrase.Add(new Paragraph(zipCodeChunk6));
        
        
                    PdfPCell physicianLeftTableContent2Column2 = new PdfPCell(paraPhrase);
                    physicianLeftTableContent2Column2.Border = Rectangle.NO_BORDER;
                    physicianLeftTable.AddCell(physicianLeftTableContent2Column2);
        
                    #endregion "Physician's column2"
        
                    //physicianLeftTable.AddCell(physicianLeftTableContent2);
        
                    #endregion "Physician's left side content table"
        
                    int count1 = pdfWriter.PageNumber;
                    //Add the left side physician table.
                    physicianTable.AddCell(physicianLeftTable);
        
                    #endregion "Physician's left side table"
        
                    #region "Physician's right side table"
        
                    //Create Physician table.
                    PdfPTable physicianRightTable = new PdfPTable(2);
                    physicianRightTable.HorizontalAlignment = 0;//0=Left, 1=Centre, 2=Right
                    physicianRightTable.TotalWidth = 285;
                    physicianRightTable.LockedWidth = true;
                    physicianRightTable.DefaultCell.Border = Rectangle.NO_BORDER;
        
                    #region "Physician's right side table contents 1"
        
                    //Create right side table content.
                    PdfPTable physicianRightTableContent1 = new PdfPTable(2);
                    physicianRightTableContent1.HorizontalAlignment = 0;//0=Left, 1=Centre, 2=Right
                    physicianRightTableContent1.TotalWidth = 142.5f;
                    physicianRightTableContent1.LockedWidth = true;
                    physicianRightTableContent1.DefaultCell.Border = Rectangle.NO_BORDER;
        
                    #region "Physician's column1"
        
                    string doctor3Path = "F:\\Image2.jpg";
                    iTextSharp.text.Image doctor3Image = iTextSharp.text.Image.GetInstance(doctor3Path);
                    doctor3Image.ScaleToFit(50f, 90f);
                    PdfPCell physicianRightTableContent1Column1 = new PdfPCell(doctor3Image);
        
                    physicianRightTableContent1Column1.Border = Rectangle.NO_BORDER;
                    physicianRightTable.AddCell(physicianRightTableContent1Column1);
                    #endregion "Physician's column1"
        
                    //Add the right side physician table.
                    //physicianRightTable.AddCell(physicianRightTableContent1);
        
                    #endregion "Physician's right side table contents"
        
                    #region "Physician's right side table contents 2"
        
                    //Create right side table content.
                    PdfPTable physicianRightTableContent2 = new PdfPTable(2);
                    physicianRightTableContent2.HorizontalAlignment = 0;//0=Left, 1=Centre, 2=Right
                    physicianRightTableContent2.TotalWidth = 142.5f;
                    physicianRightTableContent2.LockedWidth = true;
                    physicianRightTableContent2.DefaultCell.Border = Rectangle.NO_BORDER;
        
                    #region "Physician's column1"
        
                    string doctor4Path = "F:\\Image1.jpg";
                    iTextSharp.text.Image doctor4Image = iTextSharp.text.Image.GetInstance(doctor4Path);
                    doctor4Image.ScaleToFit(50f, 90f);
                    PdfPCell physicianRightTableContent2Column1 = new PdfPCell(doctor3Image);
        
                    physicianRightTableContent1Column1.Border = Rectangle.NO_BORDER;
                    physicianRightTableContent1.AddCell(physicianRightTableContent2Column1);
                    #endregion "Physician's column1"
        
                    //Add the left side physician table.
                    physicianRightTable.AddCell(physicianRightTableContent2);
        
                    //Add the right side physician table.
                    //physicianRightTable.AddCell(new Phrase("Hi"));
        
                    #endregion "Physician's right side table contents 2"
        
                    //Add the left side physician table.
                    physicianTable.AddCell(physicianRightTable);
        
                    #endregion "Physician's right side table"
        
                    //Add physician table to the document table.
                    documentTable.AddCell(physicianTable);
        
                    #endregion "Physician's table"
        
                    document.Add(documentTable);
        
                    document.Close();
                }
        
        
                Console.WriteLine("Completed");
        
                //if (File.Exists("F:\\Output.pdf"))
                //    File.Open("F:\\Output.pdf", FileMode.Open);
            }
        
        }
        

        我需要什么:

        1. 如果左内容表到达文档末尾,则将内容移至右内容表
        2. 如果正确内容表到达文档末尾并开始在新页面的左内容表中编写内容,则将内容移至下一页。< / LI>

          注意:

          由于我在上面的代码中没有页脚设置,我试图找到文档的结尾。我很难找到内容表是否到达文档的末尾。

0 个答案:

没有答案