使用PDFsharp合并两个PDF文件时出错 [英] Error merging two PDF files using PDFsharp
问题描述
合并两个文件时出现问题.如果我尝试将AddPage(from.Pages[i]);
放在单独的void函数中,则会得到
非静态字段,方法或属性需要对象引用 它与
CopyPages(one, outPdf); CopyPages(two, outPdf);
如果我将其设置为静态void,它将运行,但控制台显示错误,指出无法保存没有页面的PDF"
static void Main(string[] args)
{
PdfDocument one = new PdfDocument("1.pdf");
PdfDocument two = new PdfDocument("2.pdf");
PdfDocument outPdf = new PdfDocument();
{
CopyPages(one, outPdf);
CopyPages(two, outPdf);
outPdf.Save(out.pdf);
}
}
void CopyPages(PdfDocument from, PdfDocument to)
{
for (int i = 0; i < from.PageCount; i++)
{
to.AddPage(from.Pages[i]);
}
}
您的one
是一个空的PdfDocument,您的two
是一个空的PdfDocument,for循环不执行任何操作,而outPdf是一个空的PdfDocument.>
与往常一样,计算机会执行您告诉他的操作.如果您在调试器中单步执行代码,则可以轻松看到这一点.
您必须使用PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);
之类的文件来打开要导入的PDF文件.
另请参阅:
http://www.pdfsharp.net/wiki/ConcatenateDocuments-sample.ashx
new PdfDocument("1.pdf");
不会打开/读取文件,它只是准备创建具有该名称的新文件.
I am getting a problem when merging two files. If I try to have the AddPage(from.Pages[i]);
in a separate void function I get
An object reference is required for the non-static field, method, or property It relates to
CopyPages(one, outPdf); CopyPages(two, outPdf);
If I make it a static void it will run but the console displays an error stating that it "can not save a PDF with no pages"
static void Main(string[] args)
{
PdfDocument one = new PdfDocument("1.pdf");
PdfDocument two = new PdfDocument("2.pdf");
PdfDocument outPdf = new PdfDocument();
{
CopyPages(one, outPdf);
CopyPages(two, outPdf);
outPdf.Save(out.pdf);
}
}
void CopyPages(PdfDocument from, PdfDocument to)
{
for (int i = 0; i < from.PageCount; i++)
{
to.AddPage(from.Pages[i]);
}
}
Your one
is an empty PdfDocument, your two
is an empty PdfDocument, the for loop does nothing, and outPdf is an empty PdfDocument.
As always, the computer does what you tell him to do. You can easily see that if you step through your code in a Debugger.
You have to use something like PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);
to open a PDF file for import.
See also:
http://www.pdfsharp.net/wiki/ConcatenateDocuments-sample.ashx
new PdfDocument("1.pdf");
does not open/read a file, it just prepares the creation of a new file with that name.
这篇关于使用PDFsharp合并两个PDF文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!