呼呼se 发表于 2024-10-5 11:00:39

这个导入进去是线段还是图片呢

1028370790 发表于 2024-10-10 13:10:47

感觉大佬分享

呼呼se 发表于 2024-11-20 08:59:47

可以作为块插入吗

moshouhot 发表于 2024-11-25 11:04:30

本帖最后由 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;
      }
    }
}


时隔半年,穿越过来回答我自己的问题。

小鸟 发表于 2024-11-26 23:53:59

pdf插入后 绘图会卡卡的有办法解决码?

xibiao 发表于 2024-11-29 19:53:52

2025浩辰CAD不能用呢

xibiao 发表于 2024-11-29 19:59:43

命令: T23 ; 错误: no function definition: COUNTPDFPAGES

hao3ren 发表于 2024-12-27 17:45:39

2014加载图纸只显示外框,2025正常

cfxcq 发表于 2025-1-5 09:19:44

利害,我到处找终于找到了,谢谢

yefei812678 发表于 2025-1-5 12:25:39


感谢作者的无私分享....
页: 1 2 3 4 5 6 7 8 9 [10] 11
查看完整版本: 批量插入PDF文件到cad