Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri May 16, 2014 10:30 am

Hi,

How can I remove the blank pages from a PDF file?

Is it posible?

Thanks.

Best regards

inyazar
 
Posts: 24
Joined: Wed Jul 03, 2013 8:38 am

Mon May 19, 2014 6:10 am

Hello,

Thanks for your inquiry.
Sorry that currently it is impossible to remove the blank pages from a PDF file with an accurate method. And if there are no special elements on the pdf page, you can try the following method.
Code: Select all
static void RemoveBlankPage(string fileName, string newFile)
        {
            using (PdfDocument originalDoc = new PdfDocument(fileName))
            {
                for (int i = 0; i <originalDoc.Pages.Count; i++)
                {
                    PdfPageBase originalPage = originalDoc.Pages[i];
                    string text = originalPage.ExtractText();
                    Image[] imgs = originalPage.ExtractImages();
                    if ((text == null || text.Trim().Length == 0)
                        && (imgs == null || imgs.Length == 0))
                    {
                        originalDoc.Pages.Remove(originalPage);
                    }
                }
                originalDoc.SaveToFile(newFile);
            }
        }



Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Wed Jun 04, 2014 3:42 am

Hello,

The function to remove blank pages in PDF has been added in the newest hotfix, you can download Spire.PDF Pack(Hot Fix) Version:3.0.43 and test the following method.
Code: Select all
static void RemoveBlankPage(string fileName, string newFile)
{
using (PdfDocument originalDoc = new PdfDocument(fileName))
{
for (int i = 0; i <originalDoc.Pages.Count; i++)
{
PdfPageBase originalPage = originalDoc.Pages[i];
if (originalPage.IsBlank())

{ originalDoc.Pages.Remove(originalPage); i--; }
}
originalDoc.SaveToFile(newFile);
}
}

If there are any questions, welcome to get it back to us.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Wed Sep 04, 2019 9:06 am

Hi,

How can I remove the blank spaces( Not a Page) from a PDF file?

Is it posible?

prism
 
Posts: 20
Joined: Thu Aug 15, 2019 11:16 am

Wed Sep 04, 2019 9:44 am

Hi,

Thanks for your inquiry.
Sorry that there is no way to remove blank space.
If there is any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Sep 04, 2019 10:34 am

Hi, I have done convert multiple URL in a single PDF using Spire Pdf. But the problem was the first page have more blank spaces. I want to remove blank space and replace next page content in that blank space. Is it possible?

prism
 
Posts: 20
Joined: Thu Aug 15, 2019 11:16 am

Thu Sep 05, 2019 2:05 am

Hi,

Thanks for your inquiry.
Sorry that we don't have methods to remove blank space directly from PDF.
When converting URL to PDF with Spire, the layout of result PDF should be same as your original URL. The blank spaces issue occurs when conversion, right? Could you please provide following information for accurate investigation?
1. Input URL
2. The code you used
3. The desired result you want.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Sep 05, 2019 5:15 am

I am share my code in this post,

Code: Select all
List<string> Url = new List<string>();
Url.Add("https://rarunkumar454.github.io/sample_grid/");
Url.Add("https://rarunkumar454.github.io/sample_grid/");

string date = DateTime.Now.ToString("MM_dd_yyyy") + "Time_" + DateTime.Now.ToString("HHmmss");

List<MemoryStream> MemStream = new List<MemoryStream>();
foreach (var item in Url)
  {
     MemoryStream ms1 = new MemoryStream();
     HtmlConverter.Convert(item, ms1, true, 100 * 1000, new SizeF(900, 700), new PdfMargins(60, 60));
     MemStream.Add(ms1);
  }
string outputFile = $"C:/Users/Desktop/Sample_({date}).pdf";
PdfDocumentBase doc = PdfDocument.MergeFiles(MemStream.ToArray());
doc.Save(outputFile, FileFormat.PDF);


This is my code developed on .NET core 3.0 in c#. I have attached Expected sample and actual output sample.

Thanks

With Regards
Arunkumar.

prism
 
Posts: 20
Joined: Thu Aug 15, 2019 11:16 am

Thu Sep 05, 2019 7:07 am

Hi,

Thanks for your information.
According to your expected sample, I found there is a way to achieve it. That is drawing content on the PDF1. Please refer to following code:
Code: Select all
            List<string> Url = new List<string>();
            Url.Add("https://rarunkumar454.github.io/sample_grid/");
            Url.Add("https://rarunkumar454.github.io/sample_grid/");
            string date = DateTime.Now.ToString("MM_dd_yyyy") + "Time_" + DateTime.Now.ToString("HHmmss");
            List<MemoryStream> MemStream = new List<MemoryStream>();
            foreach (var item in Url)
            {
                MemoryStream ms1 = new MemoryStream();
                HtmlConverter.Convert(item, ms1, true, 100 * 1000, new SizeF(900, 700), new PdfMargins(60, 60));
                MemStream.Add(ms1);
            }
            PdfDocument doc1 = new PdfDocument(MemStream[0]);
            PdfDocument doc2 = new PdfDocument(MemStream[1]);
            //Don't use merge method, but use DrawTemplate method to draw the content on PDF 1.
            doc1.Pages[0].Canvas.DrawTemplate(doc2.Pages[0].CreateTemplate(), new PointF(0, 300));
            doc1.SaveToFile("18790.pdf");

Any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Sep 05, 2019 10:58 am

Thanks, How can I perform DrawTemplate method in a foreach loop, Because URL is not only two some times count will increase like 4,10 or 20.

prism
 
Posts: 20
Joined: Thu Aug 15, 2019 11:16 am

Fri Sep 06, 2019 10:03 am

Hi,

Please refer to following code snippet. Maybe you need to change the code by yourself according to your actual situation.
Code: Select all
            List<MemoryStream> mergedMemorys = new List<MemoryStream>();
            for (int i = 0; i < MemStream.Count; i = i + 2)
            {
                PdfDocument doc1 = new PdfDocument(MemStream[i]);
                PdfDocument doc2 = new PdfDocument(MemStream[i + 1]);
                doc1.Pages[0].Canvas.DrawTemplate(doc2.Pages[0].CreateTemplate(), new PointF(0, 300));
                MemoryStream result = new MemoryStream();
                doc1.SaveToStream(result);
                mergedMemorys.Add(result);
            }
            string outputFile = "result.pdf";
            PdfDocumentBase doc = PdfDocument.MergeFiles(mergedMemorys.ToArray());
            doc.Save(outputFile, FileFormat.PDF);


Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Sep 06, 2019 10:44 am

Thank you very much. I did this task.

prism
 
Posts: 20
Joined: Thu Aug 15, 2019 11:16 am

Mon Sep 09, 2019 1:43 am

Hi,

Thanks for your feedback.
Any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.PDF

cron