Hide or display layers in PDF in Java
This article will demonstrate how to hide and display Layers in a PDF document using Spire.PDF for Java.
Hide all layers:
import com.spire.pdf.*; import com.spire.pdf.graphics.layer.*; public class invisibleAllPdfLayers { public static void main(String[] args) { //Load the sample document PdfDocument doc = new PdfDocument(); doc.loadFromFile("layerSample.pdf"); for (int i = 0; i < doc.getLayers().getCount(); i++) { //Show all the Pdf layers //doc.getLayers().get(i).setVisibility(PdfVisibility.On); //Set all the Pdf layers invisible doc.getLayers().get(i).setVisibility(PdfVisibility.Off); } //Save to document to file doc.saveToFile("output/invisibleAllPdfLayers.pdf", FileFormat.PDF); } }
Hide some of the PDF layers:
import com.spire.pdf.*; import com.spire.pdf.graphics.layer.*; public class invisibleParticularPdfLayers { public static void main(String[] args) { //Load the sample document PdfDocument doc = new PdfDocument(); doc.loadFromFile("layerSample.pdf"); //Hide the first layer by index doc.getLayers().get(0).setVisibility(PdfVisibility.Off); //Hide the layer by name with blue line1 for (int i = 0; i < doc.getLayers().getCount(); i++) { if("blue line1".equals(doc.getLayers().get(i).getName())){ doc.getLayers().get(i).setVisibility(PdfVisibility.Off); } } //Save to document to file doc.saveToFile("output/invisiblePaticularPdfLayers.pdf", FileFormat.PDF); } }
Java expand and collapse the bookmarks for PDF
This article will demonstrate how to expand or collapse the bookmarks when viewing the PDF files.
Expand all bookmarks on PDF
import com.spire.pdf.PdfDocument; public class expandBookmarks { public static void main(String[] args) { PdfDocument doc = new PdfDocument(); doc.loadFromFile("Sample.pdf"); //Set true to expand all bookmarks; set false to collapse all bookmarks doc.getViewerPreferences().setBookMarkExpandOrCollapse(true); doc.saveToFile("output/expandAllBookmarks_out.pdf"); doc.close(); } }
Output:
Expand specific bookmarks on PDF
import com.spire.pdf.PdfDocument; import com.spire.pdf.bookmarks.*; public class expandSpecificBookmarks { public static void main(String[] args) { PdfDocument doc = new PdfDocument(); doc.loadFromFile("Sample.pdf"); //Set BookMarkExpandOrCollapse as "true" for the first bookmarks doc.getBookmarks().get(0).setExpandBookmark(true); //Set BookMarkExpandOrCollapse as "false" for the first level of the second bookmarks PdfBookmarkCollection pdfBookmark = doc.getBookmarks().get(1); pdfBookmark.get(0).setExpandBookmark(false); doc.saveToFile("output/expandSpecificBookmarks_out.pdf"); doc.close(); } }
Only expand the first bookmarks
Java: Split a Worksheet into Several Excel Files
Splitting a worksheet can be beneficial when you have a large amount of data and want to organize it into separate files for easier management and sharing. By using this approach, you can organize and distribute your data in a more organized and structured manner. In this tutorial, we will demonstrate how to split a worksheet into multiple Excel documents by using Spire.XLS for Java.
Install Spire.XLS for Java
First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls</artifactId> <version>14.4.1</version> </dependency> </dependencies>
Split a Worksheet into Several Excel Files
Spire.XLS for Java provides powerful features that enable us to achieve this task efficiently. The specific steps are as follows.
- Create a Workbook object.
- Load a sample Excel file using Workbook.loadFromFile() method.
- Get the specific sheet using Workbook.getWorksheets().get() method.
- Get the header row and cell ranges using Worksheet.getCellRange() method.
- Create a new workbook and copy the header row and range 1 to the new workbook using Worksheet.copy(CellRange sourceRange, Worksheet worksheet, int destRow, int destColumn, boolean copyStyle, boolean updateRerence) method.
- Copy the column width from the original workbook to the new workbook using Workbook.getWorksheets().get(0).setColumnWidth() method.
- Save the new workbook to an Excel file using Workbook.saveToFile() method.
- Repeat the above operation to copy the header row and range 2 to another new workbook, and save it to another Excel file.
- Java
import com.spire.xls.CellRange; import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class SplitWorksheet { public static void main(String[] args) { //Create a Workbook object to load the original Excel document Workbook bookOriginal = new Workbook(); bookOriginal.loadFromFile("C:\\Users\\Administrator\\Desktop\\Emplyees.xlsx"); //Get the first worksheet Worksheet sheet = bookOriginal.getWorksheets().get(0); //Get the header row CellRange headerRow = sheet.getCellRange(1, 1, 1, 5); //Get two cell ranges CellRange range1 = sheet.getCellRange(2, 1, 6, 5); CellRange range2 = sheet.getCellRange(7, 1, 11, 5); //Create a new workbook Workbook newBook1 = new Workbook(); //Copy the header row and range 1 to the new workbook sheet.copy(headerRow, newBook1.getWorksheets().get(0), 1, 1, true, false); sheet.copy(range1, newBook1.getWorksheets().get(0), 2, 1, true, false); //Copy the column width from the original workbook to the new workbook for (int i = 0; i < sheet.getLastColumn(); i++) { newBook1.getWorksheets().get(0).setColumnWidth(i + 1, sheet.getColumnWidth(i + 1)); } //Save the new workbook to an Excel file newBook1.saveToFile("Sales.xlsx", ExcelVersion.Version2016); //Create another new workbook Workbook newBook2 = new Workbook(); //Copy the header row and range 2 to the new workbook sheet.copy(headerRow, newBook2.getWorksheets().get(0), 1, 1, true, false); sheet.copy(range2, newBook2.getWorksheets().get(0), 2, 1, true, false); //Copy the column width from the original workbook to another new workbook for (int i = 0; i < sheet.getLastColumn(); i++) { newBook2.getWorksheets().get(0).setColumnWidth(i + 1, sheet.getColumnWidth(i + 1)); } //Save it to another new Excel file newBook2.saveToFile("Technicians.xlsx", ExcelVersion.Version2016); } }
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.
Java set the viewer preference and zoom factor for PDF
This article will demonstrate how to set the zoom factor/percentage (such as default, 100 percent or any other zoom factors as required) and the viewer preference by using Spire.PDF for Java in Java applications.
Set the zoom factor
import com.spire.pdf.*; import com.spire.pdf.actions.*; import com.spire.pdf.general.*; import java.awt.geom.*; public class setZoomFactor { public static void main(String[] args) { //Load the sample document PdfDocument doc = new PdfDocument(); doc.loadFromFile("Sample.pdf"); //Get the first page of PDF PdfPageBase page = doc.getPages().get(0); //Set pdf destination PdfDestination dest = new PdfDestination(page); dest.setMode(PdfDestinationMode.Location); dest.setLocation(new Point2D.Float(-40f, -40f)); //Set zoom factor dest.setZoom(0.8f); //Set action PdfGoToAction gotoAction = new PdfGoToAction(dest); doc.setAfterOpenAction(gotoAction); //Save pdf document String output = "output/setZoomFactor.pdf"; doc.saveToFile(output); } }
Output:
Set the viewer preference
import com.spire.pdf.*; public class viewerPreference { public static void main(String[] args) { //Load the sample document PdfDocument doc = new PdfDocument(); doc.loadFromFile("Sample.pdf"); //Set viewer reference doc.getViewerPreferences().setCenterWindow(true); doc.getViewerPreferences().setDisplayTitle(false); doc.getViewerPreferences().setFitWindow(false); doc.getViewerPreferences().setHideMenubar(true); doc.getViewerPreferences().setHideToolbar(true); doc.getViewerPreferences().setPageLayout(PdfPageLayout.Two_Column_Left); //Save pdf document String output = "output/viewerPreference.pdf"; doc.saveToFile(output); } }
Output:
C#/VB.NET: Split Excel Sheets into Separate Files
A workbook containing multiple worksheets helps to centrally manage relevant information, but sometimes we have to split the worksheets into separate Excel files so that individual worksheets can be distributed without disclosing other information. In this article, you will learn how to split Excel worksheets into separate workbooks in C# and VB.NET using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Split Excel Sheets into Separate Files
The following are the main steps to split Excel sheets into separate workbooks using Spire.XLS for .NET.
- Create a Workbook object
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Declare a new Workbook variable, which is used to create new Excel workbooks.
- Loop through the worksheets in the source document.
- Initialize the Workbook object, and add the copy of a specific worksheet of source document into it.
- Save the workbook to an Excel file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls; using System; namespace SplitWorksheets { class Program { static void Main(string[] args) { //Create a Workbook object Workbook wb = new Workbook(); //Load an Excel document wb.LoadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx"); //Declare a new Workbook variable Workbook newWb; //Declare a String variable String sheetName; //Specify the folder path which is used to store the generated Excel files String folderPath = "C:\\Users\\Administrator\\Desktop\\Output\\"; //Loop through the worksheets in the source file for (int i = 0; i < wb.Worksheets.Count; i++) { //Initialize the Workbook object newWb = new Workbook(); //Remove the default sheets newWb.Worksheets.Clear(); //Add the specific worksheet of the source document to the new workbook newWb.Worksheets.AddCopy(wb.Worksheets[i]); //Get the worksheet name sheetName = wb.Worksheets[i].Name; //Save the new workbook to the specified folder newWb.SaveToFile(folderPath + sheetName + ".xlsx", ExcelVersion.Version2013); } } } }
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.
Java draw Superscript and Subscript Text in PDF
This article will show you how to use Spire.PDF for Java to draw superscript and subscript text to PDF file in Java applications.
Draw Superscript Text
import com.spire.pdf.*; import com.spire.pdf.graphics.*; import java.awt.*; import java.awt.geom.*; public class PdfSuperscriptText { public static void main(String[] args) { //Create a new PdfDocument instance PdfDocument doc = new PdfDocument(); //Add a page to pdf PdfPageBase page = doc.getPages().add(); //Set the font PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN,14),true); PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.black)); //Set initial (x, y) coordinate float x = 120f; float y = 100f; //Draw text string String text = "Sample Text"; page.getCanvas().drawString(text, font, brush, new Point2D.Float(x, y)); //Measure the string Dimension2D size = font.measureString(text); x += size.getWidth(); //Draw the text string and set the format as Superscript PdfStringFormat format = new PdfStringFormat(); format.setSubSuperScript(PdfSubSuperScript.Super_Script); text = "Superscrip"; page.getCanvas().drawString(text, font, brush, new Point2D.Float(x, y), format); //Save the document to file String result="output/superScript.pdf"; doc.saveToFile(result); } }
Effective screenshot:
Draw Subscript Text
import com.spire.pdf.*; import com.spire.pdf.graphics.*; import java.awt.*; import java.awt.geom.*; public class PdfSubscriptText { public static void main(String[] args) { //Create a new PdfDocument instance PdfDocument doc = new PdfDocument(); //Add a page to pdf PdfPageBase page = doc.getPages().add(); //Set the font PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN,14),true); PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.black)); //Set initial (x, y) coordinate float x = 120f; float y = 100f; //Draw text string String text = "Sample Text"; page.getCanvas().drawString(text, font, brush, new Point2D.Float(x, y)); //Measure the string Dimension2D size = font.measureString(text); x += size.getWidth(); //Draw the text string and set the format as Subscript PdfStringFormat format = new PdfStringFormat(); format.setSubSuperScript(PdfSubSuperScript.Sub_Script); text = "Subscrip"; page.getCanvas().drawString(text, font, brush, new Point2D.Float(x, y), format); //Save the document to file String result="output/subScript.pdf"; doc.saveToFile(result); } }
Output:
Split a Worksheet into Several Excel Files in Java
This article demonstrates how to split a worksheet into several Excel documents by using Spire.XLS for Java.
import com.spire.xls.CellRange; import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class SplitWorksheet { public static void main(String[] args) { //Create a Workbook object to load the original Excel document Workbook bookOriginal = new Workbook(); bookOriginal.loadFromFile("C:\\Users\\Administrator\\Desktop\\Emplyees.xlsx"); //Get the first worksheet Worksheet sheet = bookOriginal.getWorksheets().get(0); //Get the header row CellRange headerRow = sheet.getCellRange(1, 1, 1, 5); //Get two cell ranges CellRange range1 = sheet.getCellRange(2, 1, 6, 5); CellRange range2 = sheet.getCellRange(7, 1, 11, 5); //Create a new workbook Workbook newBook1 = new Workbook(); //Copy the header row and range 1 to the new workbook sheet.copy(headerRow, newBook1.getWorksheets().get(0), 1, 1, true, false); sheet.copy(range1, newBook1.getWorksheets().get(0), 2, 1, true, false); //Copy the column width from the original workbook to the new workbook for (int i = 0; i < sheet.getLastColumn(); i++) { newBook1.getWorksheets().get(0).setColumnWidth(i + 1, sheet.getColumnWidth(i + 1)); } //Save the new workbook to an Excel file newBook1.saveToFile("Sales.xlsx", ExcelVersion.Version2016); //Copy the header row and range 2 to another workbook, and save it to another Excel file Workbook newBook2 = new Workbook(); sheet.copy(headerRow, newBook2.getWorksheets().get(0), 1, 1, true, false); sheet.copy(range2, newBook2.getWorksheets().get(0), 2, 1, true, false); for (int i = 0; i < sheet.getLastColumn(); i++) { newBook2.getWorksheets().get(0).setColumnWidth(i + 1, sheet.getColumnWidth(i + 1)); } newBook2.saveToFile("Technicians.xlsx", ExcelVersion.Version2016); } }
Java: Change Page Size and Page Orientation in Word
Normally, the default page size of a Word document is “Letter” (8.5 x 11 inches), and the default page orientation is “Portrait”. In most cases, the general page setup can meet the needs of most users, but sometimes you may also need to adjust the page size and orientation to design a different document such as an application form, certificate, or brochure. In this article, you will learn how to programmatically change the page size and page orientation in a Word document using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.4.6</version> </dependency> </dependencies>
Change Page Size and Page Orientation in Word
The detailed steps are as follows:
- Create a Document instance.
- Load a sample Word document using Document.loadFromFile() method.
- Get the first section using Document.getSections().get() method.
- Change the default page size using Section.getPageSetup().setPageSize() method.
- Change the default page orientation using Section.getPageSetup().setOrientation() method.
- Save the document to file using Document.saveToFile() method.
- Java
import com.spire.doc.*; import com.spire.doc.documents.*; public class WordPageSetup { public static void main(String[] args) throws Exception { //Create a Document instance Document doc= new Document(); //Load a sample Word document doc.loadFromFile("sample.docx"); //Get the first section Section section = doc.getSections().get(0); //Change the page size to A3 section.getPageSetup().setPageSize(PageSize.A3); //Change the page orientation to Landscape section.getPageSetup().setOrientation(PageOrientation.Landscape); //Save the document to file doc.saveToFile("Result.docx",FileFormat.Docx_2013); } }
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.
Embed an Excel File in a PowerPoint Document in Java
This article demonstrates how to insert an Excel file as an OEL object into a PowerPoint document using Spire.Presentation for Java.
import com.spire.presentation.FileFormat; import com.spire.presentation.IOleObject; import com.spire.presentation.Presentation; import com.spire.presentation.SlideSizeType; import com.spire.presentation.drawing.IImageData; import javax.imageio.ImageIO; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; public class EmbedExcel { public static void main(String[] args) throws Exception { //Create a Presentation object Presentation ppt = new Presentation(); ppt.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9); //Load an image file and add it to the image collection of the presentation File file = new File("C:\\Users\\Administrator\\Desktop\\image.png"); BufferedImage image = ImageIO.read(file); IImageData oleImage = ppt.getImages().append(image); //Load an Excel file and convert it to byte[] object String excelPath = "C:\\Users\\Administrator\\Desktop\\data.xlsx"; File excelFile = new File(excelPath); FileInputStream inputStream = new FileInputStream(excelFile); byte[] data = new byte[(int) excelFile.length()]; inputStream.read(data, 0, data.length); //Create a Rectangle2D object Rectangle2D rect = new Rectangle2D.Float(60, 60, image.getWidth(), image.getHeight()); //Insert the Excel file as an OLE object to the first slide IOleObject oleObject = ppt.getSlides().get(0).getShapes().appendOleObject("excel", data, rect); oleObject.getSubstituteImagePictureFillFormat().getPicture().setEmbedImage(oleImage); oleObject.setProgId("Excel.Sheet.12"); //Save to another file ppt.saveToFile("InsertOle.pptx", FileFormat.PPTX_2013); } }
Remove Blank Lines in Word Document in Java
This article demonstrates how to remove blank lines/empty paragraphs in a Word document by using Spire.Doc for Java.
Below is the sample document which contains many blank lines:
import com.spire.doc.*; import com.spire.doc.documents.*; public class removeBlankLines { public static void main(String[] args) { //Load the sample document Document document = new Document(); document.loadFromFile("sample.docx"); //Traverse every section in the word document and remove the null and empty paragraphs for (Object sectionObj : document.getSections()) { Section section=(Section)sectionObj; for (int i = 0; i < section.getBody().getChildObjects().getCount(); i++) { if ((section.getBody().getChildObjects().get(i).getDocumentObjectType().equals(DocumentObjectType.Paragraph) )) { String s= ((Paragraph)(section.getBody().getChildObjects().get(i))).getText().trim(); if (s.isEmpty()) { section.getBody().getChildObjects().remove(section.getBody().getChildObjects().get(i)); i--; } } } } //Save the document to file String result = "removeBlankLines.docx"; document.saveToFile(result, FileFormat.Docx_2013); } }