none
Want to add 4 different PDFs in one-page PDF ItextSharp in c# RRS feed

  • Question

  • I would like to show 4 different PDF files, and there may be one or more pages in PDF 4. These four PDF files will be brought in a Phase PDF. Like the table
    Wednesday, March 20, 2019 5:18 AM

Answers

  • Hi

    Thank you for posting here.

    For your question, you want to add 4 different PDFs in one-page PDF ItextSharp in c#.

    You could try the following code.

    static void Main(string[] args)
            {
                string path1 = @"D:\pic\t1.pdf";
                string path2 = @"D:\pic\t2.pdf";
                string path3 = @"D:\pic\t3.pdf";
                string path4 = @"D:\pic\t4.pdf";
                string patho = @"D:\pic\total.pdf";
                PdfReader reader = new PdfReader(GetMasterDocument(4,path1,path2,path3,path4));
                Rectangle pageSize = reader.GetPageSize(1);
                using (FileStream stream = new FileStream(
                    patho,
                    FileMode.Create,
                    FileAccess.Write))
                {
                    using (Document document = new Document(pageSize, 0, 0, 0, 0))
                    {
                        PdfWriter writer = PdfWriter.GetInstance(document, stream);
                        document.Open();
                        PdfPTable table = new PdfPTable(2);
                        table.TotalWidth = pageSize.Width;
                        table.LockedWidth = true;
                        table.DefaultCell.Border = Rectangle.NO_BORDER;
                        table.DefaultCell.FixedHeight = pageSize.Height / 2;
    
                        for (int i = 1; i <= reader.NumberOfPages; i++)
                        {
                            PdfImportedPage page = writer.GetImportedPage(reader, i);
                            table.AddCell(Image.GetInstance(page));
                        }
                        document.Add(table);
                    }
                }
    
            }
            static byte[] GetMasterDocument(int count,string path1,string path2,string path3,string path4)
            {
                using (var stream = new MemoryStream())
                {
                    using (var document = new Document())
                    {
                        PdfWriter.GetInstance(document, stream);
                        document.Open();
                        BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                        iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 30, iTextSharp.text.Font.NORMAL);
                        string text1 = text(path1);
                        string text2 = text(path2);
                        string text3 = text(path3);
                        string text4 = text(path4);
                        for (int i = 1; i <= count; ++i)
                        {
                            if(i==1)
                            {
                                document.Add(new Paragraph(string.Format(text1, i), font));
                            }
                            if (i == 2)
                            {
                                document.Add(new Paragraph(string.Format(text2, i), font));
                            }
                            if (i == 3)
                            {
                                document.Add(new Paragraph(string.Format(text3, i), font));
                            }
                            if (i == 4)
                            {
                                document.Add(new Paragraph(string.Format(text4, i), font));
                            }
                            if (i < count) document.NewPage();
                        }
                    }
                    return stream.ToArray();
                }
            }
            static string text(string path)
            {
                PdfReader pdfReader = new PdfReader(path);
                string text = string.Empty;
                iTextSharp.text.pdf.parser.ITextExtractionStrategy strategy = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();
                text = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(pdfReader, 1, strategy);
                return text;
            }
    

    Result:

    Best regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 20, 2019 8:00 AM
    Moderator

All replies

  • Hi

    Thank you for posting here.

    For your question, you want to add 4 different PDFs in one-page PDF ItextSharp in c#.

    You could try the following code.

    static void Main(string[] args)
            {
                string path1 = @"D:\pic\t1.pdf";
                string path2 = @"D:\pic\t2.pdf";
                string path3 = @"D:\pic\t3.pdf";
                string path4 = @"D:\pic\t4.pdf";
                string patho = @"D:\pic\total.pdf";
                PdfReader reader = new PdfReader(GetMasterDocument(4,path1,path2,path3,path4));
                Rectangle pageSize = reader.GetPageSize(1);
                using (FileStream stream = new FileStream(
                    patho,
                    FileMode.Create,
                    FileAccess.Write))
                {
                    using (Document document = new Document(pageSize, 0, 0, 0, 0))
                    {
                        PdfWriter writer = PdfWriter.GetInstance(document, stream);
                        document.Open();
                        PdfPTable table = new PdfPTable(2);
                        table.TotalWidth = pageSize.Width;
                        table.LockedWidth = true;
                        table.DefaultCell.Border = Rectangle.NO_BORDER;
                        table.DefaultCell.FixedHeight = pageSize.Height / 2;
    
                        for (int i = 1; i <= reader.NumberOfPages; i++)
                        {
                            PdfImportedPage page = writer.GetImportedPage(reader, i);
                            table.AddCell(Image.GetInstance(page));
                        }
                        document.Add(table);
                    }
                }
    
            }
            static byte[] GetMasterDocument(int count,string path1,string path2,string path3,string path4)
            {
                using (var stream = new MemoryStream())
                {
                    using (var document = new Document())
                    {
                        PdfWriter.GetInstance(document, stream);
                        document.Open();
                        BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                        iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 30, iTextSharp.text.Font.NORMAL);
                        string text1 = text(path1);
                        string text2 = text(path2);
                        string text3 = text(path3);
                        string text4 = text(path4);
                        for (int i = 1; i <= count; ++i)
                        {
                            if(i==1)
                            {
                                document.Add(new Paragraph(string.Format(text1, i), font));
                            }
                            if (i == 2)
                            {
                                document.Add(new Paragraph(string.Format(text2, i), font));
                            }
                            if (i == 3)
                            {
                                document.Add(new Paragraph(string.Format(text3, i), font));
                            }
                            if (i == 4)
                            {
                                document.Add(new Paragraph(string.Format(text4, i), font));
                            }
                            if (i < count) document.NewPage();
                        }
                    }
                    return stream.ToArray();
                }
            }
            static string text(string path)
            {
                PdfReader pdfReader = new PdfReader(path);
                string text = string.Empty;
                iTextSharp.text.pdf.parser.ITextExtractionStrategy strategy = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();
                text = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(pdfReader, 1, strategy);
                return text;
            }
    

    Result:

    Best regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 20, 2019 8:00 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 21, 2019 5:10 AM
    Moderator
  • Image.GetInstance(page)  Here's how I will save the data in the database.
    Thursday, March 21, 2019 6:33 AM