mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2026-04-11 02:37:09 +00:00
cad pdf转换模块提取独立 优化多线程转换方法
This commit is contained in:
@@ -4,74 +4,9 @@ import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public class RemoveSvgAdSimple {
|
||||
|
||||
/**
|
||||
* 改进版本:直接处理SVG内容,保留包含transform的<g>元素及其内容
|
||||
* @param svgContent SVG内容字符串
|
||||
* @return 清理后的SVG内容
|
||||
*/
|
||||
public static String removeSvgAdPrecisely(String svgContent) {
|
||||
// 使用非贪婪模式匹配包含transform的<g>元素及其完整内容
|
||||
String preservePattern = "<g\\s+[^>]*transform\\s*=\\s*\"[^\"]*\"[^>]*>.*?</g>";
|
||||
|
||||
// 查找所有包含transform的<g>元素
|
||||
Pattern pattern = Pattern.compile(preservePattern, Pattern.DOTALL);
|
||||
Matcher matcher = pattern.matcher(svgContent);
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
// 添加XML声明和SVG根元素
|
||||
if (svgContent.contains("<?xml")) {
|
||||
result.append("<?xml version=\"1.0\" standalone=\"no\"?>");
|
||||
}
|
||||
|
||||
// 找到SVG开始标签
|
||||
int svgStart = svgContent.indexOf("<svg");
|
||||
int svgEnd = svgContent.indexOf(">", svgStart) + 1;
|
||||
|
||||
if (svgStart != -1) {
|
||||
// 添加SVG开始标签
|
||||
result.append(svgContent.substring(svgStart, svgEnd));
|
||||
|
||||
// 收集所有包含transform的<g>元素
|
||||
while (matcher.find()) {
|
||||
result.append("\n").append(matcher.group());
|
||||
}
|
||||
|
||||
// 添加SVG结束标签
|
||||
result.append("\n</svg>");
|
||||
} else {
|
||||
// 如果没有找到SVG标签,返回空或原始内容
|
||||
return svgContent;
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单暴力版本:直接删除特定广告内容
|
||||
* @param svgContent SVG内容字符串
|
||||
* @return 清理后的SVG内容
|
||||
*/
|
||||
public static String removeSvgAdSimple(String svgContent) {
|
||||
// 查找包含广告的<g>元素(根据你的示例,广告通常包含stroke="#FFFFFF"等特征)
|
||||
String adPattern1 = "<g>\\s*<path[^>]*stroke=\"#FFFFFF\"[^>]*>.*?</path>\\s*<path[^>]*fill=\"#FFFFFF\"[^>]*>.*?</path>\\s*</g>";
|
||||
String adPattern2 = "<g>\\s*<path[^>]*M0 0L[^>]*stroke=\"#FFFFFF\"[^>]*>.*?</path>.*?</g>";
|
||||
|
||||
String result = svgContent;
|
||||
result = result.replaceAll(adPattern1, "");
|
||||
result = result.replaceAll(adPattern2, "");
|
||||
|
||||
// 也可以直接删除所有不含transform属性的顶级<g>元素
|
||||
// 这个正则会删除不带transform的顶级<g>,但保留嵌套的<g>
|
||||
result = result.replaceAll("(?s)<g>(?:(?!<g>).)*?</g>", "");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更可靠的版本:使用DOM解析思路,但用正则实现
|
||||
@@ -84,7 +19,7 @@ public class RemoveSvgAdSimple {
|
||||
// 找到XML声明
|
||||
if (svgContent.contains("<?xml")) {
|
||||
int xmlEnd = svgContent.indexOf("?>") + 2;
|
||||
cleaned.append(svgContent.substring(0, xmlEnd)).append("\n");
|
||||
cleaned.append(svgContent, 0, xmlEnd).append("\n");
|
||||
}
|
||||
|
||||
// 找到SVG开始标签
|
||||
@@ -92,7 +27,7 @@ public class RemoveSvgAdSimple {
|
||||
if (svgStart == -1) return svgContent;
|
||||
|
||||
int svgEnd = svgContent.indexOf(">", svgStart) + 1;
|
||||
cleaned.append(svgContent.substring(svgStart, svgEnd)).append("\n");
|
||||
cleaned.append(svgContent, svgStart, svgEnd).append("\n");
|
||||
|
||||
// 解析剩余内容
|
||||
String remaining = svgContent.substring(svgEnd);
|
||||
@@ -133,7 +68,7 @@ public class RemoveSvgAdSimple {
|
||||
}
|
||||
|
||||
if (gClose != -1) {
|
||||
cleaned.append(remaining.substring(gStart, gClose)).append("\n");
|
||||
cleaned.append(remaining, gStart, gClose).append("\n");
|
||||
pos = gClose;
|
||||
} else {
|
||||
pos = gTagEnd + 1;
|
||||
@@ -166,14 +101,14 @@ public class RemoveSvgAdSimple {
|
||||
public static void removeSvgAdFromFile(String sourceFilePath, String targetFilePath) throws IOException {
|
||||
// 读取文件内容
|
||||
Path sourcePath = Paths.get(sourceFilePath);
|
||||
String svgContent = new String(Files.readAllBytes(sourcePath), StandardCharsets.UTF_8);
|
||||
String svgContent = Files.readString(sourcePath);
|
||||
|
||||
// 清理SVG广告
|
||||
String cleanedContent = removeSvgAdReliable(svgContent);
|
||||
|
||||
// 写入目标文件
|
||||
Path targetPath = Paths.get(targetFilePath);
|
||||
Files.write(targetPath, cleanedContent.getBytes(StandardCharsets.UTF_8));
|
||||
Files.writeString(targetPath, cleanedContent);
|
||||
System.out.println("SVG广告清理完成!");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user