C # Add, delete pdf blank page

Source: Internet
Author: User
Tags map class blank page

PDF documents are widely used in e-books, product descriptions, corporate announcements, web materials, e-mail and other fields and occasions. We may encounter situations where you need to add a document page to add content, or delete a blank page in a document, and so on, so this article will show you how to add, remove, and delete pdf blank pages in C #.

Example Highlights

1. Add PDF blank Page
1.1 Add a blank page in the default location (at the end of the document)
1.2 Add a blank page at the specified location
2. Delete blank pages

Tool Use

In this example, the free version of the. NET PDF control is used for spire.pdf. NET (when adding a DLL reference, you can get the DLL file in the Bin folder under the installation path)

Test document (document contains two pages):

1. Add PDF blank page 1.1 Insert a blank page at the end of the document in the default location

C#

            //创建PDF文档1,并加载测试文档            PdfDocument doc1 = new PdfDocument();            doc1.LoadFromFile("sample.pdf");            //添加一页空白页到文档(默认在文档最后一页添加)            doc1.Pages.Add();            //保存并打开文档            doc1.SaveToFile("result1.pdf");            System.Diagnostics.Process.Start("result1.pdf");

Test results:

1.2 Insert a blank page at the specified location

C#

           //创建文档2,加载测试文档            PdfDocument doc2 = new PdfDocument();            doc2.LoadFromFile("sample.pdf");            //添加一页空白页作为第2页            doc2.Pages.Insert(1);            //保存并打开文档            doc2.SaveToFile("result2.pdf");            System.Diagnostics.Process.Start("result2.pdf");

Test results:

2. Delete PDF blank page

Test Document:

The test document here contains two blank pages, one blank page without any content, and the other blank page is a page with a blank image, seemingly without content, but such a page is actually not needed.
C#

Using spire.pdf;using system.drawing;using Spire.pdf.graphics;namespace deleteblankpage_pdf{class Program { static void Main (string[] args) {//Application license Spire.License.LicenseProvider.SetLicenseFileName ("L            Icense.elic.xml ");            Create a Pdfdocument class object and load the PDF pdfdocument document = new Pdfdocument (); Document.            LoadFromFile ("Test.pdf"); Traverse all pages in the document for (int i = document. pages.count-1; I >= 0; i--) {//Diagnostic page is a blank page if (document. Pages[i]. IsBlank ()) {//delete blank page document.                Pages.removeat (i); } else {//convert PDF pages to bitmap images image = document.                    Saveasimage (i, pdfimagetype.bitmap);                       Whether the diagnostic picture is a blank picture if (Isimageblank (image)) {//Remove a page that contains a blank picture Document.                    Pages.removeat (i); }}}//Save and open document documents.            SaveToFile ("Removeblankpage.pdf", fileformat.pdf);        System.Diagnostics.Process.Start ("Removeblankpage.pdf"); }//Custom method Isimageblank () diagnose whether the picture is blank picture public static bool Isimageblank (image image) {//Initialize bit            Map class instance, traversing all the pictures in the document Bitmap Bitmap = new Bitmap (image); for (int i = 0; i < bitmap. Width; i++) {for (int j = 0; J < bitmap. Height; J + +) {Color pixel = bitmap.                    GetPixel (i, j); if (pixel. R < 240 | | Pixel. G < 240 | | Pixel.                    B <) {return false;        }}} return true; }    }}

Test results:

Attached: vb.net code (delete pdf blank page)

Imports spire.pdfimports system.drawingimports Spire.Pdf.GraphicsNamespace deleteblankpage_pdf Class program Pri Vate Shared Sub Main (ByVal args as String ()) Spire.License.LicenseProvider.SetLicenseFileName ("License.elic.xml ") Dim document as Pdfdocument = New pdfdocument () document. LoadFromFile ("Test.pdf") for I as Integer = document. Pages.count-1 to 0 If document. Pages (i). IsBlank () then document. Pages.removeat (i) Else Dim image as Image = document. Saveasimage (i, Pdfimagetype.bitmap) If isimageblank (image) then document. Pages.removeat (i) End If End If Next document.        SaveToFile ("Removeblankpage.pdf", Fileformat.pdf) System.Diagnostics.Process.Start ("Removeblankpage.pdf") End Sub public Shared Function Isimageblank (ByVal image as image) as BooleaN Dim bitmap as Bitmap = New Bitmap (image) for I as Integer = 0 to bitmap. Width-1 for j as Integer = 0 to bitmap. Height-1 Dim pixel as Color = bitmap. GetPixel (i, j) If Pixel. R < OrElse pixel. G < OrElse pixel.            B < then Return False End If Next Next Return True End Function End Classend Namespace

All of the above for the "C # Add, delete pdf blank page" of the entire content, welcome reprint (Reproduced please specify the source)
Thanks for reading!

C # Add, delete pdf blank page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.