Knowledgebase (2345)
Children categories
Java remove the formulas but keep the values on Excel worksheet
2021-02-20 05:39:06 Written by AdministratorThis article will demonstrate how to use Spire.XLS for Java to remove the formulas but keep the values on the Excel worksheet.
Firstly, view the original Excel:

String inputFile = "Sample.xlsx";
String outputFile="output/removeFormulasButKeepValues_result.xlsx";
//Create a workbook.
Workbook workbook = new Workbook();
//Load the file from disk.
workbook.loadFromFile(inputFile);
//Loop through worksheets.
for (Worksheet sheet : (Iterable<? extends Worksheet>) workbook.getWorksheets())
{
//Loop through cells.
for (CellRange cell : (Iterable<? extends CellRange>) sheet.getRange())
{
//If the cell contains formula, get the formula value, clear cell content, and then fill the formula value into the cell.
if (cell.hasFormula())
{
Object value = cell.getFormulaValue();
cell.clear(ExcelClearOptions.ClearContent);
cell.setValue(value.toString());
}
}
}
//Save to file
workbook.saveToFile(outputFile, ExcelVersion.Version2013);
Output:

C#: Check Whether a PDF is Password Protected and Determine the Correct Password
2025-04-03 03:47:00 Written by KoohjiPassword protection is a widely used security feature in PDFs to restrict access and prevent unauthorized modifications. Before working with a PDF, it is essential to determine whether it is password-protected. If protection is enabled, verifying the correct password allows you to unlock the document, ensuring smooth access for viewing, editing, or extracting its contents.
In this article, we will guide you through the process of checking whether a PDF is password-protected and how to verify the correct password using C# and the Spire.PDF for .NET library.
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
Check Whether a PDF is Password Protected in C#
Spire.PDF for .NET provides the PdfDocument.IsPasswordProtected(string fileName) method to determine whether a PDF file is password-protected. The detailed steps are as follows.
- Specify the input and output file paths.
- Use the PdfDocument.IsPasswordProtected(string fileName) method to check whether the PDF is password protected.
- Save the verification result to a text file.
- C#
using Spire.Pdf;
using System.IO;
namespace CheckIfPdfIsProtected
{
internal class Program
{
static void Main(string[] args)
{
// Specify the input and output file paths
string pdfPath = "sample.pdf";
string resultFilePath = "verification_results.txt";
// Check whether the PDF file is password-protected
bool isProtected = PdfDocument.IsPasswordProtected(pdfPath);
// Create a StreamWriter to write the result to a text file
using (StreamWriter writer = new StreamWriter(resultFilePath))
{
// Write the verification result to the text file
string resultMessage = isProtected ? "The PDF is password-protected." : "The PDF is not password-protected.";
writer.WriteLine(resultMessage);
}
}
}
}

Determine the Correct Password of a PDF in C#
Spire.PDF for .NET does not have a direct method to verify if a password is correct, but this can be done by attempting to open the file with the given password. If the password is incorrect, an exception will be thrown. The detailed steps are as follows.
- Specify the input and output file paths.
- Check whether the PDF file is password-protected using the PdfDocument.IsPasswordProtected(string fileName) method.
- Create an array of potential passwords to test.
- Iterate through the array, and load the PDF with each password using the PdfDocument.LoadFromFile(string filename, string password) method.
- If no exception is thrown, the password is correct. Otherwise, the password is incorrect.
- Save the verification result to a text file.
- C#
using Spire.Pdf;
using System;
using System.IO;
namespace DetermineTheCorrectPasswordOfPdf
{
internal class Program
{
static void Main(string[] args)
{
// Specify the input and output file paths
string pdfPath = "sample.pdf";
string resultFilePath = "verification_results.txt";
// Check whether the PDF file is password-protected
bool isProtected = PdfDocument.IsPasswordProtected(pdfPath);
// Create an array of potential passwords to test
string[] passwords = new string[5] { "password1", "password2", "password3", "admin123", "test" };
// Create a StreamWriter to write results to a text file
using (StreamWriter writer = new StreamWriter(resultFilePath))
{
// If the PDF is protected, start testing passwords
if (isProtected)
{
// Iterate through each password in the array
for (int passwordcount = 0; passwordcount < passwords.Length; passwordcount++)
{
try
{
// Create a new PdfDocument object and try loading the document with the current password
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(pdfPath, passwords[passwordcount]);
// If successful, write that the password is correct to the text file
writer.WriteLine("Password " + passwords[passwordcount] + " is correct");
}
catch
{
// If an exception occurs, write that the password is not correct to the text file
writer.WriteLine("Password " + passwords[passwordcount] + " is not correct");
}
}
}
else
{
// If the PDF is not password protected, note this in the text file
writer.WriteLine("The PDF is not password protected.");
}
}
Console.WriteLine("Verification results have been saved to: " + resultFilePath);
Console.ReadKey();
}
}
}

Get a Free License
To fully experience the capabilities of Spire.PDF for .NET without any evaluation limitations, you can request a free 30-day trial license.
We have demonstrated how to use Spire.Doc for Java to add multiple text watermarks to word document. This article will show you how to add multiple image watermarks to the Word document with the help of Spire.Doc for Java.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;
public class WordImageWatermark {
public static void main(String[] args) throws Exception {
//Load the sample file
Document doc=new Document();
doc.loadFromFile("Sample.docx");
//Load the image
DocPicture picture = new DocPicture(doc);
picture.loadImage("Logo.png");
//Set the text wrapping style
picture.setTextWrappingStyle(TextWrappingStyle.Behind);
for (int n = 0; n < doc.getSections().getCount(); n++) {
Section section = doc.getSections().get(n);
//Get the head of section
HeaderFooter header = section.getHeadersFooters().getHeader();
Paragraph paragrapg1;
if(header.getParagraphs().getCount()>0){
paragrapg1=header.getParagraphs().get(0);
}else {
//Add the header to the paragraph
paragrapg1 = header.addParagraph();
}
for (int p = 0; p < 3; p++) {
for (int q = 0; q < 2; q++) {
//copy the image and add it to many places
picture = (DocPicture)picture.deepClone();
picture.setVerticalPosition(100 + 200 * p);
picture.setHorizontalPosition(50 + 210 * q);
paragrapg1.getChildObjects().add(picture);
}
}
}
//Save the document to file
doc.saveToFile("Result.docx", FileFormat.Docx_2013);
}
}
Output:
