C#-Pdftk-我如何才能让pdf的页数为奇数时,添加一个空白页



我想用Pdftk将许多pdf合并为一个。

我该如何使pdf的页数为奇数时,就会添加一个空白页?

我需要这个函数作为一个cmd工具,我需要用c#编写它。

PDF应双面打印。

using System.Diagnostics;
using System.IO;
using MergePdf.Properties;
using System.Linq;
using System.Collections;
using System.Runtime.InteropServices;
using System.Collections.Generic;
namespace System.Configuration
{
class Program
{
static void Main(string[] args)
{
Console.Title = "MergePDF";
// Folder from which the PDF will be pulled
string altDirIn = Settings.Default.AlternativeInputDir;
// Folder in which the merged PDF will be placed
string altDirOut = Settings.Default.AlternativeOutputDir;
// sort the files by the first name before the "_"
var rchg = new DirectoryInfo(altDirIn);
var files1 = rchg.EnumerateFiles("*.pdf").OrderBy(fi => Convert.ToInt32(fi.Name.Split('_')[0]));
var cmd1 = $"{string.Join(" ", files1.Select(fi => fi.FullName))} cat output {altDirOut}";

var evn = new DirectoryInfo(altDirIn);
var files2 = evn.EnumerateFiles("*.pdf").OrderBy(fi => Convert.ToInt32(fi.Name.Split('_')[0]));
var cmd2 = $"{string.Join(" ", files2.Select(fi => fi.FullName))} cat output {altDirOut}";
Process p = new Process();
p.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
p.StartInfo.FileName = "pdftk.exe";
p.StartInfo.Arguments = cmd1;
p.StartInfo.Arguments = cmd2;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
Console.WriteLine(p.StandardError.ReadToEnd());
Console.WriteLine("Bitte drücken Sie eine beliebige Taste, um das Programm zu beenden...");
Console.ReadKey();
p.WaitForExit();
}
}
}

当我这样做的时候,我会遇到这样的问题,例如,如果一个pdf的页数是奇数,那么在第一个pdf的最后一页的背面就是下一个pdf。

我该如何继续,以便每个pdf都有偶数页?

提前向致以最良好的问候和感谢

如果您可以shell到pdftk,则可以shell到cpdf并运行:

cpdf -pad-multiple x in.pdf -o out.pdf

其中,x是文件中的页数(如果为偶数(,或者是文件中页数加一(如果为奇数(。

您可以使用获取页数

cpdf -pages foo.pdf

然后可以合并生成的文件。

最新更新