基于描述将图像文件从jpg转换为png的项转换的答案,我进行了将。docx文件转换为。pdf的项转换。当我从项目名中调用它时。我得到这个错误信息:
Error 1 The condition " '%(Extension)' == '.docx' " on the
"WordToPdf" target has a reference to item metadata. References to item
metadata are not allowed in target conditions unless they are part of an item
transform. [project path].buildWordToPdf.Tasks.target 7 9
我怎样才能使它工作?
这是我的目标:
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- $Id$ -->
<Target Name="WordToPdf"
Inputs="@(Content)"
Outputs="@(Content -> '%(RootDir)%(Directory)%(Filename).pdf' )"
Condition=" '%(Extension)' == '.docx' ">
<ItemGroup>
<Sublist Include="@(Content)" Condition=" '%(Extension)' == '.docx' " />
</ItemGroup>
<PropertyGroup>
<PowerShellExe Condition=" '$(PowerShellExe)'=='' ">%WINDIR%System32WindowsPowerShellv1.0powershell.exe</PowerShellExe>
<ScriptLocation Condition=" '$(ScriptLocation)'=='' ">.WordToPdf.ps1</ScriptLocation>
</PropertyGroup>
<Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted -command "& {&'$(ScriptLocation)' -WordFilename '/Input:%(Sublist.FullPath)' }"" />
<Content Remove="@(Sublist)" />
<Content Include="@(Sublist -> '%(RootDir)%(Directory)%(Filename).pdf' )" />
</Target>
</Project>
从我的projectname调用它。项目文件从BeforeBuild目标,如下所示:
<Import Project="$(MSBuildTasksPath)WordToPdf.Tasks.target" />
....
<Target Name="BeforeBuild">
<CallTarget Targets="WordToPdf" />
</Target>
除了让转换工作之外,这个目标还有更多的错误。为了将来参考,下面是最终的工作代码:
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- $Id$ -->
<Target Name="WordToPdf"
Inputs="@(ContentFiltered)"
Outputs="@(ContentFiltered -> '%(RootDir)%(Directory)%(Filename).pdf' )"
DependsOnTargets="FilterContent">
<PropertyGroup>
<PowerShellExe Condition=" '$(PowerShellExe)'=='' ">%WINDIR%System32WindowsPowerShellv1.0powershell.exe</PowerShellExe>
<ScriptLocation Condition=" '$(ScriptLocation)'=='' ">.WordToPdf.ps1</ScriptLocation>
</PropertyGroup>
<Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted -command "& {&'$(ScriptLocation)' -WordFilename '%(ContentFiltered.FullPath)' }"" />
<ItemGroup>
<Content Remove="@(ContentFiltered)" />
<Content Include="@(ContentFiltered -> '%(RootDir)%(Directory)%(Filename).pdf' )" />
</ItemGroup>
</Target>
<!-- New target to pre-filter list -->
<Target Name="FilterContent">
<ItemGroup>
<ContentFiltered Include="@(Content)" Condition="'%(Extension)' == '.docx'" />
</ItemGroup>
</Target>
</Project>
并且,对于任何在这里搜索"msbuild word to pdf item transform"的人下面是powershell脚本:
Param(
[string]$WordFilename
)
$Word = New-Object -comobject Word.Application
$Doc=$Word.Documents.Open($WordFilename)
$Doc.saveas([ref](($WordFilename).replace("docx","pdf")), [ref]17)
$Doc.close()
将。docx文件添加到您的项目中,作为内容-复制较新的文件,如果docx文件较新,则将创建pdf文件,然后将pdf文件复制到其输出位置
您可以通过创建单独的目标来解决这个问题,该目标根据您的条件预过滤项目-实际上是创建新列表。
下面是示例-注意新的DependsOnTargets
标签和更改的ContentFiltered
名称:
<Target Name="WordToPdf"
Inputs="@(ContentFiltered)"
Outputs="@(ContentFiltered -> '%(RootDir)%(Directory)%(Filename).pdf' )"
DependsOnTargets="FilterContent">
<!--
...
your actual target body here
...
-->
</Target>
<!-- New target to pre-filter list -->
<Target Name="FilterContent">
<ItemGroup>
<ContentFiltered Include="@(Content)" Condition="'%(Extension)' == '.docx'" />
</ItemGroup>
</Target>