U3d和c4d:如何使用itext7和C#将u3d添加到现有的 pdf中

我正在使用 iText7 (C#) 创建 pdf。我需要在 exising pdf 中添加一个 u3d 图片。我可以找到示例 (://developers.itextpdf/examples/itext-action-second-edition/chapter-16#619-pdf3d.java),但它是 java 一个。任何人都可以帮助给我一个关于.net C# 的例子吗?

3

链接的示例适用于 iText5,而不是 iText7。在 iText7 中,此示例如下所示

  public static final String DEST = "./target/test/resources/book/part4/chapter16/Listing_16_16_Pdf3D.pdf";
    public static String RESOURCE = "./src/test/resources/img/teapot.u3d";
    public static void main(String args[]) throws Exception {
        new Listing_16_16_Pdf3D().manipulatePdf(DEST);
    }
    public void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        Rectangle rect = new Rectangle(100, 400, 400, 400);
        PdfStream stream3D = new PdfStream(pdfDoc, new FileInputStream(RESOURCE));
        stream3D.put(PdfName.Type, new PdfName("3D"));
        stream3D.put(PdfName.Subtype, new PdfName("U3D"));
        stream3D.setCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION);
        stream3D.flush();
        PdfDictionary dict3D = new PdfDictionary();
        dict3D.put(PdfName.Type, new PdfName("3DView"));
        dict3D.put(new PdfName("XN"), new PdfString("Default"));
        dict3D.put(new PdfName("IN"), new PdfString("Unnamed"));
        dict3D.put(new PdfName("MS"), PdfName.M);
        dict3D.put(new PdfName("C2W"),
                new PdfArray(new float[]{1, 0, 0, 0, 0, -1, 0, 1, 0, 3, -235, 28}));
        dict3D.put(PdfName.CO, new PdfNumber(235));
        Pdf3DAnnotation annot = new Pdf3DAnnotation(rect, stream3D);
        annot.setContents(new PdfString("3D Model"));
        annot.setDefaultInitialView(dict3D);
        pdfDoc.addNewPage().addAnnotation(annot);
        doc.close();
    }

或者,如果你想在 C #(没有在本地运行它,但 Visual Studio 不抱怨语法)

    public void manipulatePdf(String dest) {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        Rectangle rect = new Rectangle(100, 400, 400, 400);
        PdfStream stream3D = new PdfStream(pdfDoc, new FileInputStream(RESOURCE));
        stream3D.Put(PdfName.Type, new PdfName("3D"));
        stream3D.Put(PdfName.Subtype, new PdfName("U3D"));
        stream3D.SetCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION);
        stream3D.Flush();
        PdfDictionary dict3D = new PdfDictionary();
        dict3D.Put(PdfName.Type, new PdfName("3DView"));
        dict3D.Put(new PdfName("XN"), new PdfString("Default"));
        dict3D.Put(new PdfName("IN"), new PdfString("Unnamed"));
        dict3D.Put(new PdfName("MS"), PdfName.M);
        dict3D.Put(new PdfName("C2W"),
                new PdfArray(new float[] { 1, 0, 0, 0, 0, -1, 0, 1, 0, 3, -235, 28 }));
        dict3D.Put(PdfName.CO, new PdfNumber(235));
        Pdf3DAnnotation annot = new Pdf3DAnnotation(rect, stream3D);
        annot.SetContents(new PdfString("3D Model"));
        annot.SetDefaultInitialView(dict3D);
        pdfDoc.AddNewPage().AddAnnotation(annot);
        doc.Close();
    }
0

.net 的 iText 代码库被设计为 Java 的(几乎)精确镜像。

除了代码约定(例如以大写开头的方法名称),您应该能够在.net 中使用 java 代码。

这也解释了为什么我们通常不发布.net 的代码。我建议你简单地复制 / 粘贴 java 代码,更改方法名称以考虑代码约定,并尝试编译它。

如果有的话,它会给你一个代码样本,你可以在这里发布,让你发布更明智。

0

您也可以尝试在 C # 下面的 code,这是使用 spirepdf

/ / 步骤 1:初始化 PdfDocuemnt 的新对象,并在 PDF 文档中添加空白页。

        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.Pages.Add();
        //Step 2: Draw a rectangle on the page to define the canvas area for the 3D file.
        Rectangle rt = new Rectangle(0, 80, 200, 200);
        //Step 3: Initialize a new object of Pdf3DAnnotation, load the.u3d file as 3D annotation.
        Pdf3DAnnotation annotation = new Pdf3DAnnotation(rt, "E:\\Testingfolder\\u3dpdf\\BRO_JR_6910K Femur - Bone Model.stl");
        annotation.Activation = new Pdf3DActivation();
        annotation.Activation.ActivationMode = Pdf3DActivationMode.PageOpen;
        //Step 4: Define a 3D view mode.
        Pdf3DView View = new Pdf3DView();
        View.Background = new Pdf3DBackground(new PdfRGBColor());
        View.ViewNodeName = "test";
        View.RenderMode = new Pdf3DRendermode(Pdf3DRenderStyle.Solid);
        View.InternalName = "test";
        View.LightingScheme = new Pdf3DLighting();
        View.LightingScheme.Style = Pdf3DLightingStyle.Day;
        //Step 5: Set the 3D view mode for the annotation.
        annotation.Views.Add(View);
        //Step 6: Add the annotation to PDF.
        page.AnnotationsWidget.Add(annotation);
        //Step 7: Save the file.
        doc.SaveToFile("E:\\Testingfolder\\u3dpdf\\Create3DPdf.pdf", FileFormat.PDF);
0

这是使用 c # 将 u3d 文件打印为 pdf 的工作代码

FileStream stream = new FileStream("E:\\Testingfolder\\u3dpdf\\DoctoPdf.pdf", FileMode.Open, FileAccess.Read);
        String RESOURCE;
        RESOURCE = "E:\\Testingfolder\\u3dpdf\\Testu3d.u3d";
        iTextSharp.text.Rectangle rect;
        using (iTextSharp.text.Document document = new iTextSharp.text.Document())
        {
            PdfWriter pdfwriter = PdfWriter.GetInstance(document, stream);
            // step 3
            document.Open();
            // step 4
            rect = new iTextSharp.text.Rectangle(100, 400, 500, 800);
            rect.Border = iTextSharp.text.Rectangle.BOX;
            rect.BorderWidth = 0.5f;
            rect.BorderColor = new BaseColor(0xFF, 0x00, 0x00);
            document.Add(rect);
            document.SetMargins(129,259,647,1416);
            PdfIndirectObject streamObject = null;
            using (FileStream fs =
                new FileStream(RESOURCE, FileMode.Open, FileAccess.Read))
            {
                PdfStream stream3D = new PdfStream(fs, pdfwriter);
                stream3D.Put(PdfName.TYPE, new PdfName("3D"));
                stream3D.Put(PdfName.SUBTYPE, new PdfName("U3D"));
                stream3D.FlateCompress();
                streamObject = pdfwriter.AddToBody(stream3D);
                stream3D.WriteLength();
            }
            PdfDictionary dict3D = new PdfDictionary();
            dict3D.Put(PdfName.TYPE, new PdfName("3DView"));
            dict3D.Put(new PdfName("XN"), new PdfString("Default"));
            dict3D.Put(new PdfName("IN"), new PdfString("Unnamed"));
            dict3D.Put(new PdfName("MS"), PdfName.M);
            dict3D.Put(new PdfName("C2W"),
                    new PdfArray(new float[] { 1, 0, 0, 0, 0, -1, 0, 1, 0, 3, -235, 28 }));
            dict3D.Put(PdfName.CO, new PdfNumber(235));
            PdfIndirectObject dictObject = pdfwriter.AddToBody(dict3D);
            PdfAnnotation annot = new PdfAnnotation(pdfwriter, rect);
            annot.Put(PdfName.CONTENTS, new PdfString("3D Model"));
            annot.Put(PdfName.SUBTYPE, new PdfName("3D"));
            annot.Put(PdfName.TYPE, PdfName.ANNOT);
            annot.Put(new PdfName("3DD"), streamObject.IndirectReference);
            annot.Put(new PdfName("3DV"), dictObject.IndirectReference);
            PdfAppearance ap = pdfwriter.DirectContent.CreateAppearance(
                rect.Width, rect.Height
            );
            annot.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap);
            annot.SetPage();
            pdfwriter.AddAnnotation(annot);
        }

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(704)
D sur:BigSurmacOS11.2.1(20D74)正在从“APFS系统快照”运行-如何修复
上一篇
Link cloud:链接JIRA云和Bitbucket云
下一篇

相关推荐

  • css禅意花园pdf用简单的代码让网页变得更美

    CSS Zen Garden(CSS禅意花园)是一个由Dave Shea于2003年创建的网站,旨在展示CSS的功能。该网站提供了一个HTML文档,该文档包含一系列用CSS样式表格式化的元素,但没有任何表示外观的CSS代码。任何人都可以使用此HTML文档,并通过改变CSS样式表来改变文档的外观。…

    2023-07-21 06:36:31
    0 24 65
  • qt quick核心编程pdf深入理解Qt Quick的基础和应用

    Qt Quick Core Programming是一本关于Qt Quick的技术书籍,主要介绍了Qt Quick的基础知识、技术和应用。书中介绍了Qt Quick的基本原理、基本概念、核心功能和实际应用,并结合代码实例,帮助读者更好地理解Qt Quick的核心概念和应用。…

    2023-03-11 11:23:40
    0 16 59
  • javascript权威指南 pdf从入门到精通

    示例示例权威指南 pdf是一本关于编程的书,由David 编写,旨在帮助读者深入理解的各个方面。它介绍了的基本语法,并讨论了如何使用它来创建动态Web页面。此外,它还提供了有关DOM,Ajax,JSON, HTML5,CSS3和其他最新Web技术的信息。以下是一个简单的代码示例:…

    2023-05-21 09:33:58
    0 74 44
  • Ye al:whatsapperroralenviarpdf "Fallo alcomparter"

    关于Ye al的问题,在como mandar una foto en pdf por whatsapp中经常遇到,Tengo una app proploa que envia un pdf al whatsapp,lo hacia todo bien,pero desde hace un tiempo el whatsapp (solo en esta app) me dice“Fallo al comparator……”y no lo puedo enviar.He actualizado la app y el gradle pero nada de nada…

    2023-11-19 15:27:30
    0 52 76
  • Gs认证和ce认证的区别:PDF的“认证”和“签名”之间的区别(what is a certified pdf)

    关于Gs认证和ce认证的区别的问题,在what is a certified pdf中经常遇到,我正在开发一个将签署 pdf 文档的脚本。我使用 tcpdf 和 tcpdi。我有.key 私钥,passphrase 和.pem 证书。我使用函数:…

    2023-11-12 03:36:43
    0 19 72
  • Caj转pdf的软件:SWF转 PDF批量转换

    关于Caj转pdf的软件的问题,在how to convert adobe flash player file to pdf中经常遇到,我有数百个 swf 格式的文件(这是一个电子书),我想将它们批量转换为 pdf(在我的 iPad 上阅读它们)。…

    2023-11-03 10:22:43
    0 60 12
  • 工程索赔程序:索赔835到PDF(835 file format converter)

    关于工程索赔程序的问题,在835 file format converter中经常遇到,我有一个案例,我收到一些.835 文件。我已经搜索了一点,但需要一个解决方案(python,c # 等)来获取这些文件并将它们导出为 pdf。有没有人遇到一个库或免费的命令行应用程序,因为我需要能够批量调用这个程序。…

    2023-10-20 02:08:48
    0 40 75
  • Decoded:PDF 1.6交叉引用流解码(decoded pdf)

    关于Decoded的问题,在decoded pdf中经常遇到,我试图复制显示的解决方案here,但没有运气。…

    2022-11-23 08:40:45
    0 46 86

发表评论

登录 后才能评论

评论列表(22条)