MVC view to pdf a second blank page is added

MVC view to pdf a second blank page is added. This page is good but why is there a second page added. I searched aspose there other posts say it is margins. I changed the margins down to 1 for all 4 sides and still get second page. Also some posts say it is the page width and length and I am using A4 width and length. I want 8 1/2 x 11 so that is why I picked A4.

@{
Layout = null;
}

test
basic test
                // ASPOSE 
                Aspose.Pdf.License license = new Aspose.Pdf.License();
                license.SetLicense("Aspose.Pdf.lic");
                string html = ViewRenderer.RenderView("/myProject/Views/Home/GeneratePDFaspose2.cshtml", Session["viewModel"]);

                // convert string to stream
                byte[] byteArray = Encoding.UTF8.GetBytes(html);
                //byte[] byteArray = Encoding.ASCII.GetBytes(contents);
                MemoryStream stream = new MemoryStream(byteArray);

                // import an HTML document
                Aspose.Pdf.HtmlLoadOptions options = new Aspose.Pdf.HtmlLoadOptions();
                options.PageInfo.IsLandscape = false;
                options.PageInfo.Margin.Top = 25;
                options.PageInfo.Margin.Left = 25;
                options.PageInfo.Margin.Right = 25;
                options.PageInfo.Margin.Bottom = 25;
                options.PageInfo.Width = Aspose.Pdf.PageSize.A4.Width;
                options.PageInfo.Height = Aspose.Pdf.PageSize.A4.Height;
                Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(stream, options);
                pdfDocument.Pages.Add();

                ArrayList watermark = new ArrayList();
                watermark.Add("UNOFFICIAL");
                //watermark.Add(" ");
                //watermark.Add(" ");
                //watermark.Add("UNOFFICIAL");
                //watermark.Add(" ");
                //watermark.Add(" ");
                //watermark.Add("UNOFFICIAL");

                // arrange multiline text stamp text
                FormattedText ft = new FormattedText(watermark[0].ToString(), System.Drawing.Color.LightGray, "Helvetica", EncodingType.Winansi, false, 80);
                for (int i = 1; i < watermark.Count; i++)
                    ft.AddNewLineText(watermark[i].ToString());

                TextStamp stamp = new TextStamp(ft);
                stamp.Background = true;
                stamp.Opacity = 0.5;
                stamp.RotateAngle = -45;
                //stamp.TextAlignment = HorizontalAlignment.Left;
                stamp.TextAlignment = HorizontalAlignment.Center;
                //stamp.HorizontalAlignment = HorizontalAlignment.Left;
                stamp.HorizontalAlignment = HorizontalAlignment.Center;
                //stamp.VerticalAlignment = VerticalAlignment.Center;

                // add stamp to page collection
                pdfDocument.Pages[1].AddStamp(stamp);

                MemoryStream stream2 = new MemoryStream();
                pdfDocument.Save(stream2, Aspose.Pdf.SaveFormat.Pdf);
                return File(stream2.ToArray(), "application/pdf", "myFile.pdf");

How do I show you the html code on your forum.aspose.com ?

htmlView.JPG (12.9 KB)

@LoganJH,
You can update page size by calling SetPageSize() method of the Page class. The PageInfo class is only to generate the PDF. Please refer to this help topic: Update Page Dimensions. However, if this does not help, then kindly create a small MVC project, which reproduces this problem in your environment, and then send us a Zip of this project (Aspose forums allow to upload Zip file). We will investigate and share our findings with you.

I created a test project for you, because my real project has private company information. My test project is getting an error in aspose code and I don’t know how to fix that. The error is on line “var view = viewEngineResult.View;”

Here is the link to my zipped project: https://drive.google.com/open?id=0B-meJm_Kg8TwT2poOWY3M0o3Nnc

@LoganJH,
You are using ViewRenderer class to convert a view to an HTML string and the problematic line of code returns a null view object. It is not related to the Aspose.Pdf for .NET API. Kindly investigate the root cause of this error and let us know if Aspose.Pdf for .NET API does not work as expected.

I fixed the path to the view. My example now works but it still shows a second blank page. What am I doing wrong?

Here is the link to the new updated test project: https://drive.google.com/open?id=0B-meJm_Kg8TwX2F5MjBjODVCQ1k

@LoganJH,
You are adding an additional blank page in the PDF document. Please modify your code as below:

[C#]

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(stream, options);
//we have comment this line of code and also do not set second page size below
//pdfDocument.Pages.Add(); 
ArrayList watermark = new ArrayList();
watermark.Add("UNOFFICIAL");
// arrange multiline text stamp text
FormattedText ft = new FormattedText(watermark[0].ToString(), System.Drawing.Color.LightGray, "Helvetica", EncodingType.Winansi, false, 80);
for (int i = 1; i < watermark.Count; i++)
    ft.AddNewLineText(watermark[i].ToString());

TextStamp stamp = new TextStamp(ft);
stamp.Background = true;
stamp.Opacity = 0.5;
stamp.RotateAngle = -45;
stamp.TextAlignment = HorizontalAlignment.Center;
stamp.HorizontalAlignment = HorizontalAlignment.Center;
stamp.VerticalAlignment = VerticalAlignment.Center;

// add stamp to page collection
pdfDocument.Pages[1].AddStamp(stamp);
MemoryStream stream2 = new MemoryStream();
pdfDocument.Save(stream2, Aspose.Pdf.SaveFormat.Pdf);

// update page size
// Get page collection
PageCollection pageCollection = pdfDocument.Pages;

// Get particular page
Page pdfPage = pageCollection[1];

// Set the page size as A4 (11.7 x 8.3 in) and in Aspose.Pdf, 1 inch = 72 points
// So A4 dimensions in points will be (842.4, 597.6)
pdfPage.SetPageSize(597.6, 842.4);

//pdfPage = pageCollection[2];
//pdfPage.SetPageSize(597.6, 842.4);

Thank you, that fixed it.