Free Support Forum - aspose.com

Page size changes on conversion of excel to pdf

Hi Team,

I am trying to convert mutliple excel files to pdf and once converted i am consolodating them into one. But when i am trying to do so the sheet size changes for particular pages in the pdf. They are in Legal papersize when i convert them from excel to pdf but the paper size goes away when i am consolidating the pdfs.
below is the code i am working on.

 void GenerateConsolidatedPdf(int versionId, DataTable dt)
        {
            try
            {
                var folderPath = string.Format("abcd");
                string[] files;

                Document consolidatedPdf = new Document();
                Document inputPdfForConsolidation;
                SetAsposeLicense();
                SetAsposePdfLicense();

                GenerateCoverSheetPdf(versionId);

                files = Directory.GetFiles(folderPath, "*.pdf");

                List<String> selectedInputFiles = new List<String>();
                ArrayList index = new ArrayList();
                ArrayList pageCount = new ArrayList();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (Directory.Exists(folderPath))
                    {
                        foreach (var f in files)
                        {
                               index.Add(f);
                        }
                    }
                }

                if (Aspose.Pdf.Document.IsLicensed)
                {
                    pageCount.Add(3);
                    Document coverSheet = new Document(folderPath + "\\CoverSheet.pdf");
                    consolidatedPdf.Pages.Add(coverSheet.Pages);

                    for (int j = 0; j < selectedInputFiles.Count; j++)
                    {
                        inputPdfForConsolidation = new Document(selectedInputFiles[j]);
                        pageCount.Add(inputPdfForConsolidation.Pages.Count + Convert.ToInt32(pageCount[j]));

                        // Add the pages of the source documents to the target document
                        consolidatedPdf.Pages.Add(inputPdfForConsolidation.Pages);
                    }
                }
                consolidatedPdf.Save(folderPath + "\\Consolidated.pdf");

                // Load an existing PDF files
                Document doc = new Document(folderPath + "\\Consolidated.pdf");

                // Get access to first page of PDF file
                Page tocPage = doc.Pages[2];
                //tocPage.SetPageSize(1008, 1012.32);

                // Create object to represent TOC information
                TocInfo tocInfo = new TocInfo();
                Aspose.Pdf.Text.TextFragment title = new Aspose.Pdf.Text.TextFragment("Contents");
                title.Margin.Top = 50;
                title.Margin.Bottom = 15;
                title.Margin.Right = 550;
                title.TextState.FontSize = 30;
                title.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
                title.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
                title.TextState.HorizontalAlignment = HorizontalAlignment.Left;
                
                // Set the title for TOC
                tocInfo.Title = title;
                tocInfo.LineDash = TabLeaderType.None;
                tocPage.TocInfo = tocInfo;
                tocPage.PageInfo.Margin = new MarginInfo(60, 00, 100, 80);
                tocPage.PageInfo.IsLandscape = true;

                for (int i = 0; i < selectedInputFiles.Count; i++)
                {
                    // Create Heading object
                    Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
                    heading2.Style = NumberingStyle.LettersUppercase;
                    Aspose.Pdf.Text.TextSegment segment2 = new Aspose.Pdf.Text.TextSegment();
                    heading2.Margin.Left = 10;
                    heading2.Margin.Right = 10;
                    heading2.Margin.Bottom = 7;
                    heading2.TextState.FontSize = 10;
                    heading2.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
                    heading2.TocPage = tocPage;
                    heading2.Segments.Add(segment2);

                    // Specify the destination page for heading object
                    int count = Convert.ToInt32(pageCount[i]);
                    heading2.DestinationPage = doc.Pages[count];

                    // Destination page
                    heading2.Top = doc.Pages[Convert.ToInt32(pageCount[i])].Rect.Height;
                    heading2.HorizontalAlignment = HorizontalAlignment.Left;

                    // Destination coordinate
                    segment2.Text = Convert.ToString(i+1)+". " + Convert.ToString(index[i]);
                    // Add heading to page containing TOC
                    tocPage.Paragraphs.Add(heading2);

                }

                for (int k = 0; k < doc.Pages.Count; k++)
                {
                    PageNumberStamp pageNumberStamp = new PageNumberStamp();

                    // Whether the stamp is background
                    pageNumberStamp.Background = false;
                    pageNumberStamp.Format = "#";
                    pageNumberStamp.BottomMargin = 10;
                    pageNumberStamp.RightMargin = 10;
                    pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Right;
                    pageNumberStamp.StartingNumber = 1;

                    // Add stamp to particular page
                    doc.Pages[k + 1].AddStamp(pageNumberStamp);
                }

                // Get rectangular region of first page of PDF
                Aspose.Pdf.Rectangle rect = doc.Pages[1].Rect;

                // Instantiate PdfPageEditor instance
                Aspose.Pdf.Facades.PdfPageEditor ppe = new Aspose.Pdf.Facades.PdfPageEditor();

                // Bind source PDF
                ppe.BindPdf(folderPath + "\\Consolidated.pdf");

                // Set zoom coefficient
                ppe.Zoom = (float)(rect.Width / rect.Height);

                // Save the updated document
                doc.Save(folderPath + "\\TOC_Output.pdf");

                string fileName = (folderPath + "\\Consolidated.pdf");

                if (fileName != null || fileName != string.Empty)
                {
                    if ((System.IO.File.Exists(fileName)))
                    {
                        System.IO.File.Delete(fileName);
                    }
                }
            }

@dewan.ishi,

Thanks for your query.

This code cannot be executed as DataTable argument is there which is not available. Please share a simple console application based project which can be compiled and executed here. This project should have all the necessary data to run this project without any error. Send few sample Excel files and the output PDF file where issue can be seen. Also mention your environment details and the version of Aspose.Cells which is used in testing.

Actually the page orientation is changing when i am adding content to the page ie. when i am trying to add heading onto the page the page size changes.

@dewan.ishi,

I have tried this by writing following simple code which converts multiple Excel files (having header and footer also) to PDF files and then concatenated them into one PDF. This simple code runs fine and no change occurs in page orientation in the resultant PDF file. Please try this code at your end using latest versions Aspose.Cells for .NET 18.7 and Aspose.Pdf for .NET 18.7 and provide your feedback.

If issue is still there, please provide us a complete runnable project that compiles and executes without any error along with the template files. This will be helpful to resolve this issue at the earliest.

DirectoryInfo d = new DirectoryInfo(@"Path");//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.xlsx"); //Getting Text files
List<string> PdfFiles = new List<string>();
foreach (FileInfo file in Files)
{
    Aspose.Cells.PdfSaveOptions options = new Aspose.Cells.PdfSaveOptions(Aspose.Cells.SaveFormat.Pdf);
    Workbook wb = new Workbook( @"Path\" + file.Name);
    // Obtaining the reference of the PageSetup of the worksheet
    Aspose.Cells.PageSetup pageSetup = wb.Worksheets[0].PageSetup;

    // Setting worksheet name at the left section of the header
    pageSetup.SetHeader(0, "&A");

    // Setting current date and current time at the centeral section of the header
    // and changing the font of the header
    pageSetup.SetHeader(1, "&\"Times New Roman,Bold\"&D-&T");

    // Setting current file name at the right section of the header and changing the
    // font of the header
    pageSetup.SetHeader(2, "&\"Times New Roman,Bold\"&12&F");

    // Setting a string at the left section of the footer and changing the font
    // of a part of this string ("123")
    pageSetup.SetFooter(0, "Hello World! &\"Courier New\"&14 123");

    // Setting the current page number at the central section of the footer
    pageSetup.SetFooter(1, "&P");

    // Setting page count at the right section of footer
    pageSetup.SetFooter(2, "&N");


    wb.Save( @"Path\" + file.Name + ".pdf", options);
    PdfFiles.Add(@"Path\" + file.Name + ".pdf");
}
PdfFileEditor pdfEditor = new PdfFileEditor();
pdfEditor.Concatenate(PdfFiles.ToArray(), @"Path\output.pdf");

Hi @ahsaniqbalsidiqui : I was able to figure out how that has to be done. Thank you for putting in so much effort but i somehow resolved it myself. Thank you for all the help.

But i have another question, where i am adding a page and resizing it and there is a problem that footer ie. page number on that page comes in the middle while in all other pages it comes to the right of the page which is the requirement !!

@dewan.ishi,

Well, this might be due to the line of code:
e.g
Sample code:

.......
 // Setting the current page number at the central section of the footer
    pageSetup.SetFooter(1, "&P");

Please note there are three sections of header/footer, check the first parameter of SetFooter() method. For your information, 0 refers to left section, 1 refers to middle/center section and 2 refers to right section. So, you may try the line of code instead:
i.e.,

// Setting the current page number in the right section of the footer
pageSetup.SetFooter(2, ā€œ&Pā€);

Also, see the document for your reference on Setting headers and footers:
https://docs.aspose.com/display/cellsnet/Setting+Headers+and+Footers

Hope, this helps a bit.

@Amjad_Sahi : Hi, I am trying to achieve this is PDF and i am facing issue for a single page only.

@dewan.ishi,

If you think it is an issue with Aspose.Cells APIs, kindly separate the issue and create a simple console application (runnable) using Aspose.Cells APIs only, zip the project and post us here to reproduce the issue, we will check it soon. Also, attach your template Excel file and output PDF file (by Aspose.Cells APIs). Please remove inter dependencies regarding data source/database (if there is any) and create dynamic dataset/datatable in your code, so we could execute your code without any issue.

Actually, the issue here is i am using aspose.pdf and i am trying to merge multiple pdfs into one. After that i am adding a page into the the merged pdf and changing the size of the added page, added some content on it etc. I added the footer to the PDF on all pages in the pdf it is working fine but only on the page that is added manually the footer is coming out in the centre.

@dewan.ishi,

I am afraid, we can only help with issues regarding Aspose.Cells APIs here. We recommend you to kindly post your issue with all the details and samples in Aspose.PDF forum, one of my fellow colleagues from Aspose.PDF team will help you soon there.

oh sorry i wasnt aware of the fact. Thank you i will post there in the PDF Forum !