如何使用C#按文件大小拆分PDF文件?
我发现了一个教程,演示了如何使用Adobe Acrobat按页面或最大文件大小将PDF文件拆分为单独的PDF文件: 拆分文档>按文件大小拆分文档“> 我在StackOverflow上讨论了如何使用C#按页面分割PDF。但如何使用C#按最大文件大小将PDF文件分割为多个PDF文件 例如,假设我有一个70页40 MB的PDF文件,如何使用C#将该文件拆分为大约5个不超过10 MB的PDF文件,而不是拆分为每个10页的7个PDF文件如何使用C#按文件大小拆分PDF文件?,c#,pdf,acrobat,C#,Pdf,Acrobat,我发现了一个教程,演示了如何使用Adobe Acrobat按页面或最大文件大小将PDF文件拆分为单独的PDF文件: 拆分文档>按文件大小拆分文档“> 我在StackOverflow上讨论了如何使用C#按页面分割PDF。但如何使用C#按最大文件大小将PDF文件分割为多个PDF文件 例如,假设我有一个70页40 MB的PDF文件,如何使用C#将该文件拆分为大约5个不超过10 MB的PDF文件,而不是拆分为每个10页的7个PDF文件 到目前为止,我所见过的最好的方法是使用where按页面分割文件,然后
到目前为止,我所见过的最好的方法是使用where按页面分割文件,然后按大小对这些页面文件进行分组。但是,不必先按页面分割,这是一种更直接的方法吗?这是一个未经测试的示例代码,假设您准备在纯二进制级别进行分割,即PDF R不会读取这些部分eader,您必须重新连接部件以使其可读: 下面的代码首先获取字节[]数组中的pdf文件。然后根据任意分区大小(本例中为5),获取每个部件二进制文件的文件大小。然后,它将创建一个临时内存流,并循环通过以创建每个分区并写入一个新的.part文件。 (您可能需要进行一些更改以使其可行)
byte[]pdfBytes=File.ReadAllBytes(“c:\foo.pdf”);
int fileSize=pdfBytes.Length/5;//假设foo是40MB,那么文件大小将是8MB
MemoryStream m=新的MemoryStream(pdfBytes);
对于(int i=0;i<4;i++)
{
byte[]tbytes=新字节[文件大小];
m、 读取(tbytes,i*文件大小,文件大小);
文件.writealBytes(“C:\foo”+i+“.part”,t字节);
}
这是一个未经测试的示例代码,假设您准备在纯二进制级别进行拆分,即PDF阅读器不会读取这些部分,您必须重新连接这些部分以使其可读:
下面的代码首先获取字节[]数组中的pdf文件。然后根据任意分区大小(本例中为5),获取每个部件二进制文件的文件大小。然后,它将创建一个临时内存流,并循环通过以创建每个分区并写入一个新的.part文件。
(您可能需要进行一些更改以使其可行)
byte[]pdfBytes=File.ReadAllBytes(“c:\foo.pdf”);
int fileSize=pdfBytes.Length/5;//假设foo是40MB,那么文件大小将是8MB
MemoryStream m=新的MemoryStream(pdfBytes);
对于(int i=0;i<4;i++)
{
byte[]tbytes=新字节[文件大小];
m、 读取(tbytes,i*文件大小,文件大小);
文件.writealBytes(“C:\foo”+i+“.part”,t字节);
}
从开始,我编写了以下SplitBySize方法:
public static void SplitBySize(string filename, long limit)
{
PdfDocument input = PdfReader.Open(filename, PdfDocumentOpenMode.Import);
PdfDocument output = CreateDocument(input);
string name = Path.GetFileNameWithoutExtension(filename);
string temp = string.Format("{0} - {1}.pdf", name, 0);
int j = 1;
for (int i = 0; i < input.PageCount; i++)
{
PdfPage page = input.Pages[i];
output.AddPage(page);
output.Save(temp);
FileInfo info = new FileInfo(temp);
if (info.Length <= limit)
{
string path = string.Format("{0} - {1}.pdf", name, j);
if (File.Exists(path))
{
File.Delete(path);
}
File.Move(temp, path);
}
else
{
if (output.PageCount > 1)
{
output = CreateDocument(input);
++j;
--i;
}
else
{
throw new Exception(
string.Format("Page #{0} is greater than the document size limit of {1} MB (size = {2})",
i + 1,
limit / 1E6,
info.Length));
}
}
}
}
publicstaticvoidsplitbysize(字符串文件名,长度限制)
{
PdfDocument input=PdfReader.Open(文件名,PdfDocumentOpenMode.Import);
PdfDocument输出=创建文档(输入);
string name=Path.GetFileNameWithoutExtension(文件名);
string temp=string.Format(“{0}-{1}.pdf”,名称,0);
int j=1;
对于(int i=0;i
我将继续测试,但到目前为止它仍在工作。从开始,我编写了以下SplitBySize方法:
public static void SplitBySize(string filename, long limit)
{
PdfDocument input = PdfReader.Open(filename, PdfDocumentOpenMode.Import);
PdfDocument output = CreateDocument(input);
string name = Path.GetFileNameWithoutExtension(filename);
string temp = string.Format("{0} - {1}.pdf", name, 0);
int j = 1;
for (int i = 0; i < input.PageCount; i++)
{
PdfPage page = input.Pages[i];
output.AddPage(page);
output.Save(temp);
FileInfo info = new FileInfo(temp);
if (info.Length <= limit)
{
string path = string.Format("{0} - {1}.pdf", name, j);
if (File.Exists(path))
{
File.Delete(path);
}
File.Move(temp, path);
}
else
{
if (output.PageCount > 1)
{
output = CreateDocument(input);
++j;
--i;
}
else
{
throw new Exception(
string.Format("Page #{0} is greater than the document size limit of {1} MB (size = {2})",
i + 1,
limit / 1E6,
info.Length));
}
}
}
}
publicstaticvoidsplitbysize(字符串文件名,长度限制)
{
PdfDocument input=PdfReader.Open(文件名,PdfDocumentOpenMode.Import);
PdfDocument输出=创建文档(输入);
string name=Path.GetFileNameWithoutExtension(文件名);
string temp=string.Format(“{0}-{1}.pdf”,名称,0);
int j=1;
对于(int i=0;i
我将继续测试,但到目前为止它仍在工作。谢谢你的回答,但我确实需要生成的文件为PDF格式。我已经更新了我的问题,使其更具体。在这种情况下,也许你可以查看PDFSharp,一个开放源代码库,允许你通过.NET处理PDF文件:谢谢你的回答,但我确实需要生成的文件这些文件是PDF格式的。我已经更新了我的问题,使之更加具体。在这种情况下,也许你可以查看PDFSharp,一个开放源代码库,它允许你通过.NET处理PDF文件:CreateDocument(input)呢语句?你在那个函数里面有什么?你能把完整的代码放进去吗…很有趣..想试试代码。CreateDocument(input)语句呢?你在那个函数里面有什么?你能把完整的代码放进去吗…很有趣..想试试代码。