C# iTextSharp在导入PDF时在页面顶部添加白色块
我在使用C# iTextSharp在导入PDF时在页面顶部添加白色块,c#,pdf,itextsharp,C#,Pdf,Itextsharp,我在使用iTextSharp和C#时遇到了一个小问题 背景: 我下载PDF并将它们合并成一个大文件 问题: 在每一页上,前几厘米都是白色的,我导入的pdf在白色块之后开始 每一页的结尾都是正确的。没有重叠或丢失的对象/文本-您可以假设这是因为它必须处理更少的空间。我想它可能会垂直拉伸 因此,导入工作正常,但它总是在每页的顶部添加一些白色的中间点。 这感觉像是一个最高利润。但我似乎无法修复它 有什么想法吗 我感谢你的帮助。非常感谢 public void method() { // ne
iTextSharp
和C#时遇到了一个小问题
背景:
我下载PDF并将它们合并成一个大文件
问题:
在每一页上,前几厘米都是白色的,我导入的pdf在白色块之后开始
每一页的结尾都是正确的。没有重叠或丢失的对象/文本-您可以假设这是因为它必须处理更少的空间。我想它可能会垂直拉伸
因此,导入工作正常,但它总是在每页的顶部添加一些白色的中间点。
这感觉像是一个最高利润。但我似乎无法修复它
有什么想法吗
我感谢你的帮助。非常感谢
public void method()
{
// needed variables for the pdf-merging part
fs = new FileStream(Variables.destinationFile, FileMode.Create);
writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
doc.SetPageSize(PageSize.A4);
doc.SetMargins(0f, 0f, 0f, 0f);
pdfContent = writer.DirectContent;
byte[] result;
int numPages;
foreach (Tuple<string, string, int> currentTuple in someArray)
try
{
result = client.DownloadData(new Uri(adress + currentTuple.Item1 + ".pdf"));
// read and add the pages to the output file
reader = new PdfReader(result);
numPages = reader.NumberOfPages;
for (int i = 1; i < numPages + 1; i++)
{
doc.NewPage();
page = writer.GetImportedPage(reader, i);
pdfContent.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
catch (Exception e)
{
}
}
doc.Close();
writer.Close();
fs.Close();
}
public void方法()
{
//pdf合并部分所需的变量
fs=新文件流(Variables.destinationFile,FileMode.Create);
writer=PdfWriter.GetInstance(doc,fs);
doc.Open();
文件设置页面大小(页面大小A4);
单据设置页边距(0f、0f、0f、0f);
pdfContent=writer.DirectContent;
字节[]结果;
国际货币单位;
foreach(数组中的元组currentTuple)
尝试
{
结果=client.DownloadData(新Uri(address+currentuple.Item1+“.pdf”);
//读取页面并将其添加到输出文件中
读卡器=新的PDF读卡器(结果);
numPages=reader.NumberOfPages;
对于(int i=1;i
p、 为什么它总是删除我的“你好”?:) 您使用了错误的方法来合并文档。您的方法丢弃了所有交互性,并且不考虑页面大小(这解释了您报告的问题)。请告诉我你是从哪里得到这样合并文档的灵感的,这样我就可以去打你使用的例子的负责人;-) 中介绍了连接文档的正确方法 您可以在此处找到更多示例:
PdfCopy
),而不是使用错误的方式(使用PdfWriter
和AddTemplate()
)
在您的评论中,您说方法AddPage()
在PdfCopy
中不存在。让我们来看一下该类的最新版本:
我清楚地看到:
/**
* Add an imported page to our output
* @param iPage an imported page
* @throws IOException, BadPdfFormatException
*/
public virtual void AddPage(PdfImportedPage iPage) {
请注意,最新版本还有一个AddDocument()
方法:
virtual public void AddDocument(PdfReader reader) {
使用此方法,您不再需要在所有页面上循环,但可以一次添加由PdfReader
读取的PDF的所有页面
如果只想添加选定的页面,可以使用:
virtual public void AddDocument(PdfReader reader, List<int> pagesToKeep) {
virtualpublicsvoidadddocument(PdfReader阅读器,列表页){
请不要使用非官方版本!官方版本可在此处下载:
iText Group对iTextSharp的旧版本不承担任何责任,我们也不能对我们软件的分叉负责。对于您的“p.s:”谢谢您的帮助!他们似乎都使用了PdfCopy.AddPage(第页);但在我的情况下,该方法从未找到。它不存在。我不知道我使用的是什么版本。但它是最新的版本之一。最多1个月。好的。第8层问题。感谢您的帮助。修复了它。在谷歌搜索时,我发现大多数使用PdfWriter:/