Memformat Dokumen PDF menggunakan C#

Memformat Dokumen PDF

Mendapatkan Properti Jendela Dokumen dan Tampilan Halaman

Topik ini membantu Anda memahami cara mendapatkan properti jendela dokumen, aplikasi penampil, dan cara halaman ditampilkan. Untuk mengatur properti ini:

Buka file PDF menggunakan kelas Document. Sekarang, Anda dapat mengatur properti objek Document, seperti

  • CenterWindow – Pusatkan jendela dokumen di layar. Default: false.
  • Direction – Urutan bacaan. Ini menentukan bagaimana halaman disusun saat ditampilkan berdampingan. Default: kiri ke kanan.
  • DisplayDocTitle – Tampilkan judul dokumen di bilah judul jendela dokumen. Default: false (judul ditampilkan).
  • HideMenuBar – Sembunyikan atau tampilkan bilah menu jendela dokumen. Default: false (bilah menu ditampilkan).
  • HideToolBar – Sembunyikan atau tampilkan bilah alat jendela dokumen. Default: false (bilah alat ditampilkan).
  • HideWindowUI – Sembunyikan atau tampilkan elemen jendela dokumen seperti bilah gulir. Default: false (elemen UI ditampilkan).
  • NonFullScreenPageMode – Cara dokumen ditampilkan saat tidak ditampilkan dalam mode layar penuh.
  • PageLayout – Tata letak halaman.
  • PageMode – Cara dokumen ditampilkan saat pertama kali dibuka. Opsi yang tersedia adalah menampilkan thumbnail, layar penuh, menampilkan panel lampiran.

Cuplikan kode berikut juga bekerja dengan pustaka Aspose.PDF.Drawing.

Cuplikan kode berikut menunjukkan cara mendapatkan properti menggunakan kelas Document.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetDocumentWindowProperties()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "GetDocumentWindow.pdf"))
    {
        // Get different document properties
        // Position of document's window - Default: false
        Console.WriteLine("CenterWindow : {0}", document.CenterWindow);

        // Predominant reading order; determines the position of page
        // When displayed side by side - Default: L2R
        Console.WriteLine("Direction : {0}", document.Direction);

        // Whether window's title bar should display document title
        // If false, title bar displays PDF file name - Default: false
        Console.WriteLine("DisplayDocTitle : {0}", document.DisplayDocTitle);

        // Whether to resize the document's window to fit the size of
        // First displayed page - Default: false
        Console.WriteLine("FitWindow : {0}", document.FitWindow);

        // Whether to hide menu bar of the viewer application - Default: false
        Console.WriteLine("HideMenuBar : {0}", document.HideMenubar);

        // Whether to hide tool bar of the viewer application - Default: false
        Console.WriteLine("HideToolBar : {0}", document.HideToolBar);

        // Whether to hide UI elements like scroll bars
        // And leaving only the page contents displayed - Default: false
        Console.WriteLine("HideWindowUI : {0}", document.HideWindowUI);

        // Document's page mode. How to display document on exiting full-screen mode.
        Console.WriteLine("NonFullScreenPageMode : {0}", document.NonFullScreenPageMode);

        // The page layout i.e. single page, one column
        Console.WriteLine("PageLayout : {0}", document.PageLayout);

        // How the document should display when opened
        // I.e. show thumbnails, full-screen, show attachment panel
        Console.WriteLine("PageMode : {0}", document.PageMode);
    }
}

Mengatur Properti Jendela Dokumen dan Tampilan Halaman

Topik ini menjelaskan cara mengatur properti jendela dokumen, aplikasi penampil, dan tampilan halaman. Untuk mengatur berbagai properti ini:

  1. Buka file PDF menggunakan kelas Document.
  2. Atur properti objek Document.
  3. Simpan file PDF yang diperbarui menggunakan metode Save.

Properti yang tersedia adalah:

  • CenterWindow.
  • Direction.
  • DisplayDocTitle.
  • FitWindow.
  • HideMenuBar.
  • HideToolBar.
  • HideWindowUI.
  • NonFullScreenPageMode.
  • PageLayout.
  • PageMode.

Masing-masing digunakan dan dijelaskan dalam kode di bawah ini. Cuplikan kode berikut menunjukkan cara mengatur properti menggunakan kelas Document.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetDocumentWindowProperties()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "SetDocumentWindow.pdf"))
    {
        // Set different document properties
        // Specify to position document's window - Default: false
        document.CenterWindow = true;

        // Predominant reading order; determines the position of page
        // When displayed side by side - Default: L2R
        document.Direction = Aspose.Pdf.Direction.R2L;

        // Specify whether window's title bar should display document title
        // If false, title bar displays PDF file name - Default: false
        document.DisplayDocTitle = true;

        // Specify whether to resize the document's window to fit the size of
        // First displayed page - Default: false
        document.FitWindow = true;

        // Specify whether to hide menu bar of the viewer application - Default: false
        document.HideMenubar = true;

        // Specify whether to hide tool bar of the viewer application - Default: false
        document.HideToolBar = true;

        // Specify whether to hide UI elements like scroll bars
        // And leaving only the page contents displayed - Default: false
        document.HideWindowUI = true;

        // Document's page mode. Specify how to display document on exiting full-screen mode.
        document.NonFullScreenPageMode = Aspose.Pdf.PageMode.UseOC;

        // Specify the page layout i.e. single page, one column
        document.PageLayout = Aspose.Pdf.PageLayout.TwoColumnLeft;

        // Specify how the document should display when opened
        // I.e. show thumbnails, full-screen, show attachment panel
        document.PageMode = Aspose.Pdf.PageMode.UseThumbs;

        // Save PDF document
        document.Save(dataDir + "SetDocumentWindow_out.pdf");
    }
}

Menyematkan Font dalam file PDF yang ada

Pembaca PDF mendukung inti 14 font sehingga dokumen dapat ditampilkan dengan cara yang sama terlepas dari platform tempat dokumen ditampilkan. Ketika sebuah PDF berisi font yang bukan salah satu dari 14 font inti, sematkan font ke file PDF untuk menghindari penggantian font.

Aspose.PDF for .NET mendukung penyematan font dalam file PDF yang ada. Anda dapat menyematkan font lengkap atau subset dari font. Untuk menyematkan font, buka file PDF menggunakan kelas Document. Kemudian gunakan kelas Aspose.Pdf.Text.Font untuk menyematkan font ke dalam file PDF. Untuk menyematkan font lengkap, gunakan properti IsEmbeded dari kelas Font; untuk menggunakan subset dari font, gunakan properti IsSubset.

Cuplikan kode berikut menunjukkan cara menyematkan font dalam file PDF.

Menyematkan Font Tipe 1 Standar

Beberapa dokumen PDF memiliki font dari set font khusus Adobe. Font dari set ini disebut “Font Tipe 1 Standar”. Set ini mencakup 14 font dan penyematan jenis font ini memerlukan penggunaan bendera khusus yaitu Aspose.Pdf.Document.EmbedStandardFonts. Berikut adalah cuplikan kode yang dapat digunakan untuk mendapatkan dokumen dengan semua font disematkan termasuk Font Tipe 1 Standar:

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void EmbedFontsType1ToPdf()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Text();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
    {
        // Set EmbedStandardFonts property of document
        document.EmbedStandardFonts = true;

        // Iterate through each page
        foreach (var page in document.Pages)
        {
            if (page.Resources.Fonts != null)
            {
                foreach (var pageFont in page.Resources.Fonts)
                {
                    // Check if font is already embedded
                    if (!pageFont.IsEmbedded)
                    {
                        pageFont.IsEmbedded = true;
                    }
                }
            }
        }

        // Save PDF document
        document.Save(dataDir + "EmbeddedFontsUpdated_out.pdf");
    }
}

Menyematkan Font saat membuat PDF

Jika Anda perlu menggunakan font lain selain 14 font inti yang didukung oleh Adobe Reader, Anda harus menyematkan deskripsi font saat menghasilkan file PDF. Jika informasi font tidak disematkan, Adobe Reader akan mengambilnya dari Sistem Operasi jika terinstal di sistem, atau akan membangun font pengganti sesuai dengan deskriptor font dalam PDF.

Harap dicatat bahwa font yang disematkan harus diinstal di mesin host yaitu, dalam kasus kode berikut font ‘Univers Condensed’ diinstal di sistem.

Kami menggunakan properti IsEmbedded dari kelas Font untuk menyematkan informasi font ke dalam file PDF. Mengatur nilai properti ini ke ‘True’ akan menyematkan file font lengkap ke dalam PDF, dengan mengetahui bahwa ini akan meningkatkan ukuran file PDF. Berikut adalah cuplikan kode yang dapat digunakan untuk menyematkan informasi font ke dalam PDF.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void EmbedFontWhileCreatingPdf()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Create a section in the Pdf object
        var page = document.Pages.Add();

        // Create a TextFragment
        var fragment = new Aspose.Pdf.Text.TextFragment("");

        // Create a TextSegment with sample text
        var segment = new Aspose.Pdf.Text.TextSegment(" This is a sample text using Custom font.");

        // Create and configure TextState
        var ts = new Aspose.Pdf.Text.TextState();
        ts.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
        ts.Font.IsEmbedded = true;
        segment.TextState = ts;

        // Add the segment to the fragment
        fragment.Segments.Add(segment);

        // Add the fragment to the page
        page.Paragraphs.Add(fragment);

        // Save PDF Document
        document.Save(dataDir + "EmbedFontWhileDocCreation_out.pdf");
    }
}

Mengatur Nama Font Default saat Menyimpan PDF

Ketika dokumen PDF berisi font, yang tidak tersedia dalam dokumen itu sendiri dan di perangkat, API mengganti font ini dengan font default. Ketika font tersedia (diinstal di perangkat atau disematkan ke dalam dokumen), PDF keluaran harus memiliki font yang sama (tidak boleh diganti dengan font default). Nilai dari font default harus berisi nama font (bukan jalur ke file font). Kami telah menerapkan fitur untuk mengatur nama font default saat menyimpan dokumen sebagai PDF. Cuplikan kode berikut dapat digunakan untuk mengatur font default:

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetDefaultFontOnDocumentSave(string documentName, string newName)
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Open PDF document
    using (var fs = new FileStream(dataDir + "GetDocumentWindow.pdf", FileMode.Open))
    {
        using (var document = new Aspose.Pdf.Document(fs))
        {
            // Create PdfSaveOptions and specify Default Font Name
            var pdfSaveOptions = new Aspose.Pdf.PdfSaveOptions
            {
                DefaultFontName = newName
            };

            // Save PDF document
            document.Save(dataDir + "DefaultFont_out.pdf", pdfSaveOptions);
        }
    }
}

Mendapatkan Semua Font dari Dokumen PDF

Jika Anda ingin mendapatkan semua font dari dokumen PDF, Anda dapat menggunakan metode FontUtilities.GetAllFonts() yang disediakan dalam kelas Document. Silakan periksa cuplikan kode berikut untuk mendapatkan semua font dari dokumen PDF yang ada:

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetAllFontsFromPdf()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
    {
        // Get all fonts used in the document
        var fonts = document.FontUtilities.GetAllFonts();

        // Iterate through each font and print its name
        foreach (var font in fonts)
        {
            Console.WriteLine(font.FontName);
        }
    }
}

Mendapatkan Peringatan untuk Penggantian Font

Aspose.PDF for .NET menyediakan metode untuk mendapatkan notifikasi tentang penggantian font untuk menangani kasus penggantian font. Cuplikan kode di bawah ini menunjukkan cara menggunakan fungsionalitas yang sesuai.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void NotificationFontSubstitution()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
    {
        // Attach the FontSubstitution event handler
        document.FontSubstitution += OnFontSubstitution;
        // You can use lambda
        // (oldFont, newFont) => Console.WriteLine(string.Format("Font '{0}' was substituted with another font '{1}'",
        //                                                                        oldFont.FontName, newFont.FontName));

        // Save PDF document
        document.Save(dataDir + "NotificationFontSubstitution_out.pdf");
    }
}

Metode OnFontSubstitution adalah sebagai berikut.

private static void OnFontSubstitution(Aspose.Pdf.Text.Font oldFont, Aspose.Pdf.Text.Font newFont)
{
    // Handle the font substitution event here
    Console.WriteLine(string.Format("Font '{0}' was substituted with another font '{1}'",
        oldFont.FontName, newFont.FontName));
}

Meningkatkan Penyematan Font menggunakan FontSubsetStrategy

Fitur untuk menyematkan font sebagai subset dapat dicapai dengan menggunakan properti IsSubset, tetapi terkadang Anda ingin mengurangi set font yang sepenuhnya disematkan hanya menjadi subset yang digunakan dalam dokumen. Aspose.Pdf.Document memiliki properti FontUtilities yang mencakup metode SubsetFonts(FontSubsetStrategy subsetStrategy). Dalam metode SubsetFonts(), parameter subsetStrategy membantu menyetel strategi subset. FontSubsetStrategy mendukung dua varian berikut dari subsetting font.

  • SubsetAllFonts - Ini akan menyubset semua font yang digunakan dalam dokumen.
  • SubsetEmbeddedFontsOnly - Ini akan menyubset hanya font yang sepenuhnya disematkan ke dalam dokumen.

Cuplikan kode berikut menunjukkan cara mengatur FontSubsetStrategy:

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetFontSubsetStrategy()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
    {
        // All fonts will be embedded as subset into document in case of SubsetAllFonts.
        document.FontUtilities.SubsetFonts(Aspose.Pdf.FontSubsetStrategy.SubsetAllFonts);

        // Font subset will be embedded for fully embedded fonts but fonts which are not embedded into document will not be affected.
        document.FontUtilities.SubsetFonts(Aspose.Pdf.FontSubsetStrategy.SubsetEmbeddedFontsOnly);

        // Save PDF document
        document.Save(dataDir + "SetFontSubsetStrategy_out.pdf");
    }
}

Mengatur-Faktor Zoom dari File PDF

Terkadang, Anda ingin menentukan apa faktor zoom saat ini dari dokumen PDF. Dengan Aspose.Pdf, Anda dapat mengetahui nilai saat ini serta mengatur satu.

Properti Destination dari kelas GoToAction memungkinkan Anda untuk mendapatkan nilai zoom yang terkait dengan file PDF. Demikian pula, dapat digunakan untuk mengatur faktor zoom file.

Mengatur Faktor Zoom

Cuplikan kode berikut menunjukkan cara mengatur faktor zoom dari file PDF.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetZoomFactor()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "SetZoomFactor.pdf"))
    {
        // Create GoToAction with a specific zoom factor
        var action = new Aspose.Pdf.Annotations.GoToAction(new Aspose.Pdf.Annotations.XYZExplicitDestination(1, 0, 0, 0.5));
        document.OpenAction = action;

        // Save PDF document
        document.Save(dataDir + "ZoomFactor_out.pdf");
    }
}

Mendapatkan Faktor Zoom

Cuplikan kode berikut menunjukkan cara mendapatkan faktor zoom dari file PDF.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetZoomFactor()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "Zoomed_pdf.pdf"))
    {
        // Create GoToAction object
        if (document.OpenAction is Aspose.Pdf.Annotations.GoToAction action)
        {
            // Get the Zoom factor of PDF file
            if (action.Destination is Aspose.Pdf.Annotations.XYZExplicitDestination destination)
            {
                System.Console.WriteLine(destination.Zoom); // Document zoom value;
            }
        }
    }
}

Mengatur Properti Preset Dialog Cetak

Aspose.PDF memungkinkan pengaturan properti Preset Dialog Cetak dari dokumen PDF. Ini memungkinkan Anda untuk mengubah properti DuplexMode untuk dokumen PDF yang diatur ke simplex secara default. Ini dapat dicapai menggunakan dua metodologi berbeda seperti yang ditunjukkan di bawah ini.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetPrintDialogPresetProperties()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        document.Pages.Add();

        // Set duplex printing to DuplexFlipLongEdge
        document.Duplex = Aspose.Pdf.PrintDuplex.DuplexFlipLongEdge;

        // Save PDF document
        document.Save(dataDir + "SetPrintDlgPresetProperties_out.pdf", Aspose.Pdf.SaveFormat.Pdf);
    }
}

Mengatur Properti Preset Dialog Cetak menggunakan Editor Konten PDF

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetPrintDialogPresetPropertiesUsingPdfContentEditor()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    string inputFile = dataDir + "input.pdf";

    using (var ed = new Aspose.Pdf.Facades.PdfContentEditor())
    {
        // Bind PDF document
        ed.BindPdf(inputFile);

        // Check if the file has duplex flip short edge
        if ((ed.GetViewerPreference() & Aspose.Pdf.Facades.ViewerPreference.DuplexFlipShortEdge) > 0)
        {
            Console.WriteLine("The file has duplex flip short edge");
        }

        // Change the viewer preference to duplex flip short edge
        ed.ChangeViewerPreference(Aspose.Pdf.Facades.ViewerPreference.DuplexFlipShortEdge);

        // Save PDF document
        ed.Save(dataDir + "SetPrintDlgPropertiesUsingPdfContentEditor_out.pdf");
    }
}