Je dois ajouter plusieurs fichiers PDF (chaque page) à mon pdf principal. Ceux-ci doivent être ajoutés après un numéro de page spécifique et non ajoutés à la fin.en utilisant PdfCopy pour fusionner des fichiers pdf
Comment puis-je
1: fusionner le pdf à un numéro de page spécifique
2: pdfCopy.AddDocument n'est pas disponible. J'ai testé avec la version 5.4.3, 5.4.5 et 5.5.10. Qu'est-ce que j'oublie ici? Tous disent utiliser 5.X que je suis ...
'PdfCopy' does not contain a definition for 'AddDocument' and no extension method 'AddDocument' accepting a first argument of type 'PdfCopy' could be found (are you missing a using directive or an assembly reference?)
3: Comment gérer quand pageToInsert à est supérieur au nombre total de pages dans la source?
J'ai regardé des tonnes de docs maintenant. Tous disent utiliser PdfCopy et .AddDocument ...
Merging multiple PDFs using iTextSharp in c#.net
ici est ma première prise à lui ....
using System;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace PdfMergeTest
{
public partial class Form1 : Form
{
private const string baseFile = "baseFile.tmp";
private const string baseTempPdfFileName = "temp.pdf";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnMerge_Click(object sender, EventArgs e)
{
if (!CheckBasePaths())
return;
//get the files to merge to baseFile
var filesToMerge = GetAllFilesToMerge();
if (filesToMerge.Length == 0)
return;
//get basefile to which we need to merge the above files, it is with .tmp ext
var baseFileWithPath = GetBaseFile();
if (string.IsNullOrWhiteSpace(baseFileWithPath))
return;
//temp base pdf
var tempPdfWithPath = GetBaseTempFile();
if (string.IsNullOrWhiteSpace(tempPdfWithPath))
return;
//loop through the files to merge and merge into baseFile
var page = 2; //page where to merge the file, we are not appending to the end. Actual code will find the page from source where to merge and will add 1 to it
foreach (FileInfo toMerge in filesToMerge)
{
//copy the base file as temp file for source; for debugging purposes at this time
File.Copy(baseFileWithPath, tempPdfWithPath, true);
//start merging, first at #2, second at #4, third at #6 and so on
MergeFiles(baseFileWithPath, tempPdfWithPath, toMerge.FullName, page);
page += 2;
}
}
private bool CheckBasePaths()
{
if (string.IsNullOrWhiteSpace(txtBaseDir.Text))
{
MessageBox.Show("No Base Directory");
return false;
}
if (string.IsNullOrWhiteSpace(txtFilesToMergeToBase.Text))
{
MessageBox.Show("No files to merge Directory");
return false;
}
if (!Directory.Exists(txtBaseDir.Text))
{
MessageBox.Show("Base dir does not exist");
return false;
}
if (!Directory.Exists(txtFilesToMergeToBase.Text))
{
MessageBox.Show("Files to merge dir does not exist");
return false;
}
return true;
}
private FileInfo[] GetAllFilesToMerge()
{
DirectoryInfo d = new DirectoryInfo(txtFilesToMergeToBase.Text);
FileInfo[] files = d.GetFiles("*.pdf");
if (files.Length == 0)
MessageBox.Show("No files to merge");
return files;
}
private String GetBaseFile()
{
var myBaseFile = Path.Combine(txtBaseDir.Text, baseFile);
if (!File.Exists(myBaseFile))
{
myBaseFile = "";
MessageBox.Show("Base file missing");
}
return myBaseFile;
}
private String GetBaseTempFile()
{
var myBaseTempFile = Path.Combine(txtBaseDir.Text, baseTempPdfFileName);
return myBaseTempFile;
}
private void MergeFiles(string originalFile, string sourceFile, string toMergeFile, int insertPage)
{
Document document = null;
PdfCopy pdfCopy = null;
PdfReader pdfReader = null;
try
{
//Step#1: create a document object
document = new Document();
//Step#2: create a writer that listen to the document
pdfCopy = new PdfSmartCopy(document, new FileStream(originalFile, FileMode.Create));
if (pdfCopy == null)
return;
//Step#3: open document
document.Open();
//Step#4: create a reader for the toMergeFile and add document
pdfReader = new PdfReader(toMergeFile);
//add the entire document instead of page by page
pdfCopy.AddDocument(pdfReader);
pdfReader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (pdfReader != null) pdfReader.Close();
if (pdfCopy != null) pdfCopy.Close();
if (document != null) document.Close();
}
}
}
}
J'ai regardé suivant à l'aide .AddPage
mais ce n'est pas ce que je veux.
http://www.worldbestlearningcenter.com/index_files/csharp-combine-pdf-files.htm
La méthode 'addDocument()' est certainement présent dans iTextSharp 5.4.5 et 5.4.11, mais il n'a pas été disponible en 5.0.0. Peut-être avez-vous différentes versions d'iTextSharp dans votre environnement, et peut-être utilisez-vous une ancienne version sans vous rendre compte que vous utilisez cette ancienne version. Comment savoir? Créez un exemple simple Hello World et regardez la ligne de production (voir Propriétés du document dans Adobe Reader). Cela vous dira la version que vous utilisez réellement. –
J'ai configuré un nouveau test et téléchargé les versions 5.4.5 et 5.5.10. Donc, le code de test ci-dessus provient de mon projet de test. AddDocument n'est pas disponible. Maintenant, le zip que j'ai téléchargé, même si dit 5.5.10, contient en fait l'ancienne version? Pouvez-vous me pointer vers la bonne url? –
en utilisant 'iTextSharp.text' et' iTextSharp.text.pdf' et la DLL est pdfa. cela vous semble-t-il correct @BrunoLowagie? –