page 201

How to remove page breaks in a worksheet

2017-02-23 07:25:37 Written by Koohji

We have already demonstrated how to add page breaks in Excel worksheet in C# with the help of Spire.XLS. Spire.XLS supports to remove all the horizontal and vertical page breaks and it also supports to remove the special page breaks. Here comes to the steps of how to remove the page breaks from an Excel worksheet.

Firstly, view the same Excel document with horizontal page breaks and vertical page breaks:

How to remove page breaks in a worksheet

Step 1: Initialize an instance of Workbook and load the document from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Get the first worksheet from the workbook.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Clear all the vertical page breaks by call the VPageBreaks.Clear() method.

sheet.VPageBreaks.Clear();

Step 4: Remove the specified horizontal Page Break by call the HPageBreaks.RemoveAt() method.

sheet.HPageBreaks.RemoveAt(0);

Step 5: Set the ViewMode as Preview to see how the page breaks work.

sheet.ViewMode = ViewMode.Preview;

Step 6: Save the document to file.

workbook.SaveToFile("RemovePageBreak.xlsx", FileFormat.Version2010);

Effective screenshot of removing the page breaks in a worksheet:

How to remove page breaks in a worksheet

Full codes:

using Spire.Xls;
namespace RemovePageBreak
{
    class Program
    {

        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            Worksheet sheet = workbook.Worksheets[0];

            //sheet.HPageBreaks.Clear();
            sheet.VPageBreaks.Clear();

            sheet.HPageBreaks.RemoveAt(0);


            sheet.ViewMode = ViewMode.Preview;

            workbook.SaveToFile("RemovePageBreak.xlsx", FileFormat.Version2010);
        }
    }
}

Replace font(s) in PDF document

2017-02-22 09:12:35 Written by Koohji

Spire.PDF supports the functionality to replace font(s) used in PDF document. The following parts shows how we can use Spire.PDF to replace all the fonts used in an existing PDF document with another alternate font in C# and VB.NET.

Screenshot before replacing font:

How to replace font(s) in PDF document

Code snippets:

Step 1: Instantiate an object of PdfDocument class and load the PDF document.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"E:\Program Files\input.pdf");

Step 2: Use the UsedFonts attribute of PdfDocument class to get all the fonts used in the document.

PdfUsedFont[] fonts = doc.UsedFonts;

Step 3: Create a new PDF font. Loop through the fonts and call PdfUsedFont.replace() method to replace them with the new font.

PdfFont newfont = new PdfFont(PdfFontFamily.TimesRoman, 11f, PdfFontStyle.Italic | PdfFontStyle.Bold);
foreach (PdfUsedFont font in fonts)
{
    font.Replace(newfont);
}

Step 4: Save the resultant document.

doc.SaveToFile("output.pdf");

Screenshot after replacing font:

How to replace font(s) in PDF document

Full code:

[C#]
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Graphics.Fonts;

namespace Replace_font_in_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"E:\Program Files\input.pdf");
            PdfUsedFont[] fonts = doc.UsedFonts;
            PdfFont newfont = new PdfFont(PdfFontFamily.TimesRoman, 11f, PdfFontStyle.Italic | PdfFontStyle.Bold);
            foreach (PdfUsedFont font in fonts)
            {
                font.Replace(newfont);
            }

            doc.SaveToFile("output.pdf");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Graphics.Fonts

Namespace Replace_font_in_PDF
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New PdfDocument()
			doc.LoadFromFile("E:\Program Files\input.pdf")
			Dim fonts As PdfUsedFont() = doc.UsedFonts
			Dim newfont As New PdfFont(PdfFontFamily.TimesRoman, 11F, PdfFontStyle.Italic Or PdfFontStyle.Bold)
			For Each font As PdfUsedFont In fonts
				font.Replace(newfont)
			Next

			doc.SaveToFile("output.pdf")
		End Sub
	End Class
End Namespace

C#/VB.NET: Flatten Form Fields in PDF

2022-08-15 09:07:00 Written by Koohji

Flattening form fields is an efficient way to prevent others from modifying or deleting the form field contents in PDF. After flattening, the editing or filling capability of the form fields will be removed and their contents will appear as regular text. In this article, you will learn how to flatten form fields in a PDF document in C# and VB.NET using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Flatten a Specific Form Field in PDF in C# and VB.NET

The following are the steps to flatten a specific form field in a PDF document using Spire.PDF for .NET:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the form widget collection from the document.
  • Get a specific form field from the widget collection by its name or index through PdfFormWidget.FieldsWidget["fieldName"] property or PdfFormWidget.FieldsWidget.List[fieldIndex] property.
  • Flatten the form field through PdfField.Flatten property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Widget;

namespace FlattenSpecificFormField
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile("Form.pdf");

            //Get the form widget collection
            PdfFormWidget formWidget = (PdfFormWidget)pdf.Form;
            //Get a specific form field by its name
            PdfField form = formWidget.FieldsWidget["Address"];
            //Get a specific form field by its index
            //PdfField form = formWidget.FieldsWidget.List[2] as PdfField;
            //Flatten the form
            form.Flatten = true;

            //Save the result document
            pdf.SaveToFile("FlattenSpecific.pdf");
        }
    }
}

C#/VB.NET:  Flatten Form Fields in PDF

Flatten All Form Fields in PDF in C# and VB.NET

The following are the steps to flatten all the form fields in a PDF document using Spire.PDF for .NET:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Flatten all the form fields in the document through PdfDocument.Form.IsFlatten property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace FlattenAllFormFields
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile("Form.pdf");

            //Flatten all the forms in the document
            pdf.Form.IsFlatten = true;

            //Save the result document
            pdf.SaveToFile("FlattenAll.pdf");
        }
    }
}

C#/VB.NET:  Flatten Form Fields in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

page 201