Create Chart without Using Worksheet Data Range in C#
This article demonstrates how to create a chart without reference to the worksheet data range using Spire.XLS.
Detail steps:
Step 1: Create a workbook and get the first worksheet.
Workbook wb = new Workbook(); Worksheet sheet = wb.Worksheets[0];
Step 2: Add a chart to the worksheet.
Chart chart = sheet.Charts.Add();
Step 3: Add a series to the chart.
var series = chart.Series.Add();
Step 4: Add data.
series.EnteredDirectlyValues = new object[] { 10, 20, 30 };
Step 5: Save the file.
wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);
Output:
Full code:
using Spire.Xls; namespace Create_chart { class Program { static void Main(string[] args) { //Create a workbook Workbook wb = new Workbook(); //Get the first worksheet Worksheet sheet = wb.Worksheets[0]; //Add a chart to the worksheet Chart chart = sheet.Charts.Add(); //Add a series to the chart var series = chart.Series.Add(); //Add data series.EnteredDirectlyValues = new object[] { 10, 20, 30 }; //Save the file wb.SaveToFile("result.xlsx", ExcelVersion.Version2013); } } }
Insert an Image to a PowerPoint Table in Java
This article demonstrates how to insert an image to a table cell in PowerPoint using Spire.Presentataion for Java.
import com.spire.presentation.FileFormat; import com.spire.presentation.ITable; import com.spire.presentation.Presentation; import com.spire.presentation.drawing.FillFormatType; import com.spire.presentation.drawing.IImageData; import com.spire.presentation.drawing.PictureFillType; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.FileInputStream; public class InsertImageToTableCell { public static void main(String[] args) throws Exception { //create a Presentation object and load an example PowerPoint file Presentation presentation = new Presentation(); presentation.loadFromFile("C:/Users/Administrator/Desktop/example.pptx"); //append a table to the first slide Double[] widths = new Double[]{100d,100d}; Double[] heights = new Double[]{100d,100d}; ITable table = presentation.getSlides().get(0).getShapes().appendTable(100,100, widths, heights); //insert an image to the cell(0,0) table.get(0,0).getFillFormat().setFillType(FillFormatType.PICTURE); table.get(0,0).getFillFormat().getPictureFill().setFillType(PictureFillType.STRETCH); BufferedImage bufferedImage = ImageIO.read(new FileInputStream("C:/Users/Administrator/Desktop/logo.png")); IImageData imageData = presentation.getImages().append(bufferedImage); table.get(0,0).getFillFormat().getPictureFill().getPicture().setEmbedImage(imageData); //save to file presentation.saveToFile("InsertImageToCell.pptx", FileFormat.PPTX_2013); } }
Set PDF Viewer Preference in Java
PDF viewer preference allows users to view PDF with specified view mode or display layout. This article demonstrates how to set the viewer preference in a PDF file using Spire.PDF for Java.
import com.spire.pdf.*; public class ViewerPreference { public static void main(String[] args) { //Load the PDF file PdfDocument pdf = new PdfDocument(); pdf.loadFromFile("Additional.pdf"); //Set viewer preference pdf.getViewerPreferences().setCenterWindow(true); pdf.getViewerPreferences().setDisplayTitle(false); pdf.getViewerPreferences().setFitWindow(true); pdf.getViewerPreferences().setHideMenubar(true); pdf.getViewerPreferences().setHideToolbar(true); pdf.getViewerPreferences().setPageLayout(PdfPageLayout.Single_Page); //pdf.getViewerPreferences().setPageMode(PdfPageMode.Full_Screen); //pdf.getViewerPreferences().setPrintScaling(PrintScalingMode.App_Default); //Save the file. pdf.saveToFile("ViewerPreference.pdf"); //Close pdf.close(); } }
Output:
Insert Video in PowerPoint in Java
This article demonstrates how to insert a video file (.mp4) in a presentation slide by using Spire.Presentation for Java.
import com.spire.presentation.*; import com.spire.presentation.drawing.FillFormatType; import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; public class InsertVideo { public static void main(String[] args) throws Exception { //create a Presentation object and load an example PowerPoint file Presentation presentation = new Presentation(); presentation.loadFromFile("C:/Users/Administrator/Desktop/example.pptx"); //add a shape to the first slide Rectangle2D.Double labelRect = new Rectangle2D.Double(50, 120, 100, 50); IAutoShape labelShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, labelRect); labelShape.getLine().setFillType(FillFormatType.NONE); labelShape.getFill().setFillType(FillFormatType.NONE); labelShape.getTextFrame().setText("Video:"); labelShape.getTextFrame().getTextRange().setFontHeight(28); labelShape.getTextFrame().getTextRange().setLatinFont(new TextFont("Myriad Pro Light")); labelShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID); labelShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK); //append a video file to the slide and set the cover image Rectangle2D.Double videoRect = new Rectangle2D.Double(150, 120, 400, 225); IVideo video = presentation.getSlides().get(0).getShapes().appendVideoMedia((new java.io.File("C:/Users/Administrator/Desktop/video.mp4")).getAbsolutePath(), videoRect); BufferedImage coverImage = ImageIO.read( new File("C:/Users/Administrator/Desktop/coverImage.jpg")); video.getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(coverImage)); //save to file presentation.saveToFile("output/InsertVideo.pptx", FileFormat.PPTX_2010); presentation.dispose(); } }
C# Copy shapes between slides in PowerPoint document
In this article, we will explain how to copy a shapes or all shapes from one slide into another within the same PowerPoint document by using Spire.Presentation.
Firstly, view the sample PowerPoint document:
Copy a single shape from the first slide to the second slide:
//Load the sample document Presentation ppt = new Presentation(); ppt.LoadFromFile("Sample.pptx"); //define the source slide and target slide ISlide sourceSlide = ppt.Slides[0]; ISlide targetSlide = ppt.Slides[1]; //copy the second shape from the source slide to the target slide targetSlide.Shapes.AddShape((Shape)sourceSlide.Shapes[1]); //save the document to file ppt.SaveToFile("Copyshape.pptx", FileFormat.Pptx2013);
Effective screenshot after copy a single shape from the first slide to second slide:
Copy all shapes from the first slide to the second slide:
//Load the sample document Presentation ppt = new Presentation(); ppt.LoadFromFile("Sample.pptx"); //copy all the shapes from the source slide to the target slide for (int i = 0; i < ppt.Slides.Count - 1; i++) { ISlide sourceSlide = ppt.Slides[i]; ISlide targetSlide = ppt.Slides[ppt.Slides.Count - 1]; for (int j = 0; j < sourceSlide.Shapes.Count; j++) { targetSlide.Shapes.AddShape((Shape)sourceSlide.Shapes[j]); } } //save the document to file ppt.SaveToFile("Copyshapes.pptx", FileFormat.Pptx2013);
Effective screenshot after copy all shapes from the first slide to second slide:
Insert Audio in PowerPoint in Java
This article demonstrates how to insert an audio file (.wav) in a presentation slide by using Spire.Presentation for Java.
import com.spire.presentation.*; import com.spire.presentation.drawing.FillFormatType; import java.awt.*; import java.awt.geom.Rectangle2D; public class InsertAudio { public static void main(String[] args) throws Exception { //create a Presentation object and load an example PowerPoint file Presentation presentation = new Presentation(); presentation.loadFromFile("C:/Users/Administrator/Desktop/example.pptx"); //add a shape to the first slide Rectangle2D.Double labelRect= new Rectangle2D.Double(50, 120, 100, 50); IAutoShape labelShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, labelRect); labelShape.getLine().setFillType(FillFormatType.NONE); labelShape.getFill().setFillType(FillFormatType.NONE); labelShape.getTextFrame().setText("Audio:"); labelShape.getTextFrame().getTextRange().setFontHeight(28); labelShape.getTextFrame().getTextRange().setLatinFont(new TextFont("Myriad Pro Light")); labelShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID); labelShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK); //append an audio file to the slide Rectangle2D.Double audioRect = new Rectangle2D.Double(170, 120, 50, 50); presentation.getSlides().get(0).getShapes().appendAudioMedia((new java.io.File("C:/Users/Administrator/Desktop/Music.wav")).getAbsolutePath(), audioRect); //save to file presentation.saveToFile("output/InsertAudio.pptx", FileFormat.PPTX_2010); presentation.dispose(); } }
How to print PDF document in Java
This following code snippets demonstrate how to use Spire.PDF for Java to print a PDF file in Java programs in the following three aspects:
- Silent print PDF document with default printer
- Print PDF document with Print dialog
- Print PDF document with customized page size
Print the PDF document to default printer without showing print dialog, we could also customize some print settings, such as removing the default print margins, setting the number of copies, etc.
import com.spire.pdf.*; import java.awt.print.*; public class Print { public static void main(String[] args) { //load the sample document PdfDocument pdf = new PdfDocument(); pdf.loadFromFile("Sample.pdf"); PrinterJob loPrinterJob = PrinterJob.getPrinterJob(); PageFormat loPageFormat = loPrinterJob.defaultPage(); Paper loPaper = loPageFormat.getPaper(); //remove the default printing margins loPaper.setImageableArea(0,0,loPageFormat.getWidth(),loPageFormat.getHeight()); //set the number of copies loPrinterJob.setCopies(2); loPageFormat.setPaper(loPaper); loPrinterJob.setPrintable(pdf,loPageFormat); try { loPrinterJob.print(); } catch (PrinterException e) { e.printStackTrace(); } } }
Print PDF with print dialog
import com.spire.pdf.*; import java.awt.print.*; public class Print { public static void main(String[] args) { //load the sample document PdfDocument pdf = new PdfDocument(); pdf.loadFromFile("Sample.pdf"); PrinterJob loPrinterJob = PrinterJob.getPrinterJob(); PageFormat loPageFormat = loPrinterJob.defaultPage(); Paper loPaper = loPageFormat.getPaper(); //remove the default printing margins loPaper.setImageableArea(0,0,loPageFormat.getWidth(),loPageFormat.getHeight()); loPageFormat.setPaper(loPaper); loPrinterJob.setPrintable(pdf,loPageFormat); //display the print dialog if (loPrinterJob.printDialog()) { try { loPrinterJob.print(); } catch (PrinterException e) { e.printStackTrace(); } } } }
Print PDF document with customized page size
import com.spire.pdf.*; import java.awt.print.*; public class Print { public static void main(String[] args) { //load the sample document PdfDocument pdf = new PdfDocument(); pdf.loadFromFile("Sample.pdf"); PrinterJob loPrinterJob = PrinterJob.getPrinterJob(); PageFormat loPageFormat = loPrinterJob.defaultPage(); //set the print page size Paper loPaper = loPageFormat.getPaper(); loPaper.setSize(500,600); loPageFormat.setPaper(loPaper); loPrinterJob.setPrintable(pdf,loPageFormat); try { loPrinterJob.print(); } catch (PrinterException e) { e.printStackTrace(); } } }
Horizontally and Vertically Split a PDF Page into multiple Pages in C#
Spire.PDF supports to horizontally and vertically split a PDF page into two or more pages. This article will show you how to use Spire.PDF to accomplish this function.
The sample PDF file:
Detail steps:
Step 1: Load the sample PDF file and get the first page.
PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("New Zealand.pdf"); PdfPageBase page = pdf.Pages[0];
Step 2: Create a new PDF file and remove page margins.
PdfDocument newPdf = new PdfDocument(); newPdf.PageSettings.Margins.All = 0;
Step 3: Set page width and height in order to horizontally or vertically split the first page into 2 pages.
//Horizontally Split newPdf.PageSettings.Width = page.Size.Width; newPdf.PageSettings.Height = page.Size.Height / 2; //Vertically split //newPdf.PageSettings.Width = page.Size.Width / 2; //newPdf.PageSettings.Height = page.Size.Height;
Step 5: Add a new page to the new PDF file.
PdfPageBase newPage = newPdf.Pages.Add();
Step 6: Create layout format.
PdfTextLayout format = new PdfTextLayout(); format.Break = PdfLayoutBreakType.FitPage; format.Layout = PdfLayoutType.Paginate;
Step 7: Create template from the first Page of the sample PDF, and draw the template to the new added page with the layout format.
page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);
Step 8: Save and close.
newPdf.SaveToFile("SplitPage.pdf"); newPdf.Close(); pdf.Close();
Horizontally split:
Vertically split:
Full code:
using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics; namespace SplitPDFPage { class Program { static void Main(string[] args) { //Load the sample PDF PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("New Zealand.pdf"); //Get the first page PdfPageBase page = pdf.Pages[0]; //Create a new PDF PdfDocument newPdf = new PdfDocument(); //Remove page margins newPdf.PageSettings.Margins.All = 0; //Set page width and height in order to horizontally split the first page into 2 pages newPdf.PageSettings.Width = page.Size.Width; newPdf.PageSettings.Height = page.Size.Height / 2; //Set page width and height in order to vertically split the first page into 2 pages //newPdf.PageSettings.Width = page.Size.Width / 2; //newPdf.PageSettings.Height = page.Size.Height; //Add a new page to the new PDF PdfPageBase newPage = newPdf.Pages.Add(); //Create layout format PdfTextLayout format = new PdfTextLayout(); format.Break = PdfLayoutBreakType.FitPage; format.Layout = PdfLayoutType.Paginate; //Create template from the first Page of the sample PDF, and draw the template to the new added page with the layout format page.CreateTemplate().Draw(newPage, new PointF(0, 0), format); //Save and close newPdf.SaveToFile("SplitPage.pdf"); newPdf.Close(); pdf.Close(); } } }
Merge and Split Table Cells in PowerPoint in Java
This article demonstrates how to access a table in an existing PowerPoint document and how to merge and split cells in the table by using Spire.Presentation for Java.
Here is the screenshot of the input file.
import com.spire.presentation.FileFormat; import com.spire.presentation.ITable; import com.spire.presentation.Presentation; public class MergeAndSplitCells { public static void main(String[] args) throws Exception { //create a Presentation object Presentation presentation = new Presentation(); //load a sample PowerPoint file presentation.loadFromFile("C:/Users/Administrator/Desktop/sample.pptx"); //declare ITable variable ITable table = null; //get the table from the presentation slide for (Object shape : presentation.getSlides().get(0).getShapes()) { if (shape instanceof ITable) { table = (ITable) shape; //merge the cells between cell[0,0] and cell[2,0] table.mergeCells(table.get(0, 0), table.get(2, 0), false); //merge the cells between cell[0,1] and cell[0,3] table.mergeCells(table.get(0, 1), table.get(0, 3), false); //split cell[2,3] to 2 columns table.get(2,3).Split(1,2); } } //save to file presentation.saveToFile("MergeAndSplitCells.pptx", FileFormat.PPTX_2010); } }
Output
How to install Spire Series Products for Java from Maven Repository
Developers can easily use Spire Series Products for Java directly in their Maven Projects with simple configurations. E-iceblue hosts all Java APIs on Maven repository. Here we use Spire.PDF for Java as example to show you how to istall it from Maven.
Firstly please specify e-iceblue Maven Repository configuration / location in your Maven pom.xml as below:
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories>
Then define Spire.PDF for Java API dependency in your pom.xml as follows:
<dependencies> <dependency> <groupId> e-iceblue </groupId> <artifactId>spire.pdf</artifactId> <version>2.2.0</version> </dependency> </dependencies>
For IDEA, you only need to click “Import Changes” to import the Spire.PDF jars.
For Eclipse, you only need to click the “Save” button, then Spire.PDF jars will be downloaded automatically.
Now you have successfully defined the Spire.PDF for Java Maven dependency in your Maven project.
The correct name for Free Java products, use Free.Spire.PDF for example:
<dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf.free</artifactId> <version>2.0.0</version> </dependency> </dependencies>
Step details for IDEA
Create a new Maven Project: File - New - Project
Set the GroupId for the project:
Update the porm.xml and then Import Changes:
Step details for Eclipse
Create a new Maven Project:
Define the location and set the Group ID:
Update the porm.xml and then click Save button: