gpt4 book ai didi

c# - 替换 PDF 中的特定文档

转载 作者:太空宇宙 更新时间:2023-11-03 19:54:56 24 4
gpt4 key购买 nike

包括:

using Ghostscript.NET;
using Ghostscript.NET.Processor;
using Ghostscript.NET.Rasterizer;

现在,我正在使用 Ghostscript.Net 将几个单独的 PDF 合并到一个文档中:

/// <summary>
/// Ghostscripts the file specified in parameter 1 as a PDF to the file specified in parameter 2
/// </summary>
/// <param name="fileNames">String[]. Array of Full Paths to a file to convert to a single PDF</param>
/// <param name="outputPath">String. Full Path to where Ghostscript will write the PDF</param>
public static void GhostscriptNetJoin(String[] fileNames, String outputPath)
{
var sb = new StringBuilder();
foreach (var fileName in fileNames)
{
var source = (fileName.IndexOf(' ') == -1) ? fileName : String.Format("\"{0}\"", fileName);
sb.Append(source + " ");
}
var output_file = (outputPath.IndexOf(' ') == -1) ? outputPath : String.Format("\"{0}\"", outputPath);
var gsArgs = new List<String>();
gsArgs.Add("-empty"); // first argument is ignored. REF: http://stackoverflow.com/q/25202577/153923
gsArgs.Add("-dBATCH");
gsArgs.Add("-q");
gsArgs.Add("-dNOPAUSE");
gsArgs.Add("-dNOPROMPT");
gsArgs.Add("-sDEVICE=pdfwrite");
gsArgs.Add("-dPDFSETTINGS=/prepress");
gsArgs.Add(String.Format(@"-sOutputFile={0}", output_file));
gsArgs.Add(sb.ToString());
var version = GhostscriptVersionInfo.GetLastInstalledVersion();
using (var processor = new GhostscriptProcessor(version, false))
{
processor.Process(gsArgs.ToArray());
}
}

我怎样才能稍后返回REPLACE or UPDATE第N页?

我已经草拟了一个有我计划的例程,但此时我不知道如何完成它。我可以提供 arg 值还是我应该使用其他工具?

/// <summary>
/// Replace Specific Document from source PDF file
/// </summary>
/// <param name="source">String. Full path to the multi-page PDF</param>
/// <param name="documentN">String. Full path to the document to insert</param>
/// <param name="indexN">int. Page Index where the new document should be inserted</param>
public static void GhostscriptNetReplace(String source, String documentN, int indexN)
{
var list = new List<String>();
var version = GhostscriptVersionInfo.GetLastInstalledVersion();
using (var processor = new GhostscriptProcessor(version, false))
{
var gsArgs = new List<String>();
// what arguments are needed?
throw new NotImplementedException("I don't know how to code for this yet.");
processor.Process(gsArgs.ToArray());
}
list.RemoveAt(indexN);
list.Insert(indexN, documentN);
var sb = new StringBuilder();
foreach (var fileName in list)
{
var fmtSource = (fileName.IndexOf(' ') == -1) ? fileName : String.Format("\"{0}\"", fileName);
sb.Append(fmtSource + " ");
}
var output_file = (source.IndexOf(' ') == -1) ? source : String.Format("\"{0}\"", source);
using (var processor = new GhostscriptProcessor(version, false))
{
var gsArgs = new List<String>();
gsArgs.Add("-empty"); // first argument is ignored. REF: http://stackoverflow.com/q/25202577/153923
gsArgs.Add("-dBATCH");
gsArgs.Add("-q");
gsArgs.Add("-dNOPAUSE");
gsArgs.Add("-dNOPROMPT");
gsArgs.Add("-sDEVICE=pdfwrite");
gsArgs.Add("-dPDFSETTINGS=/prepress");
gsArgs.Add(String.Format(@"-sOutputFile={0}", output_file));
gsArgs.Add(sb.ToString());
processor.Process(gsArgs.ToArray());
}
}

最佳答案

你也许可以做这样的事情(现在无法测试代码,但它的原理是基于 Ghostscript.NET repo 检查出来的):

var prcPath = "PATH"; //a path to store the temporary files
var pageCount = GetPDFPageCount(source);
var list = SplitPDFatIndex(source, prcPath, indexN);

private static List<String> SplitPDFatIndex(String pathToFile, String tempPath, int index)
{
var outList = new List<String>();
outList.Add(SlicePDFatIndex(pathToFile, tempPath, index, true);
outlist.Add(null); // Alternatively modify method below to permit pulling page N
outList.Add(SlicePDFatIndex(pathToFile, tempPath, index, false);

return outList;
}

private static String SlicePDFatIndex(String pathToFile, String tempPath, int index, bool lessThanIndex)
{
using (var processor = new GhostscriptProcessor(version, false))
{
var pageFrom = 1;
var pageTo = index - 1;
var name = tempPath + "temp_left.pdf";

if (!lessThanIndex)
{
pageFrom = index + 1;
pageTo = pageCount;
name = tempPath + "temp_right.pdf";
}

var gsArgs = new List<String>();
gsArgs.Add("-empty");
gsArgs.Add("-dBATCH");
gsArgs.Add("-q");
gsArgs.Add("-dNOPAUSE");
gsArgs.Add("-dNOPROMPT");
gsArgs.Add("-sDEVICE=pdfwrite");
gsArgs.Add("-dPDFSETTINGS=/prepress");
gsArgs.Add(String.Format(@"-f{0}", pathToFile);
gsArgs.Add("-dFirstPage=" + pageFrom.ToString());
gsArgs.Add("-dLastPage=" + pageTo.ToString());
gsArgs.Add(String.Format(@"-sOutputFile={0}", name));
processor.Process(@"-f{0}", pathToFile);

return name;
}

private static int GetPDFPageCount(String pathToFile)
{
var count;
var GhostscriptViewer viewer;

viewer = new GhostscriptViewer();
viewer.ShowPageAfterOpen = false;
viewer.ProgressiveUpdate = false;
viewer.Open(source); // try (source, version, false) or (source, version, true) if for some reason it hangs up here
count = viewer.LastPageNumber;
viewer.Close()

return count;
}

关于c# - 替换 PDF 中的特定文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34748511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com