[英]Merging and Saving PDF with pdfsharp only works if the target path is empty
我正在嘗試合並具有相同名稱的X PDF(例如186666、186666_AB,186666-AC,187777、187777_AB等)。 第一次運行程序時,此方法運行良好。 第二次嘗試運行它時,會生成一些PDF,而有些則沒有。 因此186666正在工作,而187777無法工作。 然后,我的防病毒程序將我的.exe隔離,並顯示錯誤“未經授權的文件加密”。
因此,我手動刪除了所有文件,程序通過了。 我在代碼中刪除了它們,並且當目標路徑中有文件時,該程序將進入隔離狀態。
該程序是Visual Basic中的.NET C#。
我不太喜歡編程,我也不知道這個錯誤是什么意思,或者我怎么解決。
提前致謝 :)
代碼是C#。 它運行在Win10 PC(而非服務器)上。 PDF是從不同位置生成的。 有些來自其他公司,有些則通過PDFCreator從Excel工具中打印。
static void Main(string[] args)
{
//Deletes every File in Target Path
DeleteFiles();
int AnzahlBestellungen = 0;
//Gets all pdf files from start folder
string[] allFiles = Directory.GetFiles(pfad);
foreach (string startDatei in allFiles)
{
string fileName = System.IO.Path.GetFileNameWithoutExtension(startDatei);
int lenght = fileName.Length;
//6 is the shortest name for the pdf, only if this exists
//PDFS have to be merges
if (lenght == 6)
{
AnzahlBestellungen = AnzahlBestellungen + 1;
string searchName = fileName + "*.pdf";
//Gets PDFs with the same first 6 letters
string[] mergeDateien = GetFiles(pfad, searchName, SearchOption.TopDirectoryOnly);
//Merges all PDFs with the same name
Merge(mergeDateien, fileName);
}
}
}
public static void Merge(string[] pdfs, string dateiName)
{
//Name of the new PDF
string dateiNameNeu = ziel + dateiName + ".pdf";
using (PdfDocument targetDoc = new PdfDocument())
{
//All Names from above mentioned same names about to be merged
foreach (string pdf in pdfs)
{
using (PdfDocument pdfDoc = PdfReader.Open(pdf, PdfDocumentOpenMode.Import))
{
for (int i = 0; i < pdfDoc.PageCount; i++)
{
PdfPage page = pdfDoc.Pages[i];
targetDoc.AddPage(page);
}
}
}
targetDoc.Save(dateiNameNeu);
}
調試時,代碼運行平穩。 如果運行.exe,則AntiVirus TrendMicro服務器會檢測到該文件。
如果有人在乎,趨勢科技會將程序視為病毒。 將.exe添加到例外列表后,該程序多次運行而沒有出現問題。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.