我使用iText7 (C#)来创建pdf。我需要添加一个u3d图片到现有的pdf。我可以找到示例(http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-16#619-pdf3d.java),但它是java示例。谁能帮我举一个关于.net C#的例子?
发布于 2017-07-12 23:07:01
链接的示例是针对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();
}发布于 2017-07-12 22:28:55
.net的iText代码库被设计成(几乎) java代码库的镜像。
除了代码约定(比如方法名以大写字母开头),您应该能够在.net中使用java代码。
这也解释了为什么我们通常不发布.net的代码。我建议您只需复制/粘贴java代码,更改方法名称以适应代码约定,然后尝试编译它。
如果有什么不同的话,它会给你一个代码样本,你可以在这里张贴,让你的帖子更明智一些。
发布于 2019-05-22 14:58:56
这是使用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);
}https://stackoverflow.com/questions/45059957
复制相似问题