0

I am using iText7 (C#) to create the pdf. I need to add a u3d picture to the exising pdf. I can find the example (http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-16#619-pdf3d.java) but it is java one. Can anyone help to give me an example on .net C#?

Michael Zhang
  • 47
  • 2
  • 6

5 Answers5

3

The Linked example is for iText5, not iText7. In iText7 this example would look like this

  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();
    }

Or, if you want it in C# (haven't ran it locally, but visual studio is not complaining about syntax)

    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();
    }
Samuel Huylebroeck
  • 1,639
  • 11
  • 15
  • I copied the code and gave a try, it generated the pdf with 3D framework (thanks your code Samuel), but no 3d image in it. I opened the model tree and can see the 3d info. Anything I missed. – Michael Zhang Jul 14 '17 at 01:10
  • BTW, there is no FileInputStream in .net so I change it to use {stream = new FileStream(u3dFile, FileMode.Open)} or {stream = File.Open(u3dFile, FileMode.Open)} or {stream = new StreamReader(u3dFile)} but no work. – Michael Zhang Jul 14 '17 at 01:21
  • Sorry my bad. I can see it now after I choose 'Fit visible' from Acrobat. Thanks a lot Samuel. I am wondering if I can set boarder to it? like iText5 SetBorder/SetBorderWidth/SetBorderColor. – Michael Zhang Jul 14 '17 at 01:44
  • If the answer helped you, please accept it so other people with the same problem know what to use. As for the border, best make a separate question out of it, as it's doable, but not easy, and I don't know it from the top of my head. – Samuel Huylebroeck Jul 14 '17 at 07:28
0

The iText codebase for .net was designed to be an (almost) exact mirror of the java one.

Apart from code conventions (such as method names starting with an uppercase), you should be able to use java code in .net.

This also explains why we typically do not post the code for .net. I suggest you simply copy/paste the java code, change the method names to account for code-convention, and attempt to compile it.

If anything, it will give you a code-sample you can post here to make you post a bit more sensible.

Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
  • thanks a lot and Samuel showed the code. Now it generates 3D in pdf but I can't see it when I open. I have to right click 3D image in Acrobat and choose "Part Options -> Fit Visible". If I am using example teapot.u3d it works (open then see). Is my u3d file too big (3 to 7 MB) but teapot.u3d only 141KB. If I remove this line: "annot.SetDefaultInitialView(dict3D);" I can open and see the image but the backgroud is black and image is dark. I am wondering the PdfDictionary Samuel shows in the code is not suitable for my case. Any idea about this PdfDictionary and how to set it? – Michael Zhang Jul 14 '17 at 04:10
0

This is the working code to print a u3d file into pdf using c#

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);

        }
Fatemeh Abdollahei
  • 3,040
  • 1
  • 20
  • 25
0

You can also try the below code in c# , which is using spirepdf

//Step 1: Initialize a new object of PdfDocuemnt, and add a blank page to the PDF document.

        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);
-2

Have your problem got resolved? If not, you might be also interested in this alternative solution - embed 3D interactive graphics into PDF Document using Spire.PDF in C#.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Rectangle rt = new Rectangle(0, 80, 200, 200); 
Pdf3DAnnotation annotation = new Pdf3DAnnotation(rt, "teapot.u3d");
annotation.Activation = new Pdf3DActivation();
annotation.Activation.ActivationMode = Pdf3DActivationMode.PageOpen; 
Pdf3DView View= new Pdf3DView();
View.Background = new Pdf3DBackground(new PdfRGBColor(Color.Purple));
View.ViewNodeName = "test";
View.RenderMode = new Pdf3DRendermode(Pdf3DRenderStyle.Solid);
View.InternalName = "test";
View.LightingScheme = new Pdf3DLighting();
View.LightingScheme.Style = Pdf3DLightingStyle.Day;
annotation.Views.Add(View);

page.AnnotationsWidget.Add(annotation);
doc.SaveToFile("Create3DPdf.pdf", FileFormat.PDF);
Kumar
  • 17
  • 1
  • The question was explicitly about doing the task "using itext7". Answering with code using a different library, therefore, is inappropriate. – mkl Jul 13 '17 at 14:18