此演示向您展示如何在 PowerPoint 文档中搜索特定文本并替换匹配的文本。
Upload
Click here to browse files.Convert to
如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo。
如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。。
package ppt;
import com.spire.presentation.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
public class FindAndReplaceDemo {
public void findOrReplace(String filePath,String changeType, String text, String newText, String resultFilePath) throws Exception {
Presentation presentation = new Presentation();
presentation.loadFromFile(filePath);
switch (changeType){
case "FIND":
findText(presentation,text);
break;
case "REPLACE":
replaceText(presentation,text,newText);
break;
}
presentation.saveToFile(resultFilePath,FileFormat.PPTX_2013);
}
private void findText(Presentation presentation, String text){
for(int i = 0; i < presentation.getSlides().getCount();i++){
ISlide slide = presentation.getSlides().get(i);
for (int j = 0; j < slide.getShapes().getCount();j++){
IAutoShape shape = (IAutoShape)slide.getShapes().get(j);
TextHighLightingOptions options = new TextHighLightingOptions();
options.setWholeWordsOnly(true);
options.setCaseSensitive(true);
shape.getTextFrame().highLightText(text, Color.yellow, options);
}
}
}
private void replaceText(Presentation presentation, String oldText, String newText){
for(int i = 0; i < presentation.getSlides().getCount();i++){
ISlide slide = presentation.getSlides().get(i);
Map map = new HashMap<>();
map.put(oldText,newText);
replaceTags(slide,map);
}
}
private static void replaceTags(ISlide pSlide, Map tagValues) {
for (int i = 0; i < pSlide.getShapes().getCount(); i++) {
IShape curShape = pSlide.getShapes().get(i);
if (curShape instanceof IAutoShape) {
for (int j = 0; j < ((IAutoShape) curShape).getTextFrame().getParagraphs().getCount(); j++) {
ParagraphEx tp = ((IAutoShape) curShape).getTextFrame().getParagraphs().get(j);
for (Map.Entry entry : tagValues.entrySet()) {
String mapKey = entry.getKey();
String mapValue = entry.getValue();
if (tp.getText().contains(mapKey)) {
tp.setText(tp.getText().replace(mapKey, mapValue));
}
}
}
}
}
}
}
using Spire.Presentation;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoOnlineCode
{
class HighlightOrReplace
{
public void HighlightOrReplaceDemo(string filePath, string format, string text, string newText, string resultFileName)
{
Presentation presentation = new Presentation();
presentation.LoadFromFile(filePath);
switch (format)
{
case "HIGHLIGHT":
HighlightText(presentation,text,resultFileName);
break;
case "REPLACE":
ReplaceText(presentation, text, newText, resultFileName);
break;
}
}
private static void HighlightText(Presentation presentation, string text, string resultFileName)
{
foreach (ISlide slide in presentation.Slides)
{
foreach (IShape shape in slide.Shapes)
{
if (shape is IAutoShape)
{
IAutoShape autoShape = shape as IAutoShape;
TextHighLightingOptions options = new TextHighLightingOptions();
options.WholeWordsOnly = true;
options.CaseSensitive = true;
autoShape.TextFrame.HighLightText(text, Color.Yellow, options);
}
}
}
presentation.SaveToFile(resultFileName+".pptx", FileFormat.Pptx2013);
}
private static void ReplaceText(Presentation presentation, string oldText, string newText, string resultFileName)
{
Dictionary tagValues = new Dictionary();
tagValues.Add(oldText,newText);
foreach (ISlide slide in presentation.Slides)
{
ReplaceTags(slide, tagValues);
}
presentation.SaveToFile(resultFileName + ".pptx", FileFormat.Pptx2013);
}
private static void ReplaceTags(ISlide pSlide, Dictionary TagValues)
{
foreach (IShape curShape in pSlide.Shapes)
{
if (curShape is IAutoShape)
{
foreach (TextParagraph tp in (curShape as IAutoShape).TextFrame.Paragraphs)
{
foreach (var curKey in TagValues.Keys)
{
if (tp.Text.Contains(curKey))
{
tp.Text = tp.Text.Replace(curKey, TagValues[curKey]);
}
}
}
}
}
}
}
}
此演示向您展示如何将 PowerPoint 文档 ( ppt/pptx ) 转换为 PDF、HTML、图像、XPS 和 Tiff。
Upload
Click here to browse files.Convert to
如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo。
如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。。
package ppt;
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class ConvertDemo {
public void convertDemo(String filePath, String convertTo, String resultFileName) throws Exception {
Presentation presentation = new Presentation();
presentation.loadFromFile(filePath);
ConvertFormat(presentation, convertTo,resultFileName);
}
private void ConvertFormat(Presentation presentation, String convertTo, String resultFileName) throws Exception {
switch (convertTo){
case "PDF":
presentation.saveToFile(resultFileName+".pdf", FileFormat.PDF);
break;
case "HTML":
presentation.saveToFile(resultFileName+".html",FileFormat.HTML);
break;
case "IMAGE":
BufferedImage[] images = new BufferedImage[presentation.getSlides().getCount()];
for (int i = 0; i< presentation.getSlides().getCount();i++){
images[i] = presentation.getSlides().get(i).saveAsImage();
}
if (images != null && images.length > 0){
if (images.length == 1){
ImageIO.write(images[0],".PNG", new File(resultFileName+".png"));
}
}else {
for (int j = 0; j < images.length;j++){
String fileName = String.format("image-{0}.png",j);
ImageIO.write(images[j],".PNG",new File(fileName));
}
}
break;
case "XPS":
presentation.saveToFile(resultFileName+".xps",FileFormat.XPS);
break;
case "TIFF":
presentation.saveToFile(resultFileName+".tiff",FileFormat.TIFF);
break;
}
}
}
using Spire.Presentation;
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace DemoOnlineCode
{
class Convertors
{
public void demoConvert(string filePath, string format, string resultFileName)
{
Presentation presentation = new Presentation();
presentation.LoadFromFile(filePath);
ConvertFormat(presentation,format,resultFileName);
}
private static void ConvertFormat(Presentation presentation, string format, string resultFileName)
{
switch (format)
{
case "PDF":
presentation.SaveToFile(resultFileName + ".PDF", FileFormat.PDF);
break;
case "HTML":
presentation.SaveToFile(resultFileName + ".html", FileFormat.Html);
break;
case "IMAGE":
Image[] images = new Image[presentation.Slides.Count];
for (int i = 0; i < presentation.Slides.Count;i++)
{
images[i] = presentation.Slides[i].SaveAsImage();
}
if (images != null && images.Length > 0)
{
if (images.Length == 1)
{
images[0].Save(resultFileName + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
else
{
for (int i = 0; i < images.Length; i++)
{
string fileName = string.Format("{0}-Image-{1}.png", resultFileName, i);
images[i].Save(fileName, ImageFormat.Png);
}
}
}
break;
case "XPS":
presentation.SaveToFile(resultFileName + ".xps", FileFormat.XPS);
break;
case "TIFF":
Image[] images2 = new Image[presentation.Slides.Count];
if (images2 != null && images2.Length > 0)
{
for (int i = 0; i < presentation.Slides.Count; i++)
{
images2[i] = presentation.Slides[i].SaveAsImage();
}
JoinTiffImages(images2,resultFileName+ ".tiff", EncoderValue.CompressionLZW);
}
break;
}
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int j = 0; j < encoders.Length; j++)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
}
public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
{
//Use the save encoder
System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)compressEncoder);
Image pages = null;
int frame = 0;
ImageCodecInfo info = GetEncoderInfo("image/tiff");
foreach (Image img in images)
{
if (frame == 0)
{
pages = img;
//Save the first frame
pages.Save(outFile, info, ep);
}
else
{
//Save the intermediate frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
pages.SaveAdd(img, ep);
}
if (frame == images.Length - 1)
{
//Flush and close
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
}
}
}
此演示向您展示如何将文本水印或图像水印添加到 PDF 文档。
Upload
Click here to browse files.Set text watermark
| Text: | |
| Font: | |
| Font Size: | |
| Color: | |
|
downloads
|
|
Set image watermark
| Image: |
Click here to browse files
|
![]() |
|
|
downloads
|
|
如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo。
如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。。
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.ByteArrayInputStream;
public class PdfWatermarkDemo {
public void addWatermark(String pdfFile, String watermarkType, String text, byte[] imageData){
String outputFile = "output.pdf";
PdfDocument pdf=new PdfDocument();
pdf.loadFromFile(pdfFile);
switch (watermarkType) {
case "Text":
addTextWatermark(pdf, text);
break;
case "Image":
addImageWatermark(pdf, imageData);
break;
}
pdf.saveToFile(outputFile, FileFormat.PDF);
pdf.close();
}
public void addImageWatermark(PdfDocument pdf, byte[] imgData) {
PdfImage image = PdfImage.fromStream(new ByteArrayInputStream(imgData));
for (int i = 0; i < pdf.getPages().getCount(); i++) {
PdfPageBase page = pdf.getPages().get(i);
page.getCanvas().save();
page.getCanvas().setTransparency(0.5f, 0.5f, PdfBlendMode.Multiply);
page.getCanvas().drawImage(image, new Point2D.Float(160, 260));
page.getCanvas().restore();
}
}
public void addTextWatermark(PdfDocument pdf, String text) {
for (int i = 0; i < pdf.getPages().getCount(); i++) {
PdfPageBase page = pdf.getPages().get(i);
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(page.getCanvas().getClientSize().getWidth() / 2, page.getCanvas().getClientSize().getHeight() / 3);
PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
brush.getGraphics().setTransparency(0.3F);
brush.getGraphics().save();
brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 2, (float) brush.getSize().getHeight() / 2);
brush.getGraphics().rotateTransform(-45);
brush.getGraphics().drawString(text, new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 80), true), PdfBrushes.getViolet(), 0, 0, new PdfStringFormat(PdfTextAlignment.Center));
brush.getGraphics().restore();
brush.getGraphics().setTransparency(0.5);
Rectangle2D loRect = new Rectangle2D.Float();
loRect.setFrame(new Point2D.Float(0, 0), page.getCanvas().getClientSize());
page.getCanvas().drawRectangle(brush, loRect);
}
}
}
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System.IO;
namespace DemoOnlineCode
{
class WatermarkDemo
{
public void addWatermark(string pdfFile, string watermarkType, string text, byte[] imageData)
{
string outputFile = "output.pdf";
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(pdfFile);
switch (watermarkType)
{
case "Text":
addTextWatermark(pdf, text);
break;
case "Image":
addImageWatermark(pdf, imageData);
break;
}
pdf.SaveToFile(outputFile, FileFormat.PDF);
pdf.Close();
}
public void addImageWatermark(PdfDocument pdf, byte[] imgData)
{
PdfImage image = PdfImage.FromStream(new MemoryStream(imgData));
for (int i = 0; i < pdf.Pages.Count; i++)
{
PdfPageBase page = pdf.Pages[i];
page.Canvas.Save();
page.Canvas.SetTransparency(0.5f, 0.5f, PdfBlendMode.Multiply);
page.Canvas.DrawImage(image, new System.Drawing.PointF(160,260));
page.Canvas.Restore();
}
}
public void addTextWatermark(PdfDocument pdf, string text)
{
for (int i = 0; i < pdf.Pages.Count; i++)
{
PdfPageBase page = pdf.Pages[i];
PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3));
brush.Graphics.SetTransparency(0.3F);
brush.Graphics.Save();
brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2);
brush.Graphics.RotateTransform(-45);
brush.Graphics.DrawString(text, new PdfTrueTypeFont(new Font("Arial",80,FontStyle.Bold), true), PdfBrushes.Violet, 0, 0, new PdfStringFormat(PdfTextAlignment.Center));
brush.Graphics.Restore();
brush.Graphics.SetTransparency(0.5f);
page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));
}
}
}
}
此演示向您展示如何将多个 PDF 文件合并为一个 PDF 文档。
Upload
Source file:
如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo。
如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。。
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfDocumentBase;
public class MergePdfsDemo {
public void mergePdfs(String []pdfFiles){
String outputFile="output.pdf";
PdfDocumentBase documentBase= PdfDocument.mergeFiles(pdfFiles);
documentBase.save(outputFile);
documentBase.close();
}
}
using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoOnlineCode
{
class MergePdfsDemo
{
public void mergePdfs(string[] pdfFiles)
{
String outputFile = "output.pdf";
PdfDocumentBase documentBase = PdfDocument.MergeFiles(pdfFiles);
documentBase.Save(outputFile);
documentBase.Close();
}
}
}
此演示向您展示如何在 PDF 文档中创建包含指定数据的表格。
Data
Option
如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo。
如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。。
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.grid.PdfGrid;
import com.spire.pdf.grid.PdfGridCell;
import com.spire.pdf.grid.PdfGridRow;
import java.awt.*;
import java.util.List;
public class PdfTableDemo {
public void createTable(List< CustomerModel > data,Color borderColor){
String outputFile = "output.pdf";
String []haeder={"Contact","Company","City","Country","Phone"};
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.getPages().add(PdfPageSize.A4, new PdfMargins(40, 30, 40, 30));
PdfBorders borders = new PdfBorders();
if (borderColor == null) {
borders.setAll(new PdfPen(new PdfRGBColor(Color.TRANSLUCENT)));
} else {
borders.setAll(new PdfPen(new PdfRGBColor(borderColor)));
}
PdfGrid grid = null;
PdfGridRow pdfGridRow;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 16), true);
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 10), true);
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
grid = new PdfGrid();
grid.setAllowCrossPages(true);
grid.getStyle().setCellSpacing(3f);
grid.getColumns().add(5);
double width = page.getCanvas().getClientSize().getWidth() - (grid.getColumns().getCount() + 1);
grid.getColumns().get(0).setWidth(width * 0.2);
grid.getColumns().get(1).setWidth(width * 0.2);
grid.getColumns().get(2).setWidth(width * 0.2);
grid.getColumns().get(3).setWidth(width * 0.2);
grid.getColumns().get(4).setWidth(width * 0.2);
pdfGridRow = grid.getHeaders().add(1)[0];
pdfGridRow.setHeight(50);
int i = 0;
//header row
for (i = 0; i < haeder.length; i++) {
PdfGridCell cell = pdfGridRow.getCells().get(i);
cell.setColumnSpan(1);
cell.setValue(haeder[i]);
cell.setStringFormat(format1);
cell.getStyle().setBorders(borders);
cell.getStyle().setFont(font1);
}
//data row
for (i = 0; i < data.size(); i++) {
pdfGridRow = grid.getRows().add();
pdfGridRow.setHeight(40f);
CustomerModel model=data.get(i);
String []values={model.getContact(),model.getCompany(),model.getCity(),model.getCountry(),model.getPhone()};
for (int j = 0; j < pdfGridRow.getCells().getCount(); j++) {
PdfGridCell cell = pdfGridRow.getCells().get(j);
cell.setColumnSpan(1);
cell.setValue(values[j]);
cell.getStyle().setBorders(borders);
cell.getStyle().setFont(font2);
cell.setStringFormat(format1);
}
}
grid.setRepeatHeader(true);
grid.draw(page, new Point(0, 0));
pdf.saveToFile(outputFile, FileFormat.PDF);
pdf.close();
}
}
class CustomerModel {
private String contact;
private String company;
private String city;
private String country;
private String phone;
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System;
using System.Collections.Generic;
using System.Drawing;
namespace DemoOnlineCode
{
class CreateTableDemo
{
public void createTable(List< CustomerModel > data, Color borderColor)
{
String outputFile = "output.pdf";
String[] haeder = { "Contact", "Company", "City", "Country", "Phone" };
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4, new PdfMargins(40, 30, 40, 30));
PdfBorders borders = new PdfBorders();
if (borderColor == null)
{
borders.All=new PdfPen(new PdfRGBColor(Color.Transparent));
}
else
{
borders.All=new PdfPen(new PdfRGBColor(borderColor));
}
PdfGrid grid = null;
PdfGridRow pdfGridRow;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial",16f, FontStyle.Bold), true);
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 10f,FontStyle.Regular), true);
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
grid = new PdfGrid();
grid.AllowCrossPages=true;
grid.Style.CellSpacing=3f;
grid.Columns.Add(5);
float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
grid.Columns[0].Width=width * 0.2f;
grid.Columns[1].Width=width * 0.2f;
grid.Columns[2].Width=width * 0.2f;
grid.Columns[3].Width=width * 0.2f;
grid.Columns[4].Width=width * 0.2f;
pdfGridRow = grid.Headers.Add(1)[0];
pdfGridRow.Height=50;
int i = 0;
//header row
for (i = 0; i < haeder.Length; i++)
{
PdfGridCell cell = pdfGridRow.Cells[i];
cell.ColumnSpan=1;
cell.Value = haeder[i];
cell.StringFormat = format1;
cell.Style.Borders = borders;
cell.Style.Font = font1;
}
//data row
for (i = 0; i < data.Count; i++)
{
pdfGridRow = grid.Rows.Add();
pdfGridRow.Height = 40f;
CustomerModel model = data[i];
String[] values = { model.Contact, model.Company,model.City, model.Country, model.Phone };
for (int j = 0; j < pdfGridRow.Cells.Count; j++)
{
PdfGridCell cell = pdfGridRow.Cells[j];
cell.ColumnSpan=1;
cell.Value = values[j];
cell.Style.Borders = borders;
cell.Style.Font = font2;
cell.StringFormat = format1;
}
}
grid.RepeatHeader = true;
grid.Draw(page, new Point(0, 0));
pdf.SaveToFile(outputFile, FileFormat.PDF);
pdf.Close();
}
}
}
class CustomerModel
{
public string Contact;
public string Company;
public string City;
public string Country;
public string Phone;
}
此演示向您展示如何在 PDF 文档中搜索文本并突出显示匹配的文本。
Upload
Click here to browse files.Convert to
如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo。
如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。。
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.general.find.PdfTextFind;
import com.spire.pdf.general.find.TextFindParameter;
import java.awt.*;
import java.util.EnumSet;
public class PdfFindAndHighlightDemo {
public void findAndHighlight(String pdfFiles, String keyText, Color highlightColor){
String outputFile = "output.pdf";
PdfDocument pdf=new PdfDocument();
pdf.loadFromFile(pdfFiles);
PdfTextFind[] result = null;
for (Object pageObj : pdf.getPages()) {
PdfPageBase page = (PdfPageBase) pageObj;
// Find text
result = page.findText(keyText, EnumSet.of(TextFindParameter.None)).getFinds();
if (result != null) {
for (PdfTextFind find : result) {
find.applyHighLight(highlightColor);
}
}
}
pdf.saveToFile(outputFile, FileFormat.PDF);
pdf.close();
}
}
using Spire.Pdf;
using Spire.Pdf.General.Find;
using System.Drawing;
namespace DemoOnlineCode
{
class FindAndHighlightDemo
{
public void findAndHighlight(string pdfFiles, string keyText, Color highlightColor)
{
string outputFile = "output.pdf";
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(pdfFiles);
PdfTextFind[] result = null;
foreach (PdfPageBase page in pdf.Pages)
{
// Find text
result = page.FindText(keyText, TextFindParameter.None).Finds;
if (result != null)
{
foreach (PdfTextFind find in result)
{
find.ApplyHighLight(highlightColor);
}
}
}
pdf.SaveToFile(outputFile, FileFormat.PDF);
pdf.Close();
}
}
}
制作 PDF 文档时,如果我们只使用黑色字体,有时会让文章变得单调乏味。我们可以为不同的文字设置不同的字体颜色,使制作的文档更美观、更生动,增强文档对读者的吸引力。本文将介绍如何使用 Spire.PDF for Java 通过代码设置 PDF 文档中文字的字体颜色。
首先,您需要在 Java 程序中添加 Spire.Pdf.jar 文件作为依赖项。JAR 文件可以从此链接下载。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而在应用程序中导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.11.11</version>
</dependency>
</dependencies>
Spire.PDF for Java 提供了 PdfSolidBrush 类以设置文本的绘制颜色,支持以特定的 RGB 值或 HTML 颜色代码来定义绘制颜色。详细操作步骤如下:
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfRGBColor;
import com.spire.pdf.graphics.PdfSolidBrush;
import com.spire.pdf.graphics.PdfTrueTypeFont;
import java.awt.*;
public class setFontColor {
public static void main(String[] args) {
//创建 PdfDocument 类的对象
PdfDocument doc = new PdfDocument();
//添加一个页面
PdfPageBase page = doc.getPages().add();
//设置位置
float y = 30;
//创建 PdfSolidBrush 类的对象并设置颜色
PdfRGBColor rgb1 = new PdfRGBColor(Color.green);
PdfSolidBrush brush1 = new PdfSolidBrush(rgb1);
//以RGB值设置颜色
PdfRGBColor rgb2 = new PdfRGBColor(0,197,205);
PdfSolidBrush brush2 = new PdfSolidBrush(rgb2);
//以HTML代码设置颜色
Color color = Color.decode("#A52A2A");
PdfSolidBrush brush3 = new PdfSolidBrush(new PdfRGBColor(color));
//创建 PdfTrueTypeFont 类的对象
Font font = new Font("华文中宋", java.awt.Font.BOLD, 14);
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font);
//绘制文本
page.getCanvas().drawString("设置绘制颜色", trueTypeFont, brush1, 0, (y = y + 30f));
page.getCanvas().drawString("以RGB值设置颜色", trueTypeFont, brush2, 0, (y = y + 50f));
page.getCanvas().drawString("以HTML颜色代码设置颜色", trueTypeFont, brush3, 0, (y = y + 60f));
//保存文档
doc.saveToFile("设置字体颜色.pdf");
}
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Excel 中,我们可以通过调整数据列顺序来实现将批量数据进行布局调整,以达到表格设计美观性或者表格数据设计得严谨性等目的。本文,将通过使用 Spire.XLS for Java 来实现如何调整列顺序。以下是具体步骤及方法。
首先,您需要在 Java 程序中添加 Spire.XLS for Java 文件作为依赖项。JAR 文件可以从此链接下载。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而轻松地在应用程序中导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls</artifactId>
<version>15.11.3</version>
</dependency>
</dependencies>
以下是调整列顺序的主要代码步骤:
import com.spire.xls.*;
public class ChangeColumnOrder {
public static void main(String[] args) {
//创建Workbook类的对象
Workbook workbook = new Workbook();
//加载Excel文件
workbook.loadFromFile( "test.xlsx");
//获取第一个工作表
Worksheet worksheet = workbook.getWorksheets().get(0);
//设置新的列顺序
int[] newColumnOrder = new int[]{3, 0, 1, 5, 7, 4,2,8,6};
//添加一个临时工作表
Worksheet newSheet = workbook.getWorksheets().add("temp");
//将数据从第一个工作表复制到临时工作表
newSheet.copyFrom(worksheet);
//循环遍历newColumnOrder数组
for (int i = 0; i < newColumnOrder.length; i++) {
//从临时工作表复制列到第一个工作表
newSheet.getColumns()[newColumnOrder[i]].copy(worksheet.getColumns()[i],true,true);
//将第一个工作表的某一列的宽度设置为临时工作表的宽度
worksheet.getColumns()[i].setColumnWidth(newSheet.getColumns()[newColumnOrder[i]].getColumnWidth());
}
//删除临时表格
workbook.getWorksheets().remove(newSheet);
//将工作簿另存为文件
workbook.saveToFile("ChangeColumnOrder.xlsx", FileFormat.Version2010);
}
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
XPS(XML Paper Specification)是一种固定布局的文档格式,旨在保持文档的保真度并提供与设备无关的文档外观。XPS 类似于 PDF,但 XPS 基于 XML 而不是 PostScript。如果想要将 Word 文档保存为固定布局的文件格式,XPS 是个不错的选择。本文将介绍如何使用 Spire. Doc for .NET 通过代码将 Word 文档转换为 XPS 文件。
首先,您需要将 Spire.Doc for.NET 包含的 DLL 文件作为引用添加到您的 .NET 项目中。DLL 文件可以从此链接下载,也可以通过 NuGet 安装。
PM> Install-Package Spire.Doc使用 Spire.Doc for .NET 将 Word 文档转换为 XPS 文件只需载入 Word 文档后保存为 XPS 格式文件即可,详细操作步骤如下:
using Spire.Doc;
namespace ConvertWordToXps
{
class Program
{
static void Main(string[] args)
{
//创建 Document 类的对象
Document doc = new Document();
//载入Word文档
doc.LoadFromFile(@"C:\示例.docx");
//将文档保存为XPS文件
doc.SaveToFile("Word转XPS.xps", FileFormat.XPS);
}
}
}Imports Spire.Doc
Namespace ConvertWordToXps
Class Program
Shared Sub Main(ByVal args() As String)
'创建 Document 类的对象
Dim doc As Document = New Document()
'载入Word文档
doc.LoadFromFile("https://cdn.e-iceblue.cn/C:\示例.docx")
'将文档保存为XPS文件
doc.SaveToFile("Word转XPS.xps", FileFormat.XPS)
End Sub
End Class
End Namespace
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.PDF for Java 8.8.3 已发布。本次更新新增支持创建无序列表,且调整了签名时间戳的内部安全性。同时该版本还增强了 PDF 到 Tiff 的转换。此外,一些已知问题也得到了修复。详情请阅读以下内容。
新功能:
public void DrawMarker(PdfUnorderedMarkerStyle style, String outputFile) {
PdfDocument doc = new PdfDocument();
PdfNewPage page = (PdfNewPage) doc.getPages().add();
PdfMarker marker = new PdfMarker(style);
String listContent = "Data Structure\n"
+ "Algorithm\n"
+ "Computer Newworks\n"
+ "Operating System\n"
+ "C Programming\n"
+ "Computer Organization and Architecture";
PdfUnorderedList list = new PdfUnorderedList(listContent);
list.setIndent(2);
list.setTextIndent(4);
list.setMarker(marker);
list.draw(page, 100, 100);
doc.saveToFile(outputFile, FileFormat.PDF);
doc.close();
}
public void PdfMarker_CustomImage() throws Exception {
String outputFile = "PdfMarker_CustomImage.pdf";
String inputFile_Img = "sample.png";
PdfDocument doc = new PdfDocument();
PdfNewPage page = (PdfNewPage) doc.getPages().add();
PdfMarker marker = new PdfMarker(PdfUnorderedMarkerStyle.Custom_Image);
marker.setImage(PdfImage.fromFile(inputFile_Img));
String listContent = "Data Structure\n"
+ "Algorithm\n"
+ "Computer Newworks\n"
+ "Operating System\n"
+ "C Programming\n"
+ "Computer Organization and Architecture";
PdfUnorderedList list = new PdfUnorderedList(listContent);
list.setIndent(2);
list.setTextIndent(4);
list.setMarker(marker);
list.draw(page, 100, 100);
doc.saveToFile(outputFile, FileFormat.PDF);
doc.close();
}
public void PdfMarker_CustomTemplate() throws Exception {
String outputFile = "PdfMarker_CustomTemplate.pdf";
String inputFile_Img = "sample.png";
PdfDocument doc = new PdfDocument();
PdfNewPage page = (PdfNewPage) doc.getPages().add();
PdfMarker marker = new PdfMarker(PdfUnorderedMarkerStyle.Custom_Template);
PdfTemplate template = new PdfTemplate(210, 210);
marker.setTemplate(template);
template.getGraphics().drawImage(PdfImage.fromFile(inputFile_Img), 0, 0);
String listContent = "Data Structure\n"
+ "Algorithm\n"
+ "Computer Newworks\n"
+ "Operating System\n"
+ "C Programming\n"
+ "Computer Organization and Architecture";
PdfUnorderedList list = new PdfUnorderedList(listContent);
list.setIndent(2);
list.setTextIndent(4);
list.setMarker(marker);
list.draw(page, 100, 100);
doc.saveToFile(outputFile, FileFormat.PDF);
doc.close();
}
public void PdfMarker_CustomString() throws Exception {
String outputFile = "PdfMarker_CustomString.pdf";
PdfDocument doc = new PdfDocument();
PdfNewPage page = (PdfNewPage) doc.getPages().add();
PdfMarker marker = new PdfMarker(PdfUnorderedMarkerStyle.Custom_String);
marker.setText("AAA");
String listContent = "Data Structure\n"
+ "Algorithm\n"
+ "Computer Newworks\n"
+ "Operating System\n"
+ "C Programming\n"
+ "Computer Organization and Architecture";
PdfUnorderedList list = new PdfUnorderedList(listContent);
list.setIndent(2);
list.setTextIndent(4);
list.setMarker(marker);
list.draw(page, 100, 100);
doc.saveToFile(outputFile, FileFormat.PDF);
doc.close();功能调整:
问题修复: