We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

ASPOS PDF - merging PDF pages in one file to create another file

I have 2 page pdf document. Each page size is 8.5 X 11 inch. I want to convert these 2 pages to one page of size 8.5inch X 22 inch. what function I should use. Any Example on it?

@djsanty

Please check below documentation section in order to achieve your requirements:

Thanks for your reply. I did try the solution suggested by you. But the two pages does not merge to become one page. It just increase the page size of first page with lot of blank space on top. Second page still maintain the same size.

@djsanty

Please share your sample PDF document along with an expected output PDF file. We will test the scenario in our environment and address it accordingly.

public string PDFMerge(string docpath,string spath, int height)
{
string currentDirectory = Path.GetDirectoryName(docpath);
string fname = Path.GetFileNameWithoutExtension(spath);
string fPath = currentDirectory + “\” + fname + “.pdf”;

        SetLicense();

        Aspose.Pdf.Document pdfdoc = new Aspose.Pdf.Document(docpath);

        //PdfFileMend mender = new PdfFileMend();
      //  mender.BindPdf(pdfdoc);

        Aspose.Pdf.PageCollection pgc = pdfdoc.Pages;

      //  int pgCnt = pgc.Count;
   //     int i = 0;

        Page pdfpage = pgc[1];

        double wdth = pdfpage.GetPageRect(false).Width;
        //double aht = pdfpage.GetPageRect(false).Height;
        
        double rht = Convert.ToDouble(height * 72 *0.001 ); // height is in Inches * 1000


        //double rblnksp = aht - rht;

        pdfpage.SetPageSize(wdth, rht);

        pdfdoc.Save(fPath);
        return rblnksp.ToString()+"--"+ aht.ToString() +"--"+ rht.ToString() ;
    }

Outputfile.pdf (13.9 KB)
Inputfile.pdf (13.6 KB)

@djsanty

In this case, you can use PdfPageStamp Class to add content of both pages on a single page:

Document doc = new Document();
doc.Pages.Add();

Document stampedDoc = new Document(dataDir + "Inputfile.pdf");
doc.Pages[1].PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
doc.Pages[1].SetPageSize(PageSize.A4.Width, 2000);
var page = stampedDoc.Pages[1];
PdfPageStamp stamp = new PdfPageStamp(page);
stamp.Height = stampedDoc.Pages[1].GetPageRect(false).Height;
stamp.Width = stampedDoc.Pages[1].GetPageRect(false).Width;
stamp.VerticalAlignment = VerticalAlignment.Top;
stamp.HorizontalAlignment = HorizontalAlignment.Center;
doc.Pages[1].AddStamp(stamp);
stamp = new PdfPageStamp(stampedDoc.Pages[2]);
stamp.Height = stampedDoc.Pages[2].GetPageRect(false).Height;
stamp.Width = stampedDoc.Pages[2].GetPageRect(false).Width;
stamp.VerticalAlignment = VerticalAlignment.Bottom;
stamp.HorizontalAlignment = HorizontalAlignment.Left;
doc.Pages[1].AddStamp(stamp);
doc.Save(dataDir + "PdfPageStamped.pdf");

Thanks Asad for your solution. It worked for me. One more question.
I have two page document which I want to merge. The second page, even though height is 11inch, a blank space is at the bottom. I want to crop second page till the last line of text and then merge with first page. Is their function which calculates actual height of second page till the last line of text?

@djsanty

Aspose.PDF offers a way to trim white spaces around a page in a PDF document. You can first trim the white space, save the PDF and then merge the page content as per your requirements.

Thanks Asad.
I trimmed the pages as suggested by you. It cuts out left side and right letter on 2nd page onward.
Also, when I stitch pages, the first page get aligned to bottom even it is specified as Top.

File 3776664_891.pdf is input file. I need to convert this multipage document to one page without any blank spaces in between pages(paragraphs in final page). Thanks and appreciate your help.

3776664_891.pdf (13.5 KB)

@djsanty

Will you please also share the code snippet that you have tried so far to merge the page contents? We will test the scenario accordingly and share our feedback with you.

Thanks asad. Code is below

public string DocToRTFConvert(string docpath,string spath, int height)
{
string currentDirectory = Path.GetDirectoryName(docpath);
string fname = Path.GetFileNameWithoutExtension(spath);
string fPath = currentDirectory + “\” + fname + “.pdf”;

        SetLicense();

        double ht = trimAndSave(docpath);

         Aspose.Pdf.Document pdfdoc = new Aspose.Pdf.Document();
         pdfdoc.Pages.Add();

         // stitching
         Document stampedDoc = new Document(docpath);
         pdfdoc.Pages[1].PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
         pdfdoc.Pages[1].SetPageSize(4*72, ht);

        double pght = 0.00;
         
        for (int pgno = 1; pgno <= stampedDoc.Pages.Count; pgno++) //
        {
            var page = stampedDoc.Pages[pgno];
            PdfPageStamp stamp = new PdfPageStamp(page);

            stamp.YIndent = ht - pght; 

            stamp.Height = stampedDoc.Pages[pgno].GetPageRect(false).Height;
            stamp.Width = stampedDoc.Pages[pgno].GetPageRect(false).Width;

            pght = pght + stampedDoc.Pages[pgno].GetPageRect(false).Height;

            stamp.HorizontalAlignment = HorizontalAlignment.Center;

            pdfdoc.Pages[1].AddStamp(stamp);                
        }

         pdfdoc.Save(fPath);

         Document pdfdoc1 = new Document(fPath);
         Aspose.Pdf.PageCollection pgc = pdfdoc1.Pages;
         Page pdfpage = pgc[1];
         double pg1ht = pdfpage.GetPageRect(false).Height;
         pdfdoc1.Dispose();

        return "";// ht.ToString()+"--"+ stampedDoc.Pages.Count.ToString(); 
    }

public double trimAndSave(string idocpath)
{
double docht = 0.00;

        // Load source PDF file
        Document doc = new Document(idocpath);

        // Render the page to image with 72 DPI
        PngDevice device = new PngDevice(new Resolution(72));

        for (int pageno = 1; pageno <= doc.Pages.Count; pageno++)
        {
            using (MemoryStream imageStr = new MemoryStream())
            {
                device.Process(doc.Pages[pageno], imageStr);
                Bitmap bmp = (Bitmap)Bitmap.FromStream(imageStr);
                System.Drawing.Imaging.BitmapData imageBitmapData = null;

                // Determine white areas
                try
                {
                    imageBitmapData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
                    System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                    Aspose.Pdf.Rectangle prevCropBox = doc.Pages[1].CropBox;
                    int toHeight = bmp.Height;
                    int toWidth = bmp.Width;
                    int? leftNonWhite = null;
                    int? rightNonWhite = null;
                    int? topNonWhite = null;
                    int? bottomNonWhite = null;

                    for (int y = 0; y < toHeight; y++)
                    {
                        byte[] imageRowBytes = new byte[imageBitmapData.Stride];

                        // Copy the row data to byte array
                        if (IntPtr.Size == 4)
                        {
                            System.Runtime.InteropServices.Marshal.Copy(new IntPtr(imageBitmapData.Scan0.ToInt32() + y * imageBitmapData.Stride), imageRowBytes, 0, imageBitmapData.Stride);
                        }
                        else
                        {
                            System.Runtime.InteropServices.Marshal.Copy(new IntPtr(imageBitmapData.Scan0.ToInt64() + y * imageBitmapData.Stride), imageRowBytes, 0, imageBitmapData.Stride);
                        }

                        int? leftNonWhite_row = null;
                        int? rightNonWhite_row = null;

                        for (int x = 0; x < toWidth; x++)
                        {
                            if (imageRowBytes[x * 4] != 255
                            || imageRowBytes[x * 4 + 1] != 255
                            || imageRowBytes[x * 4 + 2] != 255)
                            {
                                if (leftNonWhite_row == null)
                                    leftNonWhite_row = x;
                                rightNonWhite_row = x;
                            }
                        }

                        if (leftNonWhite_row != null || rightNonWhite_row != null)
                        {
                            if (topNonWhite == null)
                                topNonWhite = y;
                            bottomNonWhite = y;
                        }

                        if (leftNonWhite_row != null && (leftNonWhite == null || leftNonWhite > leftNonWhite_row))
                        {
                            leftNonWhite = leftNonWhite_row;
                        }

                        if (rightNonWhite_row != null && (rightNonWhite == null || rightNonWhite < rightNonWhite_row))
                        {
                            rightNonWhite = rightNonWhite_row;
                        }
                    }

                    leftNonWhite = leftNonWhite ?? 0;
                    rightNonWhite = rightNonWhite ?? toWidth;
                    topNonWhite = topNonWhite ?? 0;
                    bottomNonWhite = bottomNonWhite ?? toHeight;

                    // Set crop box with correction to previous crop box

                    doc.Pages[pageno].CropBox = 
                        new Aspose.Pdf.Rectangle(
                    leftNonWhite.Value + prevCropBox.LLX,
                    (toHeight + prevCropBox.LLY) - bottomNonWhite.Value,
                    rightNonWhite.Value + doc.Pages[1].CropBox.LLX,
                    (toHeight + prevCropBox.LLY) - topNonWhite.Value
                    );
                }

                finally
                {
                    if (imageBitmapData != null)
                        bmp.UnlockBits(imageBitmapData);
                }
            }

            docht += doc.Pages[pageno].GetPageRect(false).Height;
        }

        // Save the document
        doc.Save(idocpath);
        return docht;
    }

@djsanty

We are checking it and will get back to you shortly.

@djsanty

We tested the case and achieved the attached output. Can you please share the output generated at your end? Also, please share an expected output PDF as well for our reference so that we can further proceed to assist you accordingly.output.pdf (15.9 KB)

Output should look like attached file without blank space in between. This is a sample.

2972453.pdf (11.1 KB)

The output file generated is not right. All pages are either top aligned and the first page is missing.

@djsanty

We are testing the case and will get back to you shortly.