ICE.ICE|

韩憨

园龄:4年7个月粉丝:42关注:47

spire word pdf的一些操作

pom依赖

<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>3.7.2</version>
</dependency>

<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>

 
public static void main(String[] args) throws IOException {

//==========================================================pdf转word======================================================================
// //创建一个PdfDocument对象
// PdfDocument doc = new PdfDocument();
// //加载一个示例PDF文件
// doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\bbb\\202111136103030000026.pdf");
// //另存为.doc文件(没有表格格式了)
// doc.saveToFile("C:\\Users\\Administrator\\Desktop\\bbb\\202111136103030000026.doc", FileFormat.DOC);
// //另存为。.docx 文件
// doc.saveToFile("C:\\Users\\Administrator\\Desktop\\bbb\\202111136103030000026.docx", FileFormat.DOCX);
// doc.close();

//==========================================================word转pdf======================================================================

// //Create a Document instance 创建一个Document实例
// Document doc = new Document();
// //Load a sample Word document 加载一个Word文档样本
// doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\bbb\\202111136103030000026.docx");
// //Create a ToPdfParameterList instance 创建一个ToPdfParameterList实例
// ToPdfParameterList ppl = new ToPdfParameterList();
// //Embed all fonts in the PDF document 在PDF文件中嵌入所有字体
// ppl.isEmbeddedAllFonts(true);
// //Remove the hyperlinks and keep the character formats 删除超链接,保留字符格式
// ppl.setDisableLink(true);
// //Set the output image quality as 40% of the original image. 80% is the default setting. 将输出图像质量设置为原始图像的40%。80%是默认设置。
// doc.setJPEGQuality(40);
// //Save the document as PDF 将文件保存为PDF格式
// doc.saveToFile("C:\\Users\\Administrator\\Desktop\\bbb\\hanby.pdf", ppl);

//=========================================================pdf转图片==========================================================================
// //Create a PdfDocument instance 创建一个PdfDocument实例
// PdfDocument pdf = new PdfDocument();
// //Load a PDF sample document 加载一个PDF样本文件
// pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\bbb\\202111136103030000026.pdf");
// //Loop through every page 循环浏览每一页
// for (int i = 0; i < pdf.getPages().getCount(); i++) {
// //Convert all pages to images and set the image Dpi 将所有页面转换为图像并设置图像DPI
// BufferedImage image = pdf.saveAsImage(i, PdfImageType.Bitmap, 500, 500);
// //Save images to a specific folder as a .png files 将图像以.png文件的形式保存到一个特定的文件夹中
// File file = new File("C:\\Users\\Administrator\\Desktop\\bbb\\" + String.format(("ToImage-img-%d.png"), i));
// ImageIO.write(image, "PNG", file);
// }
// pdf.close();


//=========================================================word去除水印==========================================================================
// //Create a Document instance 创建一个文档实例
// Document doc = new Document();
// //Load the Word document 加载Word文档
// doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\bbb\\456789.docx");
// //Set the watermark as null to remove the image (or text) watermark 将水印设为空,以去除图像(或文本)水印
// doc.setWatermark(null);
// //Save the document 保存文件
// doc.saveToFile("C:\\Users\\Administrator\\Desktop\\bbb\\789123.docx", FileFormat.Docx_2013);

//=========================================================word加文本水印==========================================================================
// //Create a Document instance 创建一个文档实例
// Document document = new Document();
// //Load a sample Word document 加载一个Word文档样本
// document.loadFromFile("C:\\Users\\Administrator\\Desktop\\bbb\\12346.docx");
// //Get the first section 获取第一部分
// Section section = document.getSections().get(0);
// //Create a TextWatermark instance 创建一个TextWatermark实例
// TextWatermark txtWatermark = new TextWatermark();
// //Set the format of the text watermark 设置文本水印的格式
// txtWatermark.setText("Hanby");
// txtWatermark.setFontSize(100);
// txtWatermark.setColor(Color.red);
// txtWatermark.setLayout(WatermarkLayout.Horizontal);//Diagonal对角线 Horizontal横向
// //Add the text watermark to document 将文字水印添加到文档中
// section.getDocument().setWatermark(txtWatermark);
// //Save the document to file 将文件保存到文件中
// document.saveToFile("C:\\Users\\Administrator\\Desktop\\bbb\\456789.docx", FileFormat.Docx);
//=========================================================word加图片水印==========================================================================
// //Create a Document instance 创建一个文档实例
// Document document = new Document();
// //Load a sample Word document 加载一个Word文档样本
// document.loadFromFile("C:\\Users\\Administrator\\Desktop\\bbb\\12346.docx");
// //Create a PictureWatermark instance 创建一个PictureWatermark实例
// PictureWatermark picture = new PictureWatermark();
// //Set the format of the picture watermark 设置图片水印的格式
// picture.setPicture("C:\\Users\\Administrator\\Desktop\\123.png");
// picture.setScaling(100);
// picture.isWashout(false);
// //Add the text watermark to document 将文字水印添加到文档中
// document.setWatermark(picture);
// //Save the result file Save the result file
// document.saveToFile("C:\\Users\\Administrator\\Desktop\\bbb\\pic.docx", FileFormat.Docx);
//=========================================================word加多个文本水印(待处理,有问题)==========================================================================
// //Load the sample document 加载样本文件
// Document doc = new Document();
// doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\bbb\\202111136103030000026.docx");
// //Add WordArt shape and set the size 添加WordArt形状并设置大小
// ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
// shape.setWidth(60);//设置宽度
// shape.setHeight(20);//设置高度
// //Set the text, position and sytle for the wordart 设置字画的文字、位置和结构
// shape.setVerticalPosition(30);//设置垂直位置
// shape.setHorizontalPosition(20);//设置水平位置
// shape.setRotation(315);//设置浮动
// shape.getWordArt().setText("Confidential");
// shape.setFillColor(Color.red);//设置填充颜色
// shape.setLineStyle(ShapeLineStyle.Double);//Single Default 设置线条风格
// shape.setStrokeColor(new Color(2, 4, 4, 255));//设置笔画颜色
// shape.setStrokeWeight(100);//设置笔画重量
// Section section;
// HeaderFooter header;
// for (int n = 0; n < doc.getSections().getCount(); n++) {
// section = doc.getSections().get(n);
// //Get the header of section 获取章节的标题
// header = section.getHeadersFooters().getHeader();
// Paragraph paragraph1;
// for (int i = 0; i < 4; i++) {
// //Add the hearder to the paragraph 在该段中加入听者
// paragraph1 = header.addParagraph();
// for (int j = 0; j < 3; j++) {
// //copy the word are and add it to many places 抄写word并将其添加到许多地方
// shape = (ShapeObject) shape.deepClone();
// shape.setVerticalPosition(50 + 150 * i);
// shape.setHorizontalPosition(20 + 160 * j);
// paragraph1.getChildObjects().add(shape);
// }
// }
// }
// //Save the document to file 将文件保存到文件中
// doc.saveToFile("C:\\Users\\Administrator\\Desktop\\bbb\\more.docx", FileFormat.Docx_2013);

//=========================================================word加多个图片水印==========================================================================
// //Load the sample file 加载样本文件
// Document doc = new Document();
// doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\bbb\\more.docx");
// //Load the image 加载图像(图片像素不要太大50左右)
// DocPicture picture = new DocPicture(doc);
// picture.loadImage("C:\\Users\\Administrator\\Desktop\\123.png");
// //Set the text wrapping style 设置文本包覆样式
// picture.setTextWrappingStyle(TextWrappingStyle.Behind);//Behind Inline Square Through Tight Top_And_Bottom None In_Front_Of_Text
// 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 < 4; p++) {
// for (int q = 0; q < 3; 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("C:\\Users\\Administrator\\Desktop\\bbb\\morepic.docx", FileFormat.Docx_2013);


//=========================================================pdf加文本水印==========================================================================
// //create a PdfDocument instance 创建一个PdfDocument实例
// PdfDocument pdf = new PdfDocument();
// //load the sample document 加载样本文件
// pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\bbb\\hanby.pdf");
// //get the first page of the PDF 获得PDF的第一页
// PdfPageBase page = pdf.getPages().get(0);
// //use insertWatermark()to insert the watermark 使用insertWatermark()来插入水印
// insertWatermark(page, "Hanby");
// //save the document to file 将文件保存到文件中
// pdf.saveToFile("C:\\Users\\Administrator\\Desktop\\bbb\\haha.pdf");


//=========================================================pdf加图片水印==========================================================================
//Create a pdf document and load the sample document from file 创建一个pdf文档并从文件中加载示例文档
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\bbb\\hanby.pdf");
//Get the first page 获取第一页
PdfPageBase page = doc.getPages().get(0);
//Load the image and set it as background image 加载图片并将其设置为背景图片
page.setBackgroundImage("C:\\Users\\Administrator\\Desktop\\123.png");
//Set the background region 设置背景区域
Rectangle2D.Float rect = new Rectangle2D.Float();
rect.setRect(280, 300, 150, 150);
page.setBackgroundRegion(rect);
//Save pdf file 保存pdf文件
doc.saveToFile("C:\\Users\\Administrator\\Desktop\\bbb\\hahapic.pdf");
doc.close();

}


static void insertWatermark(PdfPageBase page, String watermark) {
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(watermark, new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.getViolet(), 0, 0, new PdfStringFormat(PdfTextAlignment.Center));
brush.getGraphics().restore();
brush.getGraphics().setTransparency(1);
Rectangle2D loRect = new Rectangle2D.Float();
loRect.setFrame(new Point2D.Float(0, 0), page.getCanvas().getClientSize());
page.getCanvas().drawRectangle(brush, loRect);
}

本文作者:韩憨

本文链接:https://www.cnblogs.com/hanby/p/16145401.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   韩憨  阅读(338)  评论(0编辑  收藏  举报
//看板娘

哥伦布

评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 隔离 (Studio Live Duet) 陈凯咏,林家谦
  2. 2 明知做戏 吴雨霏
  3. 3 残酷游戏 卫兰
  4. 4 你,好不好? 周兴哲
  5. 5 我可以 蔡旻佑
  6. 6 云烟成雨 房东的猫
  7. 7 说散就散 JC 陈咏桐
  8. 8 我配不上你 夏天Alex
  9. 9 不再联系 夏天Alex
  10. 10 等我先说 夏天Alex
  11. 11 我知道他爱你 夏天Alex
  12. 12 多想在平庸的生活拥抱你 隔壁老樊
  13. 13 这一生关于你的风景 隔壁老樊
  14. 14 我曾 隔壁老樊
  15. 15 关于孤独我想说的话 隔壁老樊
  16. 16 过客 周思涵
  17. 17 备爱 周思涵
  18. 18 嚣张 en
  19. 19 海口 后弦
明知做戏 - 吴雨霏
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Xia Zhi

作曲 : Fong Man Leung

编曲 : 吴国恩

监制 : Gary Chan

等你的汽水喝一半给你加片薄冰

等你的桌面满泻我总会打理重整

不想纯情 不够聪明

你未发现我的身影

得我帮你依照编码整理家里电影

得我帮你依照编码整理家里电影

只会得我一个帮你选购喜爱铃声

天天如常 估你心情

等一个眼神求证 一闪擦过如流星

怎么我为我做过的感到惊怕

就像爱吗我也不肯定恐怕

我以为存在吗 千变万化

从来不肯开口可相信吗 离谱吗

请你不要阻我喜欢你

明明是爱但你未说话你扮作闪避

这个沉默冷静的你毫无办法处理

其实我亦怕是错摸心理

总有天会等到好天气

游行示爱大叫着你在某大片草地

等你无用退避不过仍然害羞的你

还是顾忌太不争气 明知做戏

即使你未太在意不感到惊讶

即使你未太在意不感到惊讶

现在要说爱你请准备招架

勇气还存在吗 不要害怕

随时真的胆敢亲手送花 离谱吗

请你不要阻我喜欢你

明明是爱但你未说话你扮作闪避

这个沉默冷静的你亳无办法处理

其实我亦怕是错摸心理

总有天会等到好天气

游行示爱大叫着你在某大片草地

等你无用退避不过仍然害羞的你

还是顾忌太不争气 明知做戏

不过不要阻我紧张你

如何令你愉快让我办妥为你准备

喜爱沉默冷静的你还是自信的你

仍愿意为你造一些惊喜

总有天会等到好天气

游行示爱大叫着你在某大片草地

等你无用退避不过途人目光不理

期待贴着你的手臂 无须做戏

等你喜爱等你不爱就凭摘毫验证

等你喜爱等你不爱就凭摘毫验证

想爱不爱偏爱不理亦同样难划清

天天如常 估你心情

不想扑索来求证 争取过趁还年青

终于你下决定来答应 太动听

点击右上角即可分享
微信分享提示