为有中文需求的客户提供多渠道中文技术支持.

Thu Mar 03, 2022 1:56 pm

您好!使用spire doc for java 在页眉左侧位置如何插入图片时,使图片置于顶层或删除相同位置的图片?
如何去掉字体黑色底纹?

fengyun_23
 
Posts: 13
Joined: Fri Oct 08, 2021 3:33 am

Fri Mar 04, 2022 2:20 am

您好,

感谢询问!

请参考下面代码实现您的需求。 如果您还有其他问题,请随时联系我们。
Code: Select all
   private static void InsertPicAndCleanColor(String filePath, String picPath) {
      Document document = new Document(filePath);
      HeaderFooter header = document.getSections().get(0).getHeadersFooters().getHeader();
      Paragraph headerParagraph = header.getParagraphs().get(0);
      DocPicture newPicture = new DocPicture(document);
      newPicture.loadImage(picPath);
      // if you want to delete the original pic
//      for (DocumentObject object : (Iterable<DocumentObject>) headerParagraph.getChildObjects()) {
//         if (object instanceof DocPicture) {
//            headerParagraph.getChildObjects().remove(object);
//            break;
//         }
//      }
      // if you want to move the new pic to top
      newPicture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);
      headerParagraph.getChildObjects().add(newPicture);

      // clean the black background
      Paragraph textParagraph = document.getSections().get(0).getParagraphs().get(0);
      textParagraph.getListFormat().getCurrentListLevel().getCharacterFormat().setHighlightColor(null);
      document.saveToFile("res.docx", FileFormat.Docx_2013);
      System.out.println("okk!");
   }

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Mar 04, 2022 7:44 am

图片置于顶层好使了,谢谢!
去掉字体黑色底纹有一部分好使一部分,能麻烦您帮忙看看吗?
我的代码:
for (int j = 0; j < document.getSections().getCount(); j++) {
Section section = document.getSections().get(j);
for (int i = 0; i < section.getParagraphs().getCount(); i++) {
Paragraph textParagraph = section.getParagraphs().get(i);
if(!"".equals(textParagraph.getListFormat().getCurrentListLevel()) && textParagraph.getListFormat().getCurrentListLevel() != null){
textParagraph.getListFormat().getCurrentListLevel().getCharacterFormat().setHighlightColor(null);

}
}
}

fengyun_23
 
Posts: 13
Joined: Fri Oct 08, 2021 3:33 am

Fri Mar 04, 2022 8:25 am

您好,

感谢反馈。

根据您提供的第二个文档,其中无法去除的黑色底纹分别是 黑色高亮的脚注“FootNote” 以及一张包含黑色长条的图片。 我之前给您的代码是去除段落列表的黑色高亮的。针对图片,只能你自己找到图片后删除;针对脚注 以及普通文本中的黑色高亮,请参考下面代码来去除。
Code: Select all
      for (int j = 0; j < document.getSections().getCount(); j++) {
         Section section = document.getSections().get(j);
         for (int i = 0; i < section.getParagraphs().getCount(); i++) {
            Paragraph textParagraph = section.getParagraphs().get(i);
            if (!"".equals(textParagraph.getListFormat().getCurrentListLevel())
                  && textParagraph.getListFormat().getCurrentListLevel() != null) {
               textParagraph.getListFormat().getCurrentListLevel().getCharacterFormat().setHighlightColor(null);
            }
            for (DocumentObject object : (Iterable<DocumentObject>) textParagraph.getChildObjects()) {
               if (object instanceof TextRange) {
                  TextRange range = (TextRange) object;
                  if (range.getCharacterFormat().getHighlightColor().equals(Color.BLACK)) {
                     range.getCharacterFormat().setHighlightColor(null);
                  }
               } else if (object instanceof Footnote) {
                  Footnote note = (Footnote) object;
                  if (note.getCharacterFormat().getHighlightColor().equals(Color.BLACK)) {
                     note.getCharacterFormat().setHighlightColor(null);
                  }
               }
            }
         }
      }


Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Mar 04, 2022 10:21 am

您好!请问使用spire doc for Java读取文本的时候,什么情况下会导致有些段落字体、脚注、图片自动添加黑色底纹及原有的三张不同的图片文件读取保存之后变成三个一模一样的图片?
您上面说针对图片,只能自己找到图片后删除是现在去除不了吗?

fengyun_23
 
Posts: 13
Joined: Fri Oct 08, 2021 3:33 am

Mon Mar 07, 2022 1:25 am

您好,

我们产品提取文本的 document.getText() 方法,并不会改变文档本身文字的底色或文档图片的内容。 您如果遇到了此问题,请提供给我们您的操作代码以及输入文档以供进一步调查。

此外,并不是不能删除图片,而是需要您自己去判断图片是否需要删除,因为我们并不能识别图片的具体内容。删除图片请参考下列代码:

Code: Select all
List<DocPicture> pictures = new List<DocPicture>();
... ...
... ...
else if (object instanceof DocPicture) {
                  pictures.add((DocPicture) object);
               }
... ...
for (DocPicture docPicture : pictures) {
         docPicture.getOwner().getChildObjects().remove(docPicture);
      }


Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to 中文技术支持