在我的程序中,用户将输入作为副本数传递。
因此,如果用户传递的副本数为2,则我想生成单个pdf文档,且同一页面重复两次
我正在使用PdfSharp库
我有下面的代码可以创建具有多个页面的pdf文档
var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
page.Width = XUnit.FromMillimeter(111);
page.Height = XUnit.FromMillimeter(222);
//some logic to create multiple pages
document.Save("C:\mydoc\");
我确定我们需要添加循环,但是根据提供的输入,要在同一文档中重复相同的副本,我还需要做些什么
更新
假设文档生成2页,用户说副本数2,则单个pdf文档现在将具有4页,其中2页在同一文档中重复两次:
pagecontent1页面内容2,pagecontent1,pagecontent2
更新2
我尝试了@Christophers解决方案,并在保存文档之前更改了如下代码
PdfDocument outputDocument = new PdfDocument();
for (int i = 0; i < PageCopies; i++)
{
//the loop doing the copies
for (int k = 0; k < document.Pages.Count; k++)
{
outputDocument.Pages.Add(document.Pages[k]);
}
}
但是现在我收到错误消息必须使用pdfdocumentopenmode.import打开pdf文档以从中导入页面
答案 0 :(得分:2)
如果我理解正确,我认为您可以保存原始的完整PDF,然后将其打开并合并多次。
如果是这样,可能会很接近。我已经将您的代码与pdfsharp中的一些文档结合在一起了:http://www.pdfsharp.net/wiki/ConcatenateDocuments-sample.ashx
var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
page.Width = XUnit.FromMillimeter(111);
page.Height = XUnit.FromMillimeter(222);
//some logic to create multiple pages
document.Save("OriginalDoc.pdf");
//Logic here to open and merge the document multiple times
PdfDocument outputDocument = new PdfDocument();
// Open the document to import pages from it.
PdfDocument inputDocument = PdfReader.Open("C:\mydoc\OriginalDoc.pdf", PdfDocumentOpenMode.Import);
for(int i = 0; i < Copies; i++)
{
// Iterate pages
int count = inputDocument.PageCount;
for (int idx = 0; idx < count; idx++)
{
// Get the page from the external document...
PdfPage page = inputDocument.Pages[idx];
// ...and add it to the output document.
outputDocument.AddPage(page);
}
}
// Save the document...
const string filename = "MergedDoc.pdf";
outputDocument.Save(filename);
我怀疑无需保存并重新打开文档即可完成此操作,但我本人并未使用pdfsharp。
答案 1 :(得分:1)
好吧,事实证明这个问题与我最初理解的完全不同。由于我尤其不熟悉PDFSharp,因此我只能提供一般的循环结构。
//I use a array to simulate the input PDF
int[] input = new int[]{ 1, 2 };
//And a list to simulate the output PDF
List<int> output = new List<int>();
//PageCopies is set somewhere outside this code.
//The loop counting the copies
for(int i = 0; i < PageCopies; i++){
//the loop doing the copies
for(int k = 0; k < input.Count; k++){
output.Add(input[k]);
}
}
免责声明:代码未针对编译器运行。语法和错误保留一个错误
答案 2 :(得分:0)
最后,由于@Christopher和@Blaise的线索,我设法解决了自己,但偏离了路线。这是我的最终代码
var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
page.Width = XUnit.FromMillimeter(111);
page.Height = XUnit.FromMillimeter(222);
//some logic to create multiple pages
document.Save("C:\mydoc\");
PdfDocument finalOutputDocument;
if (CopyCount >1)
{
MemoryStream stream = new MemoryStream();
document.Save(stream, false);
PdfDocument source = PdfReader.Open(stream, PdfDocumentOpenMode.Import);
finalOutputDocument= new PdfDocument();
for (int i = 0; i < CopyCount; i++)
{
for (int k = 0; k < document.Pages.Count; k++)
{
finalOutputDocument.Pages.Add(source.Pages[k]);
}
}
}
else
{
finalOutputDocument= document;
}
finalOutputDocument.Save(path);