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);
}
}
}