1

根据这个描述将图像文件从 jpg 转换为 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 &quot;&amp; {&amp;&apos;$(ScriptLocation)&apos; -WordFilename &apos;/Input:%(Sublist.FullPath)&apos; }&quot;" />

        <Content Remove="@(Sublist)" />
        <Content Include="@(Sublist -> '%(RootDir)%(Directory)%(Filename).pdf' )" />
      </Target>
    </Project>

并从 BeforeBuild 目标的我的 projectname.proj 文件中调用它,如下所示:

  <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 &quot;&amp; {&amp;&apos;$(ScriptLocation)&apos; -WordFilename &apos;%(ContentFiltered.FullPath)&apos; }&quot;" />

    <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 文件复制到其输出位置。

4

1 回答 1

1

您可以通过创建单独的目标来解决它,该目标根据您的条件预先过滤项目 - 实际上,创建新列表。

这是示例 - 注意新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>
于 2013-04-26T13:05:37.140 回答