Java 添加、删除、旋转PDF页面
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">操作PDF文档时,<span style="color: black;">咱们</span><span style="color: black;">一般</span>会<span style="color: black;">必须</span>添加、删除或旋转<span style="color: black;">有些</span>PDF页面。本文就将<span style="color: black;">经过</span><span style="color: black;">运用</span>Java程序来<span style="color: black;">仔细</span>介绍<span style="color: black;">怎样</span>在PDF文档中添加、删除和旋转页面。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">运用</span>工具:Free Spire.PDF for Java(免费版)</h1>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">Jar文件获取及导入:</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">办法</span>1:</strong><span style="color: black;">经过</span>E-iceblue中文官网下载获取jar包。解压后将lib文件夹下的Spire.Pdf.jar文件导入Java程序。(如下图)</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/e25f294be0fa417d8ec9b15fcd609f6b~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723889717&x-signature=%2BROsYWYza3cDdebRtyXdCQg9wOk%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">办法</span>2:</strong><span style="color: black;">经过</span>maven仓库安装导入。<span style="color: black;">详细</span>安装详解参见E-iceblue中文官网。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">【示例1】添加新页面</h1><span style="color: black;">import</span> com.spire.pdf.*;
<span style="color: black;">import</span> java.awt.*;
<span style="color: black;">import</span>com.spire.pdf.graphics.*;<span style="color: black;">import</span> java.awt.geom.Rectangle2D;
<span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">AddPage</span> </span>{
<span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">void</span> <span style="color: black;">main</span><span style="color: black;">(String[] args)</span> </span>{
<span style="color: black;">//创建PdfDocument对象并加载示例文档</span>
PdfDocument pdf = <span style="color: black;">new</span>PdfDocument();
pdf.loadFromFile(<span style="color: black;">"C:\\Users\\Test1\\Desktop\\Sample.pdf"</span>);
<span style="color: black;">//添加新页面</span>
PdfPageBase page = pdf.getPages().add();
<span style="color: black;">//创建TrueType字体对象</span>
PdfTrueTypeFont font= <span style="color: black;">new</span>PdfTrueTypeFont(<span style="color: black;">new</span> Font(<span style="color: black;">"Arial Unicode MS"</span>,Font.PLAIN,<span style="color: black;">14</span>),<span style="color: black;">true</span>);
<span style="color: black;">//设置字体格式、<span style="color: black;">体积</span></span>
PdfRGBColor blue = <span style="color: black;">new</span> PdfRGBColor();
blue.setB((<span style="color: black;">byte</span>) <span style="color: black;">255</span>);
PdfSolidBrush brush =<span style="color: black;">new</span> PdfSolidBrush(blue);
Rectangle2D.Float rctg1 = <span style="color: black;">new</span> Rectangle2D.Float();
rctg1.setRect(<span style="color: black;">0</span>,<span style="color: black;">70</span>,page.getCanvas().getClientSize().getWidth() /<span style="color: black;">2</span>,<span style="color: black;">100</span>);
<span style="color: black;">//绘制文字</span>
page.getCanvas().drawString(<span style="color: black;">"新添加页面"</span>, font, brush, rctg1);
<span style="color: black;">//<span style="color: black;">保留</span>文档</span>
pdf.saveToFile(<span style="color: black;">"output/添加页面.pdf"</span>);
}
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">添加效果:</strong></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/1b99d18091894e42b26968fd08a2b4ba~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723889717&x-signature=bLHozQ5M7XVjhqTpgJ6wUq6VdsE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">【示例2】删除页面</h1><span style="color: black;">import</span>com.spire.pdf.*;<span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">DeletePage</span> {</span>
<span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">void</span> <span style="color: black;">main</span><span style="color: black;">(String[] args)</span> </span>{
<span style="color: black;">//创建PdfDocument对象并加载示例文档</span>
PdfDocument pdf = <span style="color: black;">new</span>PdfDocument();
pdf.loadFromFile(<span style="color: black;">"C:\\Users\\Test1\\Desktop\\添加页面.pdf"</span>);
<span style="color: black;">//删除第二页</span>
pdf.getPages().removeAt(<span style="color: black;">1</span>);
<span style="color: black;">//<span style="color: black;">保留</span>文档</span>
pdf.saveToFile(<span style="color: black;">"output/删除页面.pdf"</span>);
}
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">删除效果:</strong></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/89d4fce9dc7e40989ef5f2f67a0adbd6~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723889717&x-signature=6WjgdBjnA1o4eDsEeu0%2B6sbrYtk%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">【示例3】旋转页面</h1><span style="color: black;">import</span>com.spire.pdf.PdfDocument;<span style="color: black;">import</span> com.spire.pdf.PdfPageBase;
<span style="color: black;">import</span> com.spire.pdf.PdfPageRotateAngle;
<span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">RotatePage</span> {</span>
<span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">void</span> <span style="color: black;">main</span><span style="color: black;">(String[] args)</span> </span>{
<span style="color: black;">//加载PDF文档</span>
PdfDocument pdf = <span style="color: black;">new</span> PdfDocument();
pdf.loadFromFile(<span style="color: black;">"C:\\Users\\Test1\\Desktop\\Sample.pdf"</span>);
<span style="color: black;">//获取<span style="color: black;">第1</span>页</span>PdfPageBase page = pdf.getPages().get(<span style="color: black;">0</span>);
<span style="color: black;">//获取<span style="color: black;">第1</span>页当前的旋转<span style="color: black;">方向</span></span>
<span style="color: black;">int</span> rotateAngle = page.getRotation().getValue();
<span style="color: black;">//在当前旋转<span style="color: black;">方向</span>的<span style="color: black;">基本</span>上,将<span style="color: black;">第1</span>页顺时针旋转90度(可选0/90/180/270度)</span>rotateAngle += PdfPageRotateAngle.Rotate_Angle_90.getValue();
page.setRotation((PdfPageRotateAngle.fromValue(rotateAngle)));<span style="color: black;">//<span style="color: black;">保留</span>文档</span>
pdf.saveToFile(<span style="color: black;">"output/Rotated.pdf"</span>);
}
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">旋转效果:</strong></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/9a09e0ae298d49ad8ab84bfa5510214b~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723889717&x-signature=pMsMIaTO5kqZJ%2BnWPen1uTuqfkw%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(本文完)</p>
祝福你、祝你幸福、早日实现等。 外链发布社区 http://www.fok120.com/ 外贸B2B平台有哪些?
页:
[1]