批量插入PDF文件到cad
本帖最后由 jun353835273 于 2023-8-30 09:15 编辑使用netload 加载Countpdfpages.dll (适用于CAD 版本2013+)文件
(setq PDFFile (getfiled "Select thePDF" "" "PDF" 2))
(setq num (Countpdfpages PDFFile));读取pdf页码函数,使用的的c#搞的内库,非纯lisp语言。
注意事项
1、itextsharp.dll文件不能移动,如果移动位置都必须和Countpdfpages.dll在一个文件夹下。
c#代码 高手看来小儿科,不过c#学了点皮毛仅能结合lisp使用
希望c#大佬多发点实用的内库函数来用用。
本帖最后由 moshouhot 于 2024-11-25 15:06 编辑
moshouhot 发表于 2024-6-5 13:21
请问在不实际插入PDF的情况下,能否提前获取到PDF的第一页的尺寸。
using iText.Kernel.Pdf;
namespace aaAcad;
public class Mylisphelper {
/// <summary>
/// 获取指定PDF文件的总页数
/// </summary>
/// <param name="rb">LISP参数缓冲区,需要包含一个TypeCode为5005的字符串类型参数,表示PDF文件路径</param>
/// <returns>
/// 返回值说明:
/// - 如果成功读取PDF,返回PDF的总页数
/// - 如果发生以下情况则返回0:
/// 1. 输入参数为null
/// 2. 参数格式不正确
/// 3. 指定的文件不存在
/// </returns>
/// <remarks>
/// 此方法通过AutoCAD LISP接口调用,用于获取PDF文件的页数。
/// 当文件不存在时会显示提示消息框。
/// </remarks>
/// <example>
/// LISP调用示例:
/// (Countpdfpages "C:\\example.pdf")
/// </example>
public int Countpdfpages(ResultBuffer rb) {
int result = 0;
if (rb == null) {
return 0;
}
TypedValue[] array = rb.AsArray();
if (array.Length == 1 && array.TypeCode == 5005) {
string filePath = array.Value as string;
if (File.Exists(filePath)) {
using (var pdfReader = new PdfReader(filePath))
using (var pdfDoc = new PdfDocument(pdfReader)) {
result = pdfDoc.GetNumberOfPages();
}
} else {
result = 0;
MessageBox.Show("么有文件");
}
}
return result;
}
/// <summary>
/// 获取PDF文件首页的尺寸(宽度和高度,单位:毫米)
/// </summary>
/// <param name="rb">LISP参数缓冲区,需要包含一个TypeCode为5005的字符串类型参数,表示PDF文件路径</param>
/// <returns>
/// 返回值说明:
/// - 如果成功读取PDF,返回包含两个浮点数的ResultBuffer,分别表示宽度和高度(毫米)
/// - 如果发生以下情况则返回null:
/// 1. 输入参数为null
/// 2. 参数格式不正确
/// 3. 指定的文件不存在
/// 4. PDF文件读取失败
/// </returns>
/// <remarks>
/// 此方法通过AutoCAD LISP接口调用,用于获取PDF文件首页的尺寸。
/// 当文件不存在时会显示提示消息框。
/// 返回的尺寸基于PDF的MediaBox,单位转换为毫米。
/// </remarks>
/// <example>
/// LISP调用示例:
/// (getpdfsize "C:\\example.pdf")
/// 返回格式: (宽度 高度) ;单位:毫米
/// </example>
public ResultBuffer GetPdfSize(ResultBuffer rb) {
if (rb == null) {
Env.Print("\n输入参数为null");
return null;
}
TypedValue[] array = rb.AsArray();
if (array.Length != 1 || array.TypeCode != 5005) {
Env.Print("\n参数格式不正确");
return null;
}
string filePath = array.Value as string;
// Env.Print($"\n文件路径: {filePath}");
if (!File.Exists(filePath)) {
Env.Print("\n文件不存在");
// MessageBox.Show("么有文件");
return null;
}
try {
using (var pdfReader = new PdfReader(filePath))
using (var pdfDoc = new PdfDocument(pdfReader)) {
var page = pdfDoc.GetPage(1);
// Env.Print("\n成功读取PDF第一页");
// 优先使用CropBox,如果不存在则使用MediaBox
var pageSize = page.GetCropBox() ?? page.GetMediaBox();
const double POINTS_TO_MM = 0.352778;
double width = pageSize.GetWidth() * POINTS_TO_MM;
double height = pageSize.GetHeight() * POINTS_TO_MM;
// Env.Print($"\n尺寸: {width}mm x {height}mm");
return new ResultBuffer(
new TypedValue(5001, width), // 包装在TypedValue中
new TypedValue(5001, height)
);
}
} catch (Exception ex) {
Env.Print($"\n读取PDF文件失败: {ex.Message}");
// MessageBox.Show($"读取PDF文件失败: {ex.Message}");
return null;
}
}
}
时隔半年,穿越过来回答我自己的问题。 hower 发表于 2023-8-28 20:24
CAD2020使用这个插件,显示错误: no function definition: COUNTPDFPAGES,这个怎么处理咧
命令行输入:netload 加载Countpdfpages.dll 文件(适用于CAD 版本2013+)
树櫴希德 发表于 2023-8-25 17:06
2018版本的pdf转cad能套出函数吗
可以的。 14版本的cad都可以插入PDF进去。刚刚测试了的
就是多页pdf插入cad么?
大佬高产了,谢谢分享。 lxl217114 发表于 2023-8-24 17:11
就是多页pdf插入cad么?
大佬高产了,谢谢分享。
是的,按默认尺寸插入的和插入光栅差不多 jun353835273 发表于 2023-8-24 17:35
是的,按默认尺寸插入的和插入光栅差不多
好工具,那弥补了pdfimport只能输入1P的缺陷。 膜拜大神:lol 又一个好工具,,, 对图形插入的效果如何?能像pdfimport一样吗? 1111111111111 好东西学习学习 感谢大佬分享