• spire word pdf的一些操作


     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);
    }
  • 相关阅读:
    undo表空间
    SQL*Plus快速入门
    win10用命令net启动服务没权限解决办法
    Oracle表空间管理
    Oracle数据泵(上)
    windows的bat脚本
    Servlet映射细节
    JSP技术(一)
    690. Employee Importance
    BFS和DFS详解以及java实现(转载)
  • 原文地址:https://www.cnblogs.com/hanby/p/16145401.html
Copyright © 2020-2023  润新知