none
4 PDF file marge to 1 PDF RRS feed

  • Question

  • Suppose 1 PDF file has 16 data (Screenshot 1). I cropped them to 16 individual. Now I want to add them in 1 PDF 4 data and for the 16 data will be shown to 4 PDF file. The output should be - 1st page 1, 5, 9, 13. 2nd page 2, 6, 10, 14. 3rd page 3, 7, 11, 15. 4th page 4, 8, 12, 16 (Screenshot 2). How to do it? Anyone know please answer.

    Thanks in advanced.

    Screenshot 1

    Screenshot 2

    Wednesday, March 27, 2019 4:54 PM

All replies

  • .NET doesn't support working with PDFs. You will have to use a third party library like iTextSharp. Since this forum is for C#-specific questions only you'll need to post questions related to the third party library in their forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, March 27, 2019 4:58 PM
    Moderator
  • Hi, it is possible using Spire.PDF, here are some clues involved. 

    Step 1. Horizontally and vertically split a PDF page into multiple pages.

    https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Page-Setting/Horizontally-and-Vertically-Split-a-PDF-Page-into-multiple-Pages-in-C.html

    Step 2. Convert the multiple PDF pages into images

    https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-PDF-Page-to-Image-with-C-code.html

    Step 3. Create a new PDF document and draw the selected images at the specified position of one page. I didn't find a proper link in their tutorials for this task, but I am sure it can be achieved. 


    Thursday, March 28, 2019 2:15 AM
  • Hi 

    Thank you for posting here.

    For your question, you want to merge 4 PDF files into a pdf file.

    I used itextsharp, you could refer to the following code.

    static void Main(string[] args)
            {
                string[] file = { "D:\\pdf\\1.pdf", "D:\\pdf\\2.pdf", "D:\\pdf\\3.pdf", "D:\\pdf\\4.pdf" };
                string outputfile = "D:\\pdf\\merge.pdf";
                MergePDFs(file,outputfile);
    
            }
            public static void  MergePDFs(IEnumerable<string> fileNames, string targetPdf)
            {
                using (FileStream stream = new FileStream(targetPdf, FileMode.Create))
                {
                    Document document = new Document();
                    PdfCopy pdf = new PdfCopy(document, stream);
                    PdfReader reader = null;
                    try
                    {
                        document.Open();
                        foreach (string file in fileNames)
                        {
                            reader = new PdfReader(file);
                            pdf.AddDocument(reader);
                            reader.Close();
                        }
                    }
                    catch (Exception)
                    {
                        if (reader != null)
                        {
                            reader.Close();
                        }
                    }
                    finally
                    {
                        if (document != null)
                        {
                            document.Close();
                        }
                    }
                }
            }

    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.



    Thursday, March 28, 2019 8:21 AM
    Moderator
  • I will crop the main PDF file (Screenshot 1) and then I'll adjust it like Screenshot 2

    Best Regards 

    Altaf Hossain


    Thursday, March 28, 2019 1:11 PM
  • Hi

    Thanks for the feedback.

    Based on my search, I could not find a way to crop the main PDF file that contains tables. However, you could try to create the pdf to get your goals. 

    Code:

     static void Main(string[] args)
            {
                
                string path1 = "D:\\newpdf\\1.pdf";
                string path2= "D:\\newpdf\\2.pdf";
                string path3= "D:\\newpdf\\3.pdf";
                string path4= "D:\\newpdf\\4.pdf";
                createpdf(path1,"1","5","9","13");
                createpdf(path2, "2", "6", "10", "14");
                createpdf(path3, "3", "7", "11", "15");
                createpdf(path4, "4", "8", "12", "16");
                string[] file = {path1,path2,path3,path4 };
                string outputfile = "D:\\newpdf\\result.pdf";
                MergePDFs(file, outputfile);
    
    
            }
            public static void  createpdf(string path,string t1,string t2,string t3,string t4)
            {
                Document doc = new Document(PageSize.A4, 10, 10, 10, 10);
                var output = new FileStream(path, FileMode.Create);
                var writer = PdfWriter.GetInstance(doc, output);
                doc.Open();
                PdfPTable table = new PdfPTable(2);
                table.DefaultCell.FixedHeight = 400f;
                PdfPCell cell = new PdfPCell(new Phrase("Page"));
                cell.Colspan = 2;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.AddCell(cell);
                BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 45, iTextSharp.text.Font.BOLD);
                table.AddCell(new Paragraph(t1, font));
                table.AddCell(new Paragraph(t2, font));
                table.AddCell(new Paragraph(t3, font));
                table.AddCell(new Paragraph(t4, font));
                doc.Add(table);
                doc.Close();
            }

    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.

    Friday, March 29, 2019 8:55 AM
    Moderator
  • There are excellent command-line tools for manipulating PDF files like this, including cpdf (https://community.coherentpdf.com/), which is the one I use.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Sunday, March 31, 2019 7:55 AM