IRONSOFTWAREHOME

混合方向PDF將每頁列印為橫向

Curtis Chau
Curtis Chau
Updated: 2026年7月3日

IronPrint 2026.1.5 在列印同時包含直向和橫向頁面的PDF時,不保留每頁的方向。 每頁到達列印機時均為橫向,無論其原始方向如何。

這會影響2026.1.5在Windows 10和Windows Server 2022上的.NET 8版本。此版本沒有修正的版本。

引擎將整個文件設置為單一方向,而不是讀取每頁的尺寸。 將文件分割為相同方向頁面的組合並分別列印每個組合可恢復正確的輸出。

解決方案

**建議:**將PDF按方向一致的批次列印,而不是作為單一作業。

1. 檢測每頁的方向

打開PDF並比較每一頁的寬度與高度。 寬度大於高度的頁面為橫向; 其他為直向。

2. 將連續相同方向的頁面分組

按順序瀏覽頁面,並在方向轉變時開始新的批次。 每個批次最終作為一個連續的頁面範圍,具有一種方向。

3. 將每個批次作為獨立作業列印

分開發送每個範圍讓列印機應用該範圍的方向,而不是強迫整個文件轉為橫向。

4. 手動處理多份拷貝

由於文件現在分散在多個作業中,每次複製時都要迴圈一次並完整地印出批次序列。這可使每份拷貝的頁面順序保持完整。

完整實作如下:

using IronPrint;
using IronPdf;
await PrintDocumentByOrientationBatchesAsync(
    documentPath: documentPath,
    printerName: printerName,
    numberOfCopies: effectiveCopies);
async Task PrintDocumentByOrientationBatchesAsync(
    string documentPath,
    string printerName,
    int numberOfCopies)
{
    var tempFolder = Path.Combine(
        Path.GetTempPath(),
        "ironprint-orientation-workaround",
        Guid.NewGuid().ToString("N"));
    Directory.CreateDirectory(tempFolder);
    try
    {
        using var pdf = PdfDocument.FromFile(documentPath);
        var batches = GetConsecutiveOrientationBatches(pdf);
        // Since the document is printed in multiple jobs, copies are handled manually
        // to preserve the full document order per copy.
        for (var copy = 1; copy <= numberOfCopies; copy++)
        {
            foreach (var batch in batches)
            {
                var batchPath = Path.Combine(
                    tempFolder,
                    $"copy_{copy}_pages_{batch.StartPageNumber}_{batch.EndPageNumber}_{batch.Orientation}.pdf");
                using var batchPdf = pdf.CopyPages(batch.StartIndex, batch.EndIndex);
                batchPdf.SaveAs(batchPath);
                var printSettings = new PrintSettings
                {
                    PrinterName = printerName,
                    NumberOfCopies = 1,
                    PaperOrientation = batch.Orientation == DetectedPageOrientation.Landscape
                        ? PaperOrientation.Landscape
                        : PaperOrientation.Portrait,
                    PaperSize = PaperSize.PrinterDefault
                };
                await Printer.PrintAsync(batchPath, printSettings);
                // Optional delay to help ensure print jobs are queued in order.
                await Task.Delay(500);
            }
        }
    }
    finally
    {
        try
        {
            Directory.Delete(tempFolder, recursive: true);
        }
        catch
        {
            // Ignore cleanup errors in case the print spooler is still accessing the files.
        }
    }
}
List<PageOrientationBatch> GetConsecutiveOrientationBatches(PdfDocument pdf)
{
    var batches = new List<PageOrientationBatch>();
    if (pdf.PageCount == 0)
    {
        return batches;
    }
    var currentOrientation = GetPageOrientation(pdf.Pages[0].Width, pdf.Pages[0].Height);
    var batchStartIndex = 0;
    for (var pageIndex = 1; pageIndex < pdf.PageCount; pageIndex++)
    {
        var page = pdf.Pages[pageIndex];
        var pageOrientation = GetPageOrientation(page.Width, page.Height);
        if (pageOrientation != currentOrientation)
        {
            batches.Add(new PageOrientationBatch(
                StartIndex: batchStartIndex,
                EndIndex: pageIndex - 1,
                Orientation: currentOrientation));
            batchStartIndex = pageIndex;
            currentOrientation = pageOrientation;
        }
    }
    batches.Add(new PageOrientationBatch(
        StartIndex: batchStartIndex,
        EndIndex: pdf.PageCount - 1,
        Orientation: currentOrientation));
    return batches;
}
DetectedPageOrientation GetPageOrientation(double width, double height)
{
    return width > height
        ? DetectedPageOrientation.Landscape
        : DetectedPageOrientation.Portrait;
}
record PageOrientationBatch(
    int StartIndex,
    int EndIndex,
    DetectedPageOrientation Orientation)
{
    public int StartPageNumber => StartIndex + 1;
    public int EndPageNumber => EndIndex + 1;
}
enum DetectedPageOrientation
{
    Portrait,
    Landscape
}
C#

GetConsecutiveOrientationBatches 為每個連續組合產生一個PaperOrientation。 作業之間的Task.Delay(500)有助於假脱机程式按順序排隊它們。

警告: 每個方向範圍都作為單獨的列印作業發送。 在物理列印機上,作業連續排隊,但在Microsoft Print to PDF或類似的虛擬列印機中,每個批次可能會提示輸出其自己的文件。
Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

準備好開始了嗎?

Nuget Downloads 46,090版本:2026.9剛剛發布

立即獲取您的免費30天試用金鑰
不需要信用卡或帳戶建立
C# NuGet PDF程式庫
安裝方法NuGet

版本: 2026.9

PM > Install-Package IronPrint
nuget.org/packages/IronPrint/
  1. 在解決方案資源管理器中,右鍵點擊引用,管理NuGet套件
  2. 選擇瀏覽並搜尋"IronPrint"
  3. 選擇套件並安裝
C# PDF DLL
下載DLL

版本: 2026.9

  1. 下載並解壓IronPrint到比如~/Libs這樣的位置在您的Solution目錄下
  2. 在Visual Studio解決方案資源管理器中,右鍵點擊引用。選擇瀏覽,"IronPrint.dll"

授權從$999

有問題嗎?聯繫我們的開發團隊。

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預約您的免費
現場演示
Booking Badge

全球數百萬工程師信賴

Iron Software的客戶標誌
獲得非義務性諮詢
完成下列表單或發郵件到sales@ironsoftware.com
您的資訊將始終保密。
全球數百萬工程師信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立