我阅读了一些与构建文档相关的文档,因此我使用构建块构建我的应用程序,其中每个块都是一个组件(标题,图像......)。
因此,当我构建我的doc结构时,我有类似的东西:
DocRun accessTypeTitle = new DocRun();
Run permissionTitle = accessTypeTitle.createParagraph("DOCUMENT ACCESS", "0475B9","16");
// document permission (ex: public, internal use only), defined on template side
DocRun accessTypePermission = new DocRun();
Run permission = accessTypePermission.createParagraph(document.documentAccess, "262626", "16");
DocRun disclaimerTitle = new DocRun();
Run disclaimerTitleTxt = disclaimerTitle.createParagraph("DISCLAIMER", "0475B9", "16");
DocRun disclaimerDescription = new DocRun();
Run disclaimerDescriptionTxt = disclaimerDescription.createParagraph(disclaimerTxt, "262626", "16");
var stream = new MemoryStream();
using (WordprocessingDocument doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true))
{
MainDocumentPart mainPart = doc.AddMainDocumentPart();
new Document(new Body()).Save(mainPart);
Body body = mainPart.Document.Body;
/*body.Append(new Paragraph(
new Run(image)));*/
body.Append(new Paragraph(
new Run(permissionTitle)));
body.Append(new Paragraph(
new Run(permission)));
body.Append(new Paragraph(
new Run(disclaimerTitleTxt)));
body.Append(new Paragraph(
new Run(disclaimerDescriptionTxt)));
mainPart.Document.Save();
}
我有一个处理这些功能的外部dll,我需要为我的图像提供相同的功能。
基本上,图像应该是文档中添加的第一件事,我看到一些文档,他们访问uri并将图像保存在路径上。我只需要访问我的图像文件夹获取图像并将其添加到文档中。
目前我有这个:
DocImage docImage = new DocImage();
Drawing image = docImage.addImage("2", "https://pbs.twimg.com/profile_images/465786286825955328/JIN8xseJ_400x400.png");
namespace openXml
{
public class DocImage
{
public Drawing addImage(String relationshipId,String uri)
{
// Define the reference of the image.
var element =
new Drawing(
new DW.Inline(
new DW.Extent() { Cx = 990000L, Cy = 792000L },
new DW.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
},
new DW.DocProperties()
{
Id = (UInt32Value)1U,
Name = "Picture 1"
},
new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks() { NoChangeAspect = true }),
new A.Graphic(
new A.GraphicData(
new PIC.Picture(
new PIC.NonVisualPictureProperties(
new PIC.NonVisualDrawingProperties()
{
Id = (UInt32Value)0U,
Name = "CriticalLogo"
},
new PIC.NonVisualPictureDrawingProperties()),
new PIC.BlipFill(
new A.Blip(
new A.BlipExtensionList(
new A.BlipExtension()
{
Uri =
"{28A0092B-C50C-407E-A947-70E740481C1C}"
})
)
{
Embed = relationshipId,
CompressionState =
A.BlipCompressionValues.Print
},
new A.Stretch(
new A.FillRectangle())),
new PIC.ShapeProperties(
new A.Transform2D(
new A.Offset() { X = 0L, Y = 0L },
new A.Extents() { Cx = 990000L, Cy = 792000L }),
new A.PresetGeometry(
new A.AdjustValueList()
)
{ Preset = A.ShapeTypeValues.Rectangle }))
)
{ Uri = uri })
)
{
DistanceFromTop = (UInt32Value)0U,
DistanceFromBottom = (UInt32Value)0U,
DistanceFromLeft = (UInt32Value)0U,
DistanceFromRight = (UInt32Value)0U
});
return element;
}
}
}
var stream = new MemoryStream();
using (WordprocessingDocument doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true))
{
MainDocumentPart mainPart = doc.AddMainDocumentPart();
new Document(new Body()).Save(mainPart);
Body body = mainPart.Document.Body;
body.Append(new Paragraph(
new Run(image)));
}
文档已创建但已损坏:/