合并两个pdf文件 [英] Merging two pdf documents

查看:121
本文介绍了合并两个pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的Web应用程序中的功能具有将两个pdf文档合并到另一个新文档(由两个pdf文件合并)的功能.

iam以任何方式在此处合并两个文件(results.pdf文件),一个pdf文件将打印所有结果(有什么内容),另一个文件将打印但无法打印一些结果

所以,让我知道我错了或向我发布如何合并两个pdf文档(文件)

//这是我的代码是

Hi all,

in my web application have functionality merging two pdf documents to another new document(combined of two pdf files).

any way iam merging two files here (results.pdf file)one pdf file will be print all results(what are content are there) another file printing but unable to print some results

So,let me know wre i wrong or post me how to merging tow pdf documents(files)

//here is my code is

class Program
   {
       //Static variables used throughout the program
       //The folder that we''ll be saving things to
       static string WorkingFolder = Environment.GetFolderPath ( Environment.SpecialFolder.Desktop );
       //The samples images that we''ll create and work with
       static string Img1 = Path.Combine ( WorkingFolder, "1st.pdf" );
       static string Img2 = Path.Combine ( WorkingFolder, "2nd.pdf" );
       //These two arrays are just used to create our sample images
       static Color[] colors = new Color[] { Color.RED, Color.BLUE };
       static string[] files = new string[] { Img1, Img2 };
       //Our main export file
       static string MergedFile = Path.Combine ( WorkingFolder, "Merged.pdf" );

       static void Main ( string[] args )
       {
           MergeDocuments ( );
       }
       public static void MergeDocuments ( )
       {
           //Create our PDF readers
           PdfReader r1 = new PdfReader ( Img1 );
           PdfReader r2 = new PdfReader ( Img2 );

           //Our new page size, an A3 in landscape mode
           iTextSharp.text.Rectangle NewPageSize = PageSize.A3.Rotate ( );
           using (FileStream fs = new FileStream ( MergedFile, FileMode.Append, FileAccess.Write, FileShare.None ))
           {                 //Create our document without margins
               //using (Document doc = new Document ( NewPageSize, 0, 0, 0, 0 ))
               //{
               Document doc = new Document ( NewPageSize, 0, 0, 0, 0 );
               if (doc != null)
               {
                   //using (PdfWriter w = PdfWriter.GetInstance ( doc, fs ))
                   PdfWriter w = PdfWriter.GetInstance ( doc, fs );
                   if (w != null)
                   {
                       doc.Open ( );
                       //Get our imported pages
                       PdfImportedPage imp1 = w.GetImportedPage ( r1, 1 );
                       PdfImportedPage imp2 = w.GetImportedPage ( r2, 1 );
                       //Add them to our merged document at specific X/Y coords
                       w.DirectContent.AddTemplate ( imp1, 0, 0 );
                       w.DirectContent.AddTemplate ( imp2, NewPageSize.Width / 2, 0 );
                       doc.Close ( );
                   }
               }
           }
           r1.Close ( );
           r2.Close ( );

       }
   }



谢谢,谢谢,



Thanks and advance,

推荐答案

最终我正在解决问题

使用
插入了itextsharp.dll iam //
finally i am solving the problem

insted of itextsharp.dll iam using
//
 using PdfSharp.Pdf.IO;
 using PdfSharp.Pdf;
  
 class Program
 {
 //Static variables used throughout the program 
//The folder that we''ll be saving things to 
static string WorkingFolder = Environment.GetFolderPath ( Environment.SpecialFolder.Desktop );
 static string filepath = "F:\\";
 //The samples images that we''ll create and work with 
static string pdffile1 = Path.Combine ( filepath, "pdf1.pdf" );
 static string pdffile2 = Path.Combine ( filepath, "pdf2.pdf" );
 //These two arrays are just used to create our sample images 
//static Color[] colors = new Color[] { Color.RED, Color.BLUE };
 static string[] files = new string[] { pdffile1, pdffile2 };
 //Our main export file 
static string MergedFileresultspath = Path.Combine ( WorkingFolder, "Mergedresults.pdf" );
  
 
static void Main ( string[] args )
 {
 string[] strings = { pdffile1, pdffile2 };
 MergeMultiplePDFIntoSinglePDF ( MergedFileresultspath, strings );
 //MergeDocuments ( );
 }

 private static void MergeMultiplePDFIntoSinglePDF ( string outputFilePath, string[] pdfFiles )
 {
 Console.WriteLine ( "Merging started....." );
 PdfDocument outputPDFDocument = new PdfDocument ( );
  
 foreach (string pdfFile in pdfFiles)
 {
 PdfDocument inputPDFDocument = PdfReader.Open ( pdfFile, PdfDocumentOpenMode.Import );
 outputPDFDocument.Version = inputPDFDocument.Version;
 foreach (PdfPage page in inputPDFDocument.Pages)
 {
 outputPDFDocument.AddPage ( page );
 }
 }
 outputPDFDocument.Save ( outputFilePath );
 Console.WriteLine ( "Successfully Completed the pdf documents" );
 Console.WriteLine ( "File path is: {0}", MergedFileresultspath );
 Process.Start ( MergedFileresultspath );
 Console.ReadLine ( );
 }


这篇关于合并两个pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆