基于这个答案describing an item transform to convert image files from jpg to png,我进行了一个项转换,将.docx文件转换为.pdf。当我从我的projectname.proj构建文件调用它时,我会收到以下错误消息:
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]\.build\WordToPdf.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%\System32\WindowsPowerShell\v1.0\powershell.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.proj文件中从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%\System32\WindowsPowerShell\v1.0\powershell.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到pdf项目转换“的人,下面是我的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文件复制到其输出位置。
发布于 2013-04-26 13:05:37
您可以通过创建单独的目标来解决这一问题,该目标根据您的情况对项目进行预筛选--实际上,创建新的列表。
下面是示例-注意新的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>https://stackoverflow.com/questions/16202062
复制相似问题